Skip to content
ikopylov edited this page Dec 26, 2014 · 2 revisions

Qoollo EntryCountingEvent

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();
        }
    }
}
Clone this wiki locally