0

I have the following code that checks if my application is already running:

    if (Process.GetProcessesByName(Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().Location)).Count() > 1)
    {
        MessageBox.Show("Multiple instances!");
        Process.GetCurrentProcess().Kill();
    }

Now, on some point in my app I use

Application.Restart();

Sporadically, when the app is restarting I get the message from the above IF statement.

How could I prevent this of happening? Can I assure somehow that all my processes will be closed before Application.Restart() or can I somehow make sure that when the app is starting after Application.Restart() it will NOT see the previous processes running so that it will NOT throw me the message box?

Marko
  • 407
  • 1
  • 7
  • 19
  • Is it a problem if you would kill after say... X(300?) ms? The flow would be: `if 2 instances` > `wait X ms` > `still 2 instances?` > `kill`. If the restart only has the old instance active for a small while (under X ms) this would fix it – EpicKip Oct 23 '17 at 11:38
  • I see. But how do I know how many ms does Application.Restart() requires the old process? – Marko Oct 23 '17 at 11:42
  • 2
    Why do you use [`Restart()`](https://msdn.microsoft.com/en-us/library/system.windows.forms.application.restart(v=vs.110).aspx)? What is a scenario? There are more [possibilities](https://stackoverflow.com/q/19147/1997232) as well to check for multiple instances. – Sinatr Oct 23 '17 at 11:43
  • The sporadic character may suggest that indeed, sometimes the Restart acts faster and catches the old process, sometimes it does not. – Marko Oct 23 '17 at 11:43
  • you could measure the amount of time 2 open instances by putting a stopwatch there (and no kill) and just restarting manually – EpicKip Oct 23 '17 at 11:44
  • Rather than searching processes (and dealing badly with naming conflicts) can you not arrange some form of IPC setup where the "owning" process acts as a server? That way the incoming and outgoing processes can communicate. – Damien_The_Unbeliever Oct 23 '17 at 11:44
  • The application checks the user licence on startup and if some user wants to change its licence the old one is removed and the app restarts and requests the new key. This is why I use Application.Restart(). – Marko Oct 23 '17 at 11:45
  • Interesting story with the mutex, very elegant approach. Thank you for the link, Sinatr! – Marko Oct 23 '17 at 11:59

1 Answers1

1

You can use a flag when is true you don't check processes

public static Restarting = false;

....

if( !Restarting && Process.GetProcessesByName(Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().Location)).Count() > 1)
                {
                    MessageBox.Show("Multiple instances!");
                    Process.GetCurrentProcess().Kill();
                }

....

Restarting = true;
Application.Restart();
Amir
  • 782
  • 1
  • 9
  • 19
  • Hmm. This could be a solution, indeed. If the restart is intended and somehow there will be multiple processes for a couple of ms, it shouldn't alert the user. Thank you for your suggestion, mirtiger! – Marko Oct 23 '17 at 11:51