-
Notifications
You must be signed in to change notification settings - Fork 16
EntryCountingEvent
ikopylov edited this page Dec 26, 2014
·
2 revisions
EntryCountingEvent is a synchronization primitive that allows to track entering clients and wait while they not complete execution.
Sample code:
public class SomeManager: IDisposable
{
private readonly EntryCountingEvent _entryCounter = new EntryCountingEvent();
public void Foo()
{
using (_entryCounter.EnterClientGuarded())
{
// do some work
Thread.Sleep(100);
}
}
public void Bar()
{
using (var guard = _entryCounter.TryEnterClientGuarded())
{
// check if we enter successfully
if (!guard.IsAcquired)
return;
// do some work
Thread.Sleep(100);
}
}
public void Dispose()
{
if (!_entryCounter.IsTerminated)
{
// After this call the new clients will not enter Foo and Bar methods
_entryCounter.Terminate();
// Wait for completion of all already executed clients
_entryCounter.Wait();
}
}
}