473,387 Members | 1,687 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Only one instance of an application -- Mutex vs GetProcessesByName

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
Nov 21 '05 #1
4 4172
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
---------------
"Samuel R. Neff" <sr********@spampleasespeakeasy.net> wrote in message
news:21********************************@4ax.com...
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

Nov 21 '05 #2
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

Nov 21 '05 #3
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
---------------
"Samuel R. Neff" <sr********@spampleasespeakeasy.net> wrote in message
news:21********************************@4ax.com...
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

Nov 21 '05 #4
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

Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: serge calderara | last post by:
Dear all, I have a vb.net application which start with a sub main procedure. inside this sub main procedure I create a and instance from an assembly x like as follow: sub main() ..... ...
6
by: Dmitry Karneyev | last post by:
Hi! I guess this question have been asked a lot of times, but please be tolerant and if you have any ideas share it. The question is: how to make availibale only one instance of application and...
8
by: Li Pang | last post by:
Hi I want to run my app only one instance, i.e. only one session is allowed to display Thank
9
by: T Cordon | last post by:
How to determine if another instance of the application is running so that ir doesn't run multiple instances? Thanks
3
by: wg | last post by:
I am attempting to prevent a second instance of my application from starting. I have the following code at the beginning of Form_Load If...
6
by: Xenio | last post by:
Hi, I'd like to make sure that only one instance of my app is running per User Session. In addition this has to work with user rights and in a Windows 2k/2k3 and Citrix Terminalserver...
3
by: Mark Jerde | last post by:
VS 2005. When I google "CSharp single instance form" the returned pages usually use a Mutex or the VB.NET runtime library....
4
by: melon | last post by:
Let's say I have a program.exe file. When I run it, one instance of it will be created. If I run it again, then another instance will be created. Question is, is it possible for instance #2 to...
20
by: Boki Digtal | last post by:
Hi All, How to limit the c# program can only run one instance at the same time ? Thank you! Best regards, Boki.
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.