Basilf
Basilf

Reputation: 461

Winform app restart not working when deployed

I need to restart my application once an unhandled exception happens. It's a .NET WinForm app.

  [STAThread]
    static void Main()
    {
        try
        {
            Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());

        }
        catch (SystemException ex)
        {
            Log.AddEntry(ex.Message);
        }


    }
    static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
    {
        Console.WriteLine(e.Exception.Message);
        Application.Exit();
        System.Diagnostics.Process.Start(Environment.CurrentDirectory+"\\Taskman.exe");
    }

I'm throwing an exception intentionally using a button click. When I test the release version on my machine it closes, then restarts normally. But when I deploy it on the production machine, it doesn't.

Any idea?

Thanks.

Upvotes: 0

Views: 59

Answers (2)

Handbag Crab
Handbag Crab

Reputation: 1538

Call this:

Application.Restart();

instead of Application.Exit();

Upvotes: 0

mm8
mm8

Reputation: 169320

You should start the new process before you exit the current one, and make sure that you pass the correct path to the Process.Start method:

static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
    Console.WriteLine(e.Exception.Message);
    System.Diagnostics.Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location);
    Application.Exit();
}

Upvotes: 2

Related Questions