First, if you want to use this across multiple applications, you'll have to create a named EventWaitHandle
using this constructor, or one of the others that creates a named object. A ManualResetEvent
will only work for a single process.
Second, a named Mutex is probably a better solution. I just realized that the code I recommended has a race condition. If Thread A does the WaitOne(0)
and it succeeds, and then Thread B comes along and does the same thing before Thread A can call Reset
, then both threads will end up displaying the message box.
Using a Mutex
and WaitOne(0)
will solve that problem. Be sure to release the Mutex
, though:
if (mutex.WaitOne(0))
{
try
{
// do stuff
}
finally
{
mutex.ReleaseMutex();
}
}