The number of machines running this isn't proof enough. If you have a
minimal number of processes on each machine, it doesn't matter how many
machines are running the app.
In the end, the process approach can take up to O(n) time, where n is
the number of processes on the machine. The mutex approach is always an
O(1) operation. Always guaranteed to be more efficient.
The Mutex approach is easy:
public static void Main()
{
// Created?
bool created = false;
// Create the mutex.
using (Mutex mutex = new Mutex(true, "MyMutex", out created))
{
// If this is not created, get out.
if (!created)
{
// Get out.
return;
}
// Run your app code here.
}
}
If you want your running app to handle command line parameters that are
passed to new instances that are run, then you should look at the
WindowsFormsApplicationBase class in the Microsoft.VisualBasic namespace, as
it handles this for you.

Signature
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com
> Can you supply the sample code for the mutex approach?
>
[quoted text clipped - 6 lines]
>>
>> Don't do it - go with the mutex approach, which is much more efficient...
Dan Tallent - 20 Sep 2006 12:55 GMT
Thanks for the sample.
When I was referring to proof, I was talking about the program running on TS
or XP.
Someone on the thread said "Take into account that this doesn't work in
Terminal Services." This is not true.
I will most likely change my code to the Mutex method, but the other method
does work.
I agree the Mutex is more efficient, but if your talking about checking the
running processes,
ONLY when attempting to run the program, its unlikely this code will fire
very often. My example was when a different user
was logging into Windows. I wanted to suppress running the program again
if another user already started it. This was especially important on
a terminal services environment.
Thanks Again
Dan
> The number of machines running this isn't proof enough. If you have a
> minimal number of processes on each machine, it doesn't matter how many
[quoted text clipped - 41 lines]
>>> Don't do it - go with the mutex approach, which is much more
>>> efficient...
A1b2c3d4@community.nospam - 13 Dec 2007 21:44 GMT
Hi,
Windows XP Pro SP2, Workgroup
The mutex approach doesn't always work for me. It works in the following
scenarios:
1. User A attempts to launch a second instance of the process. User A
launched the first instance (ie same user, same workstation)
2. User A attempts to launch a second instance of the process. User B
launched the first instance. Both instances are running in the same
workstation (e.g. RunAs was used to change the owner of one of the processes).
It doesn't seem to work when user A attempts to launch a second instance of
the process where user B launched the first instance in a different
workstation (e.g. launch the process, switch to a different user (and
therefore a different workstation) and launch a second instance of the
process).
I always thought that named mutexes provided exclusivity over the entire
machine...
I've already tried using the Mutex constructor that takes a Security object,
granting full permission to everyone, but it made no difference. When I
examine the mutex names using SysInternals' Process Explorer, the name that I
gave the mutex is qualified and the qualifications are different on the two
processes.
Should a mutex work in this scenario?
Dave Razzetti
> The number of machines running this isn't proof enough. If you have a
> minimal number of processes on each machine, it doesn't matter how many
[quoted text clipped - 40 lines]
> >>
> >> Don't do it - go with the mutex approach, which is much more efficient...
Willy Denoyette [MVP] - 13 Dec 2007 21:52 GMT
> Hi,
>
[quoted text clipped - 20 lines]
> I always thought that named mutexes provided exclusivity over the entire
> machine...
Only if the mutex is of a global type like....
"Global\MyMutex"
Willy.
A1b2c3d4@community.nospam - 13 Dec 2007 22:12 GMT
Thanks Willy,
I'd just found an MSDN article that said exactly that!
Isn't it funny how a minute or two after you post a question on here, you
stumble across the solution somewhere else? Its happened a couple of times
now...
Dave Razzetti
> > Hi,
> >
[quoted text clipped - 25 lines]
>
> Willy.
> Can you supply the sample code for the mutex approach?
1) Launch your Internet browser (IE, FireFox, Netscape, Opera Mozilla etc)
2) Navigate to http://www.google.com
3) Enter the text below in the box:
"C#" "global mutex" prevent twice
4) Hit the button
> BTW, this method works fine for TS and XP. I have 15 machines in my
> building alone as proof.
Of course it does - it's just not particularly efficient...