One application with multiple forms
-
What I am trying to do is to first start a form, which gives the user a choice between starting the application as a server or as a client. When the user has made this choice, the form should terminate. Now I wish to start another form, which provides another user interface. Like this: static void Main() { ServerOrClientForm serverOrClient = new ServerOrClientForm(); Application.Run(serverOrClient); serverOrClient.InputSelection(); UserInterface ui = new UserInterface(); Application.Run(ui); // pass information to and from ui obj = ui.GetInfo(); // ... ui.SetInfo(obj); } The first form works well, but the other one (ui) does not show up at all. What am I doing wrong?
-
What I am trying to do is to first start a form, which gives the user a choice between starting the application as a server or as a client. When the user has made this choice, the form should terminate. Now I wish to start another form, which provides another user interface. Like this: static void Main() { ServerOrClientForm serverOrClient = new ServerOrClientForm(); Application.Run(serverOrClient); serverOrClient.InputSelection(); UserInterface ui = new UserInterface(); Application.Run(ui); // pass information to and from ui obj = ui.GetInfo(); // ... ui.SetInfo(obj); } The first form works well, but the other one (ui) does not show up at all. What am I doing wrong?
You are not doing anything wrong, this is a bug in the framework. When Application.Run finishes, a bit is set indicating that the message pump on the thread has shutdown. This bit is sticky, and any subsequent calls to Application.Run see this bit set and immediately terminate. However, this should workaround your problem: static void Main() { ServerOrClientForm serverOrClient = new ServerOrClientForm(); Application.Run(serverOrClient); serverOrClient.ShowDialog(); serverOrClient.InputSelection(); UserInterface ui = new UserInterface(); Application.Run(ui); // pass information to and from ui obj = ui.GetInfo(); // ... ui.SetInfo(obj); }
-
You are not doing anything wrong, this is a bug in the framework. When Application.Run finishes, a bit is set indicating that the message pump on the thread has shutdown. This bit is sticky, and any subsequent calls to Application.Run see this bit set and immediately terminate. However, this should workaround your problem: static void Main() { ServerOrClientForm serverOrClient = new ServerOrClientForm(); Application.Run(serverOrClient); serverOrClient.ShowDialog(); serverOrClient.InputSelection(); UserInterface ui = new UserInterface(); Application.Run(ui); // pass information to and from ui obj = ui.GetInfo(); // ... ui.SetInfo(obj); }
Thank you very much for your reply! Regrettably, your solution does not work for me. It still behaves as it did before. I have changed the code into the following: static void Main() { ServerOrClientForm serverOrClient = new ServerOrClientForm(); serverOrClient.ShowDialog(); serverOrClient.InputSelection(); System.Threading.Thread.Sleep(1000); UserInterface ui = new UserInterface(); ui.ShowDialog(); } I can clearly see the second form flashing by after the 1 second delay. The first form, however, works fine.
-
Thank you very much for your reply! Regrettably, your solution does not work for me. It still behaves as it did before. I have changed the code into the following: static void Main() { ServerOrClientForm serverOrClient = new ServerOrClientForm(); serverOrClient.ShowDialog(); serverOrClient.InputSelection(); System.Threading.Thread.Sleep(1000); UserInterface ui = new UserInterface(); ui.ShowDialog(); } I can clearly see the second form flashing by after the 1 second delay. The first form, however, works fine.
Please look at the code I had posted. For the "UserInterface" class, do not use ShowDialog(), but rather Application.Run(ui).
-
Please look at the code I had posted. For the "UserInterface" class, do not use ShowDialog(), but rather Application.Run(ui).
-
I have tried that, and it did not work either. That's why I tried to use ShowDialog() on both of them.
What does the function "InputSelection" do? In a simple test application, this works fine for me: static void Main() { Form1 form1 = new Form1(); form1.ShowDialog(); Application.Run(new Form2()); }
-
What does the function "InputSelection" do? In a simple test application, this works fine for me: static void Main() { Form1 form1 = new Form1(); form1.ShowDialog(); Application.Run(new Form2()); }
I found the source to my troubles. When the user presses the OK button in my ServerOrClientForm its clicked handler issued a call to Application.Exit(). Now I call this.Close() instead, which obviously was the better choice. But I am not sure why. Do you know why? InputSelection is just a way of blocking the caller until ServerOrClientForm has completed its task (merely a busy-wait loop in which I am checking a 'done' flag and if it's not set, put the thread to sleep for a little while). The thing is, I want the form to produce a result and then feed that result to anyone that call theForm.GetResult, and at the same time block the caller until the result is ready. Can you come up with a good solution? Thank you very much for helping me with my original problem.
-
I found the source to my troubles. When the user presses the OK button in my ServerOrClientForm its clicked handler issued a call to Application.Exit(). Now I call this.Close() instead, which obviously was the better choice. But I am not sure why. Do you know why? InputSelection is just a way of blocking the caller until ServerOrClientForm has completed its task (merely a busy-wait loop in which I am checking a 'done' flag and if it's not set, put the thread to sleep for a little while). The thing is, I want the form to produce a result and then feed that result to anyone that call theForm.GetResult, and at the same time block the caller until the result is ready. Can you come up with a good solution? Thank you very much for helping me with my original problem.
Application.Exit
posts theWM_QUIT
message which signals to windows to shut down (note, that's "windows", not "Windows"). The application will ultimately exit. It's typically not a good idea to callApplication.Exit
. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles]