Create From from different Thread <vb.net>

NeuralJack

Centurion
Joined
Jul 28, 2005
Messages
138
I realize the GUI is meant to be kept working all on the same thread. But, in this project I'd really like it if a new permanent form would show after another thread tells it to do so - and then that non-GUI thread expires.

The easy way around this would be to actually have a Hidden form up constantly in the program, and invoke the Visible property on it when it's needed. But is this a good and common practice?

So far, all I can do is create a form that disappears when the thread that created it expires, which makes sense because the form must not be attached properly to the GUI thread. I do use Invoke to Show the form, but that wont make a difference because It's not actually Invoking the GUI thread, but it's invoking the thread it was created on.. the non-GUI thread.
 
I think that, in general, what is considered proper is to create helper methods on a form that perform whatever UI operations you need to do from a secondary thread. You can call the Invoke method on said form (which should have been created under the GUI thread) to execute the code on the main thread.

This would include creating new forms. If you want to create a new form from a second thread, you should have that second thread invoke code on the main thread to do so. For instance, the app's main form can have a method to create a form and return a reference to it. The secondary thread invokes this method via the main form's Invoke method. Now suppose this secondary thread also needs to update the newly created form. The newly created form should expose methods that can be invoked from the secondary thread using the newly created form's Invoke method.
 
Back
Top