Archived

This topic is now archived and is closed to further replies.

Khaos

C# restart application (WinForms)

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites