killing a process owning a system mutex

T

tony.newsgrps

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.
 
T

tony.newsgrps

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
 

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