473,385 Members | 1,356 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.

Mutex won't release after exception.

I want a (global) mutex which prevents a section of my code from being run by more than one process (or user) at a time. I have something that works, EXCEPT when there is an exception of some kind. It is possible for the user to generate an error and be able to "fix" the thing that caused the error (perhaps by changing some parameter) and then want to run the operation again from the same process. But if there's been an error, then he's out of luck and has to restart the process (which, in some cases is a bit arduous.

So, to summarize. It works perfectly as long as there is no error. When there is an error, the process has to be restarted. I need a way to guard my "DoStuff()" code so that only one thread/process/user is ever running it at any time and when it fails, the process does not have to be shut down to release the mutex

My code looks like the following
if ( WantToDoStuff (...)

bool got_ownership
Mutex mt = new Mutex(true, "Company.Division.Project.Subproject.Component ", out got_ownership)
if ( got_ownership

tr

DoStuff ( ... )
GC.Collect()

catch ( Exception e

error_message = e.Message + e.StackTrace

GC.Collect()
mt.ReleaseMutex()

els

error_message = "Didn't get mutex."
GC.Collect()
Nov 15 '05 #1
2 2580
Lowell <th****@theyas.com> wrote:
So, to summarize. It works perfectly as long as there is no error.
When there is an error, the process has to be restarted. I need a way
to guard my "DoStuff()" code so that only one thread/process/user is
ever running it at any time and when it fails, the process does not
have to be shut down to release the mutex.


You have misunderstood the purpose of the third parameter to the Mutex
constructor. It doesn't say whether or not you've got ownership, it
says whether or not the mutex had to be constructed.

Instead of the code you've got, you should create the mutex without
bothering to try to gain ownership, and then call WaitOne on it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
Bingo

Thanks

Lowel

----- Jon Skeet [C# MVP] wrote: ----

Lowell <th****@theyas.com> wrote
So, to summarize. It works perfectly as long as there is no error
When there is an error, the process has to be restarted. I need a wa
to guard my "DoStuff()" code so that only one thread/process/user i
ever running it at any time and when it fails, the process does no
have to be shut down to release the mutex


You have misunderstood the purpose of the third parameter to the Mutex
constructor. It doesn't say whether or not you've got ownership, it
says whether or not the mutex had to be constructed

Instead of the code you've got, you should create the mutex without
bothering to try to gain ownership, and then call WaitOne on it

--
Jon Skeet - <sk***@pobox.com
http://www.pobox.com/~skee
If replying to the group, please do not mail me to

Nov 15 '05 #3

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

Similar topics

1
by: Didier FRAISSE | last post by:
i want to be sure that only one instance of my script is running at the same time i try this little script #--------------------------------------------------------------------------- # -*-...
0
by: Srijit Kumar Bhadra | last post by:
Hello, Here is some sample code with pywin32 build 203 and ctypes 0.9.6. Best regards, /Srijit File: SharedMemCreate_Mutex_win32all.py # This application should be used with...
2
by: Napo | last post by:
I use this code to ensure only one application is running at one time. In debug version, it runs ok But, in release version, it failed. I don't why??? bool flgDouble ; Mutex m = new Mutex( true,...
2
by: Ken Durden | last post by:
I'm setting up an interface where clients must perform external locking before calling certain commands. I do this to force them to specify the duration the action they performed must persist...
16
by: Ed Sutton | last post by:
I use a mutex to disallow starting a second application instance. This did not work in a release build until I made it static member of my MainForm class. In a debug build, first instance got...
2
by: tony.newsgrps | last post by:
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...
3
by: NaeiKinDus | last post by:
Hello, i'm trying to program a thread that would be locked (by a mutex) and that would only be unlocked once that a function (generating data) is done. The purpose is to generate data, and unlock...
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...
45
by: Chris Forone | last post by:
hello group, is there a chance for other functions to get the lock if i have following loop: while (running) { Lock local(mutex); }
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.