1

I would like to sequentially show two separate forms. Here is the Main() function.

  [STAThread]
  static void Main() {
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    Application.Run(new AccountForm());

    Application.Run(new MainForm());
  }

When I run this, the AccountForm is displayed normally, and then the MainForm is created and the Form_Load event is called, but the MainForm is never displayed and the program exits immediately.

If the Run(AccountForm) line is commented, the MainForm DOES display properly.

Apparently, when the AccountForm is closed using Application.Exit(), this is remembered by the Application object and it does not start another message loop on the next call to Run.

Any ideas how to make this work?


Added information, solved!

jmcilhinney made a simple suggestion to close the form instead of calling Application.Exit(). This worked perfectly. The code is simple and clean. Thank you!


For instance, is there some way to reset the state of the "Application" object?

Written in C# under Windows.

In actual use, there will be criteria on whether to run each piece. Sometimes, only the first will be run, sometimes only the second, and sometimes both. So this syntax is much simpler than other options I have considered.

In response to this being a duplicate: The other user wants his forms to be displayed simultaneously and independently. His situation can be handled with the suggestions give in that post. Those solutions do not work for the forms being displayed sequentially. (Or at least would not work cleanly and easily.) I really want to avoid building some complex logic that knows about both forms and the state of each and displays and destroys them as needed. My proposed sequential operation is dirt simple and very, very clean. It just looks like the developers of C# may have never considered anyone would do this.

Mark T
  • 3,464
  • 5
  • 31
  • 45
  • Explanation added at end of question as to why this is NOT a duplicate. – Mark T Mar 15 '16 at 02:38
  • 1
    The immediately obvious answer seems to be that you close the first form by calling its `Close` method rather than `Application.Exit`. Is that possible? – jmcilhinney Mar 15 '16 at 02:39
  • As the name suggests, you call `Application.Exit` when you want to exit the application. You apparently don't want to exit the application, so you shouldn't be calling `Application.Exit`. – jmcilhinney Mar 15 '16 at 02:56
  • To: jmcilhinney -- It never occurred to me that closing the form would cause an exit from Application.Run(). But you are correct. It works perfectly with that simple change. If you would post it as an answer rather than just a comment, I will mark it as the accepted answer. Thank you for your assistance! – Mark T Mar 15 '16 at 04:35
  • I don't think an answer can be added when a question has been marked as a duplicate. FYI, I had a look at the implementation of `Application.Exit` and, as I already new, it went through all open forms and closed them. What I wasn't specifically aware of was what else it did, which involved exiting looping through `ThreadContext`s and exiting/disposing them. Basically, the application scaffolding is already gone so your second form can't load within it. By just closing the first form, all of that is avoided. – jmcilhinney Mar 15 '16 at 04:40
  • It is unfortunate if a question can not be removed from the duplicate list. Since this question has a valid answer which is not addressed in the supposed duplicate, it is obviously not a duplicate. – Mark T Mar 15 '16 at 04:50

0 Answers0