Alan392
Alan392

Reputation: 685

Restart application and check if is already running

In my application I have wrote this sub for restart my programm. This code is ok.

Sub Restart
     'For restart application many times
     Application.Current.Shutdown()
     System.Diagnostics.Process.Start(Application.ResourceAssembly.Location)
End Sub

After a few days, I've added this code to check the program is already running. (and this code is ok)

Sub IsRunning
   'Check if it's already running
   If (Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).Length > 1) Then
         Msg = "Application is running"
         MessageBox.Show(Msg, "Attention", MessageBoxButton.OK, MessageBoxImage.Error)
         Application.Current.Shutdown()
  End If
End Sub

Now, what's the issue ? If I try to restart application, I recive the message "Application is running" and I can't do that !

How solve this issue ?

Many thanks

Upvotes: 0

Views: 347

Answers (1)

Simon
Simon

Reputation: 300

At the start of the application, register a global mutex. And when the application it shutting down, release the mutex.

You will then be able to check if your application is already running by checking if you can obtain the mutex.

This might be of some inspiration: Run single instance of an application using Mutex

And this MSDN link: https://msdn.microsoft.com/en-us/library/system.threading.mutex(v=vs.110).aspx

Hope that helps

Upvotes: 1

Related Questions