Only one instance of an application -- Mutex vs GetProcessesByName

S

Samuel R. Neff

Most of the time what I've read is that to keep only one instance of
an app running one should use a Mutex. However, we have an existing
app that uses GetProcessByName() to ensure only one instance is
running.

What are the advantages of a Mutex besides protecting against name
collision (which I imagine can be high since GetProcessesByName uses
the short name of the executable without the path)?

Thanks,

Sam


Shared Sub Main(ByVal CmdArgs() As String)
Dim ThisProcess As Process = Process.GetCurrentProcess()
Dim AllProcesses As Process() =
Process.GetProcessesByName(ThisProcess.ProcessName)
If (AllProcesses.Length > 1) Then
MessageBox.Show(ThisProcess.ProcessName + " is already
running.", ThisProcess.ProcessName, MessageBoxButtons.OK,
MessageBoxIcon.Error)
Else
Application.Run(New frmMain)
End If
End Sub
 
K

Ken Tucker [MVP]

Hi,

Mutex guarantees that there is only one app running.
GetprocessByNane will allow 2 apps to run if someone changes the name.
Other disadvantage is that if you have 2 separate applications with the same
name it wont allow them to run at the same time.

Ken
---------------
Most of the time what I've read is that to keep only one instance of
an app running one should use a Mutex. However, we have an existing
app that uses GetProcessByName() to ensure only one instance is
running.

What are the advantages of a Mutex besides protecting against name
collision (which I imagine can be high since GetProcessesByName uses
the short name of the executable without the path)?

Thanks,

Sam


Shared Sub Main(ByVal CmdArgs() As String)
Dim ThisProcess As Process = Process.GetCurrentProcess()
Dim AllProcesses As Process() =
Process.GetProcessesByName(ThisProcess.ProcessName)
If (AllProcesses.Length > 1) Then
MessageBox.Show(ThisProcess.ProcessName + " is already
running.", ThisProcess.ProcessName, MessageBoxButtons.OK,
MessageBoxIcon.Error)
Else
Application.Run(New frmMain)
End If
End Sub
 
T

Tom Spink

Mutexes (or Muticies??? ;-)) also can be swindled to be present in the
"Global Namespace", which means it guarantees only one app running on
the *entire* system.... i.e. otherwise there could be an instance of
the app running in seperate server sessions, in a Windows Terminal
Server, or XP Fast-User switching environment.

-- T
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top