Show Form from (non-GUI) Thread
-
I have a thread (which is not the main GUI thread )that I want to be able to create a form using the Show method. The thread basically loops for commands and responds to them. Some of the commands are to display a form - and then wait for the next command. Now when I try to do this the Form freezes i.e. not responding. Now I assume that is because the thread is blocking waiting (Monitor.Wait) for the next command. I suppose this blocking prevent the GUI from responding to events. So my question is related to this problem. 1. Is is good practice to create Form from a thread other than the main GUI 2. Can I do a for.Show method and spawn it into a worker thread? All thoughts and suggestions would be appreciated.
-
I have a thread (which is not the main GUI thread )that I want to be able to create a form using the Show method. The thread basically loops for commands and responds to them. Some of the commands are to display a form - and then wait for the next command. Now when I try to do this the Form freezes i.e. not responding. Now I assume that is because the thread is blocking waiting (Monitor.Wait) for the next command. I suppose this blocking prevent the GUI from responding to events. So my question is related to this problem. 1. Is is good practice to create Form from a thread other than the main GUI 2. Can I do a for.Show method and spawn it into a worker thread? All thoughts and suggestions would be appreciated.
Hello
LiamD wrote:
Is is good practice to create Form from a thread other than the main GUI
If you want your working thread to be blocked by the shown forms, you can use it to show these forms. Simple, isn't it??:) OTOH I don't think is what you want. So, simply make a thread for each form that is shown. The thread should be alive as long as the form is visible. Once the form is closed, abort the thread nd dispose it. And yes of course!! Sometimes this is a good practice, depending on the structure of your program.
LiamD wrote:
Can I do a for.Show method and spawn it into a worker thread?
I don't know what is a "for.Show" method, but you can -and probably should- show each form on its own thread as above.
Regards:rose:
-
Hello
LiamD wrote:
Is is good practice to create Form from a thread other than the main GUI
If you want your working thread to be blocked by the shown forms, you can use it to show these forms. Simple, isn't it??:) OTOH I don't think is what you want. So, simply make a thread for each form that is shown. The thread should be alive as long as the form is visible. Once the form is closed, abort the thread nd dispose it. And yes of course!! Sometimes this is a good practice, depending on the structure of your program.
LiamD wrote:
Can I do a for.Show method and spawn it into a worker thread?
I don't know what is a "for.Show" method, but you can -and probably should- show each form on its own thread as above.
Regards:rose:
I was concerned because i have never seen any examples of projects creating a thread for each form. Can you please give an example of code for this? Something liek this ... Form f = new Form(); t = new Thread(new ThreadStart(f)); t.Start(); I just want to make sure that I will not run into problem using this method when running in a multi threaded project. By the way I meant to write Form.Show() method above. Thanks, Liam
-
I was concerned because i have never seen any examples of projects creating a thread for each form. Can you please give an example of code for this? Something liek this ... Form f = new Form(); t = new Thread(new ThreadStart(f)); t.Start(); I just want to make sure that I will not run into problem using this method when running in a multi threaded project. By the way I meant to write Form.Show() method above. Thanks, Liam
Hello Well, running into trouble in multithreading is as easy as slipping in the mud in a rainy day!! There are two scenarios. One if you want to keep track of each thread -for several reasons I suppose-. This is the hard way, so I'd rather skip it for now:-D. The easy way is to launch each thread and let it die as it finishes without tracking -Thank God for the GarbageCollector!!-. Here is a sample code:
private void button2_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
Thread MyThread = new Thread(new ThreadStart(MyThreadMethod));
MyThread.Start();
}private void MyThreadMethod()
{
Form1 MyForm = new Form1();
MyForm.ShowDialog();//To Block the thread excution
}The Thread will die and get collected once the form is closed. Carful not to open too many forms though.
Regards:rose: