Problem on modal dialog

  • Thread starter Thread starter babylon
  • Start date Start date
B

babylon

I have a Form form1
i created another thread in from1
the thread will listen for a network packet from a server
if the thread receive the packet
i would like to open a modal dialog that will block all the user input to
the form1

i tried the following:

public class Form1 : System.Windows.Forms.Form
{
static Form1 p = null;
public Form1()
{
p = this;
InitializeComponent();
Thread t = new Thread(new ThreadStart(thread));
t.Start();
}
public static void thread()
{
Thread.Sleep(1000); // wait until a network packet comes....
Form c = new Form();
c.ShowDialog(p);
}
}

....it didn't work....the user still able to play with form1...

any idea?
thx!
 
Hi,

The multithreading mantra for Windows Forms is: "The Control.Invoke() is
king. Always use Control.Invoke when talking to a form from another thread,
unless Control.IsInvokeRequired tells you otherwise".

So what you need is this:

1. Create a public method in the Form class that would display the dialog
2. When the working thread receives a packet, call this public method
through form's Invoke method (remember Form is inherited from Control).
3. DO NOT call ShowDialog directly from the working thread. It may not only
show a non-blocking dialog, but also crash the whole application.
 
babylon said:
I have a Form form1
i created another thread in from1
the thread will listen for a network packet from a server
if the thread receive the packet
i would like to open a modal dialog that will block all the user input to
the form1

The form must be created in the main GUI thread. You can do this by
invoking a public procedure in the GUI thread.

You will find more information on multithreading + Windows Forms in this
article:

<http://www.devx.com/dotnet/Article/11358>
 
thank you very much!!!
it really works!!


Dmitriy Lapshin said:
Hi,

The multithreading mantra for Windows Forms is: "The Control.Invoke() is
king. Always use Control.Invoke when talking to a form from another thread,
unless Control.IsInvokeRequired tells you otherwise".

So what you need is this:

1. Create a public method in the Form class that would display the dialog
2. When the working thread receives a packet, call this public method
through form's Invoke method (remember Form is inherited from Control).
3. DO NOT call ShowDialog directly from the working thread. It may not only
show a non-blocking dialog, but also crash the whole application.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

babylon said:
I have a Form form1
i created another thread in from1
the thread will listen for a network packet from a server
if the thread receive the packet
i would like to open a modal dialog that will block all the user input to
the form1

i tried the following:

public class Form1 : System.Windows.Forms.Form
{
static Form1 p = null;
public Form1()
{
p = this;
InitializeComponent();
Thread t = new Thread(new ThreadStart(thread));
t.Start();
}
public static void thread()
{
Thread.Sleep(1000); // wait until a network packet comes....
Form c = new Form();
c.ShowDialog(p);
}
}

...it didn't work....the user still able to play with form1...

any idea?
thx!
 
Back
Top