473,385 Members | 1,597 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,385 software developers and data experts.

killing a process owning a system mutex

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
2 6184
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: John E. Hadstate | last post by:
Please forgive the heavy cross-posting and feel free to delete inappropriate groups when you reply. I am really at a loss and hope someone has an answer for my problems. Environment: Redhat...
3
by: Andreas | last post by:
Hi, I just created my first vb.net pocket pc application - that consists of a main dialog and an additional dialog that is defined globally like this: Dim x as New MyTestDialog As the...
1
by: Raj | last post by:
what I want to do is start a process in a windows system (2000/XP) at the system startup without the user having to login. I know the best option for this is to write the app as a service. I tried...
2
by: Martin Maat | last post by:
Hi. I want to use the same mutex in different classes (web pages in an ASP.NET application). In global.asax.cs, the class that starts up first, I create a Mutex like this: static private...
3
by: Drasko | last post by:
Using SystemEvent.TimeChanged I can find that system time has been changed, but don't know how to find wich process initiated this. Any suggestions? Thanks Drasko
5
by: Benzi Eilon | last post by:
I have written a C# application which should run as a Windows Service. I must avoid having multiple instances of the application on one machine so I inserted the following code at the beginning of...
2
by: cj | last post by:
I take it a mutex is like a synclock but visible outside the program?
11
by: Lamont Sanford | last post by:
Given an object of type Mutex, what method or property should be called to determine if it is currently owned? I could call WaitOne(0,false) -- and if the return value is false, I could deduce...
1
by: dotnetnovice | last post by:
Hi... I want some help in registry editing Actually i am using this code namespace ConsoleApplication2 { class Win_start { static void Main(string args) {
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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.