.NET Check if another app is running
I have an application that provides basic services for a number of other applications.
When another of these applications starts, I want to check if the service application is started and if not terminated.
What's the best way to check for another app? I think I should use a global mutex in a service application and check for its existence in other applications. Is it correct?
a source to share
The global approach to mutecs is good and is used by many applications.
For example, Windows Media Player does this.
Similar questions:
Using Mutex to prevent multiple instances of the same program from running securely?
a source to share
bool IsApplicationAlreadyRunning()
{
string proc=Process.GetCurrentProcess().ProcessName;
Process[] processes=Process.GetProcessesByName(proc);
if (processes.Length > 1)
return true;
else
return false;
}
Source: http://kseesharp.blogspot.com/2008/11/c-check-if-application-is-already.html
a source to share
Yes, just be careful how you name the mutex. There are specific naming rules for these - this one has some details
a source to share
I would say to examine the method you use to communicate with the service application and see what the communication error is. If you try to contact the service application at any time and it works, it looks like you want high dependency error behavior. If you have a crash on the first call, it seems like you want to quit the application; but you also need to worry about what happens if your message fails at a later point.
a source to share
try this (VB.NET example):
If PrevInstance() Then
MsgBox("[MyApplicationName] application is already running.")
Exit Sub
End If
Function PrevInstance() As Boolean
If UBound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)) > 0 Then
'MsgBox("Application is still running", MsgBoxStyle.Information)
Return True
Else
Return False
End If
End Function
a source to share