Reputation: 10122
How can I lock a function such that it blocks other instances of the program from executing it until it's finished?
I think I need a system mutex of some kind and a WaitOne but I'm not sure how to put it together in a robust way. I'm thinking something like...
public static void function()
{
bool ok;
using (Mutex mutex = new Mutex(true, "Set-IP Instance", out ok))
{
if(!ok)
{
mutex.WaitOne();
}
...
}
}
Upvotes: 1
Views: 254
Reputation: 13394
The behavior you are looking for can be achieved with this code (simple version):
using(Mutex mutex = new Mutex(false, "GlobalMutexId"))
{
mutex.WaitOne();
//Do your thing
mutex.ReleaseMutex();
}
Or if you prefer a more reusable approach:
public class MutexFactory
{
private string _name;
public MutexFactory(string name)
{
_name = name;
}
public SingleUseMutex Lock()
{
return new SingleUseMutex(_name);
}
}
public class SingleUseMutex : IDisposable
{
private readonly Mutex _mutex;
internal SingleUseMutex(string name)
{
_mutex = new Mutex(false, name);
_mutex.WaitOne();
}
public void Dispose()
{
_mutex.ReleaseMutex();
_mutex.Dispose();
}
}
Can be used like this:
private void TestFunction()
{
MutexFactory factory = new MutexFactory("YourMutexId");
for (int i = 0; i < 100; i++)
{
// also works for new instances of your program of course
new Thread(Interlocked).Start(factory);
}
}
private void Interlocked(object obj)
{
Guid guid = Guid.NewGuid();
MutexFactory factory = obj as MutexFactory;
using (factory.Lock())
{
Debug.WriteLine(guid.ToString("N") + " start");
//Waste Time
Thread.Sleep(50);
Debug.WriteLine(guid.ToString("N") + " end");
}
}
Upvotes: 1