I am trying to determine whether I can use a ManualResetEvent
here to ensure that in a concurrent environment, the inner actions of myMethod()
are never invoked concurrently.
static volatile bool _syncInitialized = false;
static ManualResetEvent _syncEvent = new ManualResetEvent(false);
static object _syncLock = new object();
void myMethod()
{
lock (_syncLock)
{
if (!_syncInitialized) // sync hasn't started, so
{
_syncInitialized = true;
_syncEvent.Set(); // signal for the first time only
}
}
if (_syncEvent.WaitOne()) // wait for signal
{
_syncEvent.Close();
_syncEvent = new ManualResetEvent(false); // reset to false
// actions that should be forced to run sequentially
}
}
EDIT - Note I am using ManualResetEvent instead of just lock() because I want to be able to add a timeout, potentially.