0

I would like to create a semaphore in my app, where the creation will fail (with a clear exception), if another instance of the app is running and has already created the semaphore. So only one per server.

I'd like the limit of only one to hold across the system, not just the CLR. But I do not want it to hold across multiple servers (or VMs). i.e. I want the app able to run on 2 distinct servers.

Is this possible? If so, how?

thanks - dave

4

1 回答 1

1

你可以用System.Threading.Mutex这个。

命名系统互斥锁在整个操作系统中都是可见的,并且可以用来同步进程的活动。

bool b = true;
Mutex mutex = new Mutex(true, "MyMutex", out b);
if (!b) throw new InvalidOperationException("Another instance is running");
于 2013-06-07T18:03:17.073 回答