Restart Application & Supress multiple instances

I have application that updates itself and after that restarts. My problem is that when i try to start application i check if there is started instance of my application and stop second instance. So now when i restart application, probably previous is not closed yet, and another one is started which fails because there is already started instance.

My tries are:

with Process.Start("my app path") and Application.ExitThread() or Application.Exit()
with Application.Restart and Application.ExitThread() or Application.Exit()

I try to put this restarting code after Application.Run(new myMainForm()), also in Form Closing, Form Closed event of main form, but nothing works.

The weird thing is that when i test this from VS and start App in Release or Debug mode it works, but when i start manually from the folder don't work.

My code for checking previous instance is in program.cs before Application.Run .




Answer this question

Restart Application & Supress multiple instances

  • Bill API

    Thanks for great idea, it help a lot.

    Here is what i discovered in the way :
    1. I first just did simple change
    ApplicationContext appContext = new ApplicationContext(new myMainForm()):
    Application.Run(appContext);

    didn't worked.

    2. Created new class inherited form ApplicationContext and on my overload on OnMainFormClosed method i checked if restart is required and execute Application.Restart

    didn't worked.

    3. I used this sample http://www.codeproject.com/csharp/ApplicationContextSplash.asp that do same but with two forms splash and main form as part of inherited ApplicationContext class and restart app if was required after main form was closed.

    didn't worked

    4. Continued from step 3, I move update code from main form to splash form and start update from there and if restart is required i start application restart when splash form in ApplicationContext is closed on same OnMainFormClosed method overload. Which means when i start update, Main form is not opened, just splash form, and then i restart application.

    it worked, finally.

    Probably my form is somehow two slow when closing and maybe that is the problem, who knows.

    What was the again weird thing is that all procedures, my first one without any ApplicationContext implementation, just simple Application.Restart from main form, also with all this tries i have now mentioned, are working when i start application from Visual Studio in Release mode. But when i test that starting application as double click on exe file in folder it never worked except with last change.
    At the end the weirdest thing of all is that last change don't work when i start debug exe with double click of exe file in Debug Folder, but starting exe from Release working.



  • DarrellMerryweather

    Hello All.

    boban.s:

    Try running your app with Application.Run(ApplicationContext). That way, the message loop is not tied to the GUI. Then, you can close all the open forms, do any updating, and re-start the GUI without having to stop the thread execution.

    HTH.



  • Restart Application & Supress multiple instances