How to start a form from another thread
-
Hi, I am writing a forms based application, which has been working fine, until I moved my project to another computer with the same version of Visual studio. Now my initial form does not show up at all. This is what I have done: I have a console application that starts three forms, one after the other. Each form implements two methods invloved in starting the form in its own separate thread. class UIForm : .... { Thread formThread = null; public void OpenForm() { // formThread is a private member of the current form formThread = new Thread( new ThreadStart( this.ShowForm ) ); formThread.Start(); } private void ShowForm() { Application.Run( this ); } . . . } The console application then starts the form like this: UIForm theForm = new UIForm(); theForm.OpenForm(); This worked fine until I moved my project to another machine. Any help, comments or advise is much appreciated!! Thanks!
-
Hi, I am writing a forms based application, which has been working fine, until I moved my project to another computer with the same version of Visual studio. Now my initial form does not show up at all. This is what I have done: I have a console application that starts three forms, one after the other. Each form implements two methods invloved in starting the form in its own separate thread. class UIForm : .... { Thread formThread = null; public void OpenForm() { // formThread is a private member of the current form formThread = new Thread( new ThreadStart( this.ShowForm ) ); formThread.Start(); } private void ShowForm() { Application.Run( this ); } . . . } The console application then starts the form like this: UIForm theForm = new UIForm(); theForm.OpenForm(); This worked fine until I moved my project to another machine. Any help, comments or advise is much appreciated!! Thanks!
Given my limited understanding of all things .Net, take what I say with a grain of salt. The thread that creates the window must be the one that updates the window. The thread that creates the window must also participate in a message loop. Although you may get away with violating these rules some of the time, it is guarenteed to bite you in the butt sometime in the future. Unless you are doing something really special, I would have one thread handle all of the UI and have the other threads doing non-UI stuff. Use Form.Invoke() to handle communication between the threads.
-
Given my limited understanding of all things .Net, take what I say with a grain of salt. The thread that creates the window must be the one that updates the window. The thread that creates the window must also participate in a message loop. Although you may get away with violating these rules some of the time, it is guarenteed to bite you in the butt sometime in the future. Unless you are doing something really special, I would have one thread handle all of the UI and have the other threads doing non-UI stuff. Use Form.Invoke() to handle communication between the threads.
-
I agree to what you say, but I don't see how I violate any of the rules in the startup phase. The form is started in its own thread, which should then be bound to the message loop. Or have I misunderstood things?
You created the window using one thread then executed another to make it visible. This violates the first principle I stated above. Try creating the window with the new thread.
-
You created the window using one thread then executed another to make it visible. This violates the first principle I stated above. Try creating the window with the new thread.