C# restart application (WinForms)

Started by
4 comments, last by Khaos 20 years ago
I''ve got a little dialog box where a user enters a word and presses OK. It then displays some information relating to the word. When they press OK after that, the application exits (I do have a call to Application.Exit()) However, instead of this, I want to keep the application going until the user clicks the X (or similar). How can I restart the application so it shows a fresh dialog box with no entry listed? I''ve tried recalling Main(), I''ve tried Application.Run() again. I get runtime errors and exceptions. How can I easily do it?
Advertisement
In all GUIs, an OK button is expected to dismiss the dialog. So, you should rename that button to something more descriptive like "Update" or whatever it actually does. Otherwise, your app will confuse users, and users don''t like that.

The answer to your question depends upon where you''re displaying the information. If it''s somewhere in the same dialog (I assume that it is), then you simply make the event handler for your button update the information. That''s it.
I just want clicking OK to restart the same dialog, for my purposes only. Simply so I don''t have to keep reopening the EXE. It tells me I can''t call to Main or Run() because I can''t restart on the same thread. I need to rerun the application every time.
Okay then... surely this has been done before.
You need to start the app from a separate class (where the Main() procedure lives). Loop inside Main() and kick off the dialog with Application.Run() calls over and over.

There are other ways, but this would probably be the fastest and easiest.
To show what McGuireV10 means in practice:
public class Apl{  public static bool RestartApp   {    get { return s_restart; }  }  public static void Main()  {    do    {      Application.Run(new MyDialog());    } while( RestartApp );  }  private static bool s_restart = true; } 


So, whenever you want to stop the application from restarting, simply call Apl.RestartApp = false fromout the dialog.

Regars,
Andre (VizOne) Loker
Andre Loker | Personal blog on .NET

This topic is closed to new replies.

Advertisement