Open Form in Different Thread

grip003

Regular
Joined
Sep 2, 2004
Messages
89
Location
North Carolina
I am using VS2003, and I am trying to open a form in a new thread. What I have done is started a new thread from the main one, and in that thread I create a new form. The new form is used to display some loading results from the main form, but when I show the new form I just get an hourglass and I can't press the cancel button. Here is the simple idea:

Show the main form.
Start a new thread in the main form for loading.
In the new thread, create a 2nd form to display some loading results.
While the 2nd form is shown, I want the ability to cancel the loading operation.

Does this seem possible?
 
I believe that all GUI components must be run on the same thread. Windows sends messages only to the thread that is running the message pump and these messages are all dispatched on that thread. It might be possible to call Application.Start(someForm) on your second thread, but I'm not too sure that windows would allow an application to have two message pumps.
 
The GUI should remain on a single thread. The loading process is what should be on the 2nd thread. You will have to create some events on the loading thread with event handlers in your GUI code that check for InvokeRequired and use Invoke to switch to the GUI thread to update the forms.

Here is a thread describing how I was doing it in .NET 1.1. I know .NET 2.0 has a BackgroundWorkerThread, which might make things easier.

http://www.xtremedotnettalk.com/showthread.php?t=94252&highlight=thread

Todd
 
Thanks tfowler, that is the way I have been doing this in the past, I was just trying to come up with a slick form that I could use in a general way to perform any loading I wanted to do. According to marble_eater, that is just not possible. Oh well, thanks anyway.
 
Back
Top