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.Divisi on.Project.Subp roject.Componen t", 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() 2 2613
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.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
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.co m http://www.pobox.com/~skee
If replying to the group, please do not mail me to This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
#---------------------------------------------------------------------------
# -*- coding: cp1252 -*-
from win32event import CreateMutex
from win32event import ReleaseMutex
from win32api import GetLastError
|
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 SharedMemAccess_Mutex_ctypes.py
or SharedMemAccess_Mutex_win32all.py
|
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, "App", out flgDouble);
if(flgDouble)
{
//Run Application
RunApp();
}
|
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 before any
other threads can be allowed to perform some action.
My object exposes a Mutex which is locked via a IDisposable-Sentry
class. I want to throw an exception if certain functions are called
without this Mutex already being locked, but I...
|
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 ownership, second did not and
terminated. In a release build, the second instance *also* got
ownership. I "fixed" it by making the mutex a static member of my
MainForm class.
Did the garbage collector eat my mutex because it is not referenced
| |
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
{
mutex=new System.Threading.Mutex( true, mutexName, out createdNew)
|
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
the mutex in order to activate the thread once the data is generated.
I have to do it this way, i can only call the thread if the data are
generated.
********************************************************
step 1: initialize the mutex
|
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 that the Mutex is already owned... but this has the unwanted side
effect of seizing ownership of the Mutex in the case where the Mutex is NOT
already owned.
I'm looking for something like an "IsOwned" property.
|
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);
}
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |