Click here to Skip to main content
15,903,012 members
Home / Discussions / C#
   

C#

 
AnswerRe: How can I check if the datasource is writable? Pin
Dave Kreskowiak28-Jun-06 15:51
mveDave Kreskowiak28-Jun-06 15:51 
GeneralRe: How can I check if the datasource is writable? Pin
AngryC28-Jun-06 16:10
AngryC28-Jun-06 16:10 
GeneralRe: How can I check if the datasource is writable? Pin
Edbert P28-Jun-06 19:01
Edbert P28-Jun-06 19:01 
GeneralRe: How can I check if the datasource is writable? Pin
AngryC28-Jun-06 19:39
AngryC28-Jun-06 19:39 
GeneralRe: How can I check if the datasource is writable? Pin
Robert Rohde28-Jun-06 20:01
Robert Rohde28-Jun-06 20:01 
GeneralRe: How can I check if the datasource is writable? Pin
Dave Kreskowiak29-Jun-06 1:31
mveDave Kreskowiak29-Jun-06 1:31 
QuestionModal Dialog help in C# [modified] Pin
McSmack28-Jun-06 14:57
McSmack28-Jun-06 14:57 
AnswerRe: Modal Dialog help in C# Pin
Alexander Wiseman28-Jun-06 15:41
Alexander Wiseman28-Jun-06 15:41 
McSmack,

I would agree with your assessment, that it is a threading issue where the callback is running in a different thread than the main thread. This would cause the two dialogs to act as you mention. Here is a way to test that to see if it is the case, and also a way to fix it if it is.

First, to test if they are running on different threads

1) Add the following line of code to the constructor of your main parent form:
System.Threading.Thread.CurrentThread.Name = "Main Thread";
This will give a name to the main thread so that we can identify it and distinguish it from another thread.

2) Now, inside the dialog which you want to show (the MessageForm dialog), add this line to your function ShowMyDialogBox:
System.Diagnostics.Trace.WriteLine("Thread is named: " + System.Threading.Thread.CurrentThread.Name);
This will write a line to the trace output which gives you the name of the thread which is calling ShowDialog on your second form.

Now, look at the trace output (one way is to run the program in debug mode and then look at the output in Visual Studio) and see if you see "Thread is named: Main Thread" when your ShowMyDialogBox function is called. If it is, then the dialog is being displayed on the main thread after all and something else is wrong. If it is not the same thread, then you will see something like "Thread is name: " - just blank, because the thread is unnamed.

Second, marshaling back to the main thread

If the issue is indeed that the second dialog is displayed on a different thread, here is the way you can fix your ShowMyDialogBox function so that you can marshal back to the main thread:
public void ShowMyDialogBox()
{
     if(this.InvokeRequired)
     {
          this.BeginInvoke(new MethodInvoker(this.ShowMyDialogBox));
          return;
     }

     // Now we must be on the main thread - so the rest of the function is the same
     ...
}
Basically what this is doing is checking (via InvokeRequired) whether the thread which is executing the ShowMyDialogBox function is the same thread which created the main form. If it is not, then InvokeRequired will be true, and the function BeginInvoke will post a message which the main thread will receive. The BeginInvoke will cause the main thread to execute the code contained in the method ShowMyDialogBox. Thus, when the main thread is executed the function, InvokeRequired will be false, and the rest of the code will execute as normal, with your second dialog box completely modal.

Let me know if anything in this is unclear or if you are still having problems. I don't pretend to be an expert at this, althought I have dealt with the same kind of problem you are having.

Sincerely,
Alexander Wiseman
QuestionRe: Modal Dialog help in C# [modified] Pin
Martin#28-Jun-06 19:34
Martin#28-Jun-06 19:34 
AnswerRe: Modal Dialog help in C# Pin
Alexander Wiseman29-Jun-06 5:39
Alexander Wiseman29-Jun-06 5:39 
GeneralRe: Modal Dialog help in C# [modified] Pin
Martin#29-Jun-06 6:11
Martin#29-Jun-06 6:11 
GeneralRe: Modal Dialog help in C# Pin
Alexander Wiseman29-Jun-06 6:30
Alexander Wiseman29-Jun-06 6:30 
GeneralRe: Modal Dialog help in C# Pin
Martin#29-Jun-06 6:41
Martin#29-Jun-06 6:41 
GeneralRe: Modal Dialog help in C# Pin
McSmack29-Jun-06 6:00
McSmack29-Jun-06 6:00 
QuestionRe: Modal Dialog help in C# Pin
Alexander Wiseman29-Jun-06 6:41
Alexander Wiseman29-Jun-06 6:41 
AnswerRe: Modal Dialog help in C# Pin
McSmack29-Jun-06 7:17
McSmack29-Jun-06 7:17 
QuestionRe: Modal Dialog help in C# Pin
Alexander Wiseman29-Jun-06 10:53
Alexander Wiseman29-Jun-06 10:53 
AnswerRe: Modal Dialog help in C# Pin
McSmack29-Jun-06 12:19
McSmack29-Jun-06 12:19 
GeneralRe: Modal Dialog help in C# Pin
Alexander Wiseman29-Jun-06 12:54
Alexander Wiseman29-Jun-06 12:54 
QuestionQuick button question [modified] Pin
Arkon94828-Jun-06 13:05
Arkon94828-Jun-06 13:05 
AnswerRe: Quick button question [modified] Pin
Mike Poz28-Jun-06 13:45
Mike Poz28-Jun-06 13:45 
QuestionExporting to SPSS issue Pin
Nader Elshehabi28-Jun-06 11:59
Nader Elshehabi28-Jun-06 11:59 
AnswerRe: Exporting to SPSS issue Pin
farquem828-Jun-06 16:01
farquem828-Jun-06 16:01 
GeneralRe: Exporting to SPSS issue Pin
Nader Elshehabi28-Jun-06 17:29
Nader Elshehabi28-Jun-06 17:29 
QuestionASP.net and Javascript Pin
Naji.A28-Jun-06 11:27
Naji.A28-Jun-06 11:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.