Table of Contents

Class EventGate

Namespace
CsCom.Runtime
Assembly
CsCom.Runtime.dll

The gate of an event connection, which EventConnection creates for each connection and passes to the sink that it connects. The gate is closed once the connection is disconnected, or when it could not be established; a closed sink accepts the events without passing them to its handler.

public sealed class EventGate
Inheritance
EventGate
Inherited Members

Remarks

A sink passes an event to its handler only between a successful TryEnter() and Exit(), so that no handler call starts once the gate is closed, and the connection knows when the calls that started before have completed (see DisposeAsync()). The last of them runs the completions of DisposeWithCompletion(Action) in its call of Exit().

Properties

IsClosed

Gets a value indicating whether the gate is closed: the sink must pass no more events to its handler.

public bool IsClosed { get; }

Property Value

bool

Methods

Exit()

Exits an event that TryEnter() has entered, when its handler has returned. Call it exactly once after each call of TryEnter() that returned true, and never after one that returned false: if (!gate.TryEnter()) return; try { … } finally { gate.Exit(); }.

public void Exit()

Remarks

The last event to exit a closed gate runs the completions of DisposeWithCompletion(Action) before the method returns, also those that are added while they run. Once all of them ran, the method throws the exception of one that failed, or an AggregateException of them when several failed: a sink handles it like an exception of its handler, e.g. returns it to the COM object. Called in a finally block, it replaces an exception of the handler, as DispatchEventSink.InvokeHandler(gate, …) does.

The gate counts the running events, not which ones they are: a call too many is only detected when no event is running. While others are running, it counts one of them as finished, so that DisposeAsync() can complete, and the completions run, while a handler still runs, and the call of that event then throws when it exits.

Exceptions

InvalidOperationException

No event has entered the gate.

TryEnter()

Enters an event before its handler is called. Each successful call must be followed by a call of Exit() when the handler returns.

public bool TryEnter()

Returns

bool

false when the gate is closed: the sink must not call its handler then.