C# Forms programming -- have trouble

Flyback

Golden Member
Sep 20, 2006
1,303
0
0
I have a main window that, among other things, opens a thread.

In that thread, it opens another window and then it ends (end of the DoWork method).
To open the window I use:
MyCustomForm mcf = new MyCustomForm();
mcf.Show();

Basically it flashes open and then closed. so I tried something else instead:
mcf.Show(parent);

Where parent is the topmost form (my form of the application).

This doesn't work either. If I use Thread.Sleep() before the thread ends, I can see it opens and then closes/dies with the thread.

How can I get the window to stay open even tho the thread that creates it dies?? Is there any method on the parent form I could call to add it as its child so it doesn't go out of scope (which I'm assuming it does--never done forms programming before)

thanks a billion.
 

Flyback

Golden Member
Sep 20, 2006
1,303
0
0
Just tried:
parent.AddOwnedForm(mcf);

that doesn't work either. It still appears then disappears just as fast when the thread dies. Any ideas?
 

Tencntraze

Senior member
Aug 7, 2006
570
0
0
You shouldn't be doing any UI work on anything but the main thread. If you want to open a new window from a second thread, invoke the call back to the main UI thread first. I can't say that this is directly causing your problem, but it's a programming practice to stick with in .NET.
 

Flyback

Golden Member
Sep 20, 2006
1,303
0
0
Originally posted by: Tencntraze
You shouldn't be doing any UI work on anything but the main thread. If you want to open a new window from a second thread, invoke the call back to the main UI thread first. I can't say that this is directly causing your problem, but it's a programming practice to stick with in .NET.

Thanks.

How would I go about this? To invoke the callback? I tried writing a method in the main form and then call it from the thread, but for some reason it goes out of scope, too.

Edit: I mean to say, it goes to the main form and invokes the method fine, opening the form.. but it dies the same...

Does the .NET runtime know, looking at the stack, that it was called from the thread still? I am grasping at straws and need to get this little thing to work :/