By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,152 Members | 1,647 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,152 IT Pros & Developers. It's quick & easy.

killing a process owning a system mutex

P: n/a
Hi there,

I'm trying to understand the impact of killing a process that owns a
system mutex (used to ensure there is only 1 instance of my program
running)

Here is my code pretty much:
try
{
mutex=new System.Threading.Mutex( true, mutexName, out createdNew)
)
if (!createdNew)
{
Console.Error.WriteLine("Another Instance is already running");
return (int) AppErrorCode.AlreadyRunning;
}
}
catch
{
.... Some code ...
}
if(createdNew)
{
mutex.ReleaseMutex();
}
1st question: Should I always release the mutex or only in the instance
which created it.
2nd question: What happens if my program hangs and needs to be killed
(via the task manager).
The next time the program is run, will createdNew be equal to true or
false (because it is reusing the mutex that was created by the killed
instance)
3rd question: should I use the using( mutex = new Mutex ) instead?
If so, then what happens in the instance that created the mutex? In the
second instance that attempted to access the mutex?

Bonus question: any better suggestion to make sure that only 1 instance
of the process can run at a given moment?

Thank for the help.

Jul 26 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
The mutex is held by the process .. when there are no processes accessing
the mutex the OS automatically removes the mutex. It is in general a good
idea to explicitly release the mutex but it should happen anyways.

http://www.codeproject.com/csharp/cssingprocess.asp includes a pre-done
compenent that will do this for you.
Cheers,

Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung

<to***********@gmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
Hi there,

I'm trying to understand the impact of killing a process that owns a
system mutex (used to ensure there is only 1 instance of my program
running)

Here is my code pretty much:
try
{
mutex=new System.Threading.Mutex( true, mutexName, out createdNew)
)
if (!createdNew)
{
Console.Error.WriteLine("Another Instance is already running");
return (int) AppErrorCode.AlreadyRunning;
}
}
catch
{
... Some code ...
}
if(createdNew)
{
mutex.ReleaseMutex();
}
1st question: Should I always release the mutex or only in the instance
which created it.
2nd question: What happens if my program hangs and needs to be killed
(via the task manager).
The next time the program is run, will createdNew be equal to true or
false (because it is reusing the mutex that was created by the killed
instance)
3rd question: should I use the using( mutex = new Mutex ) instead?
If so, then what happens in the instance that created the mutex? In the
second instance that attempted to access the mutex?

Bonus question: any better suggestion to make sure that only 1 instance
of the process can run at a given moment?

Thank for the help.

Jul 26 '06 #2

P: n/a
Greg,

thank you for your reply but unfortunately, the mutex is not held by
the process.
It is a named system mutex (see the msdn documentation), which means
that it doesn't belong to the process itself, thus my confusion.
Also, if I right click on the process in the task manager and select
kill, does the .Net garbage collection still occur?

Tony

Greg Young wrote:
The mutex is held by the process .. when there are no processes accessing
the mutex the OS automatically removes the mutex. It is in general a good
idea to explicitly release the mutex but it should happen anyways.

http://www.codeproject.com/csharp/cssingprocess.asp includes a pre-done
compenent that will do this for you.
Cheers,

Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung

<to***********@gmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
Hi there,

I'm trying to understand the impact of killing a process that owns a
system mutex (used to ensure there is only 1 instance of my program
running)

Here is my code pretty much:
try
{
mutex=new System.Threading.Mutex( true, mutexName, out createdNew)
)
if (!createdNew)
{
Console.Error.WriteLine("Another Instance is already running");
return (int) AppErrorCode.AlreadyRunning;
}
}
catch
{
... Some code ...
}
if(createdNew)
{
mutex.ReleaseMutex();
}
1st question: Should I always release the mutex or only in the instance
which created it.
2nd question: What happens if my program hangs and needs to be killed
(via the task manager).
The next time the program is run, will createdNew be equal to true or
false (because it is reusing the mutex that was created by the killed
instance)
3rd question: should I use the using( mutex = new Mutex ) instead?
If so, then what happens in the instance that created the mutex? In the
second instance that attempted to access the mutex?

Bonus question: any better suggestion to make sure that only 1 instance
of the process can run at a given moment?

Thank for the help.
Jul 27 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.