I have a web service method in which I create a particular type of object, use it for a few seconds, and then dispose it. Because of problems arising from multiple threads creating and using instances of this class at the same time, I need to restrict the method so that only one caller at a time ever has one of these objects.
To do this, I am creating a private static object:
private static object _lock = new object();
... and then inside the web service method I do this around the critical code:
lock (_lock)
{
using (DangerousObject dob = new DangerousObject())
{
dob.MakeABigMess();
dob.CleanItUp();
}
}
I'm not sure this is working, though. Do I have this right? Will this code ensure that only one instance of DangerousObject
is instantiated and in use at a time? Or does each caller get their own copy of _lock
, rendering my code here laughable?
Update: from a link in Ben Voigt's answer, I think I need to do this instead:
Mutex m = new Mutex(false, @"Global\MyMutex");
m.WaitOne();
try
{
using (DangerousObject dob = new DangerousObject())
{
dob.MakeABigMess();
dob.CleanItUp();
}
}
catch (Exception ex)
{
// what could possibly go wrong?
}
finally
{
m.ReleaseMutex();
}
This should function pretty much the same as my lock
code, but it will instead use a global object (because of the "Global\" prefix in the mutex' name). I hope.