Class EventConnection
- Namespace
- CsCom.Runtime
- Assembly
- CsCom.Runtime.dll
A connection of an event sink to a COM object that raises events through a connection point. Disposing the connection disconnects the sink.
public sealed class EventConnection : IDisposable, IAsyncDisposable
- Inheritance
-
EventConnection
- Implements
- Inherited Members
Remarks
Dispose the connection on the thread that created it: IConnectionPoint::Unadvise must be called from the apartment
the connection was made in. There is no finalizer, so a connection that is not disposed stays connected until the COM
object releases the sink.
Every connection has an EventGate of its own, which it closes first when it is disconnected, and when it cannot be established. The generated sinks, the sinks of AdviseDispatch(object, Guid, IDispatchEventHandler) and the sinks created with the gate (Advise(object, Guid, Func<EventGate, object>)) start no handler call once the gate is closed, also when the COM object cannot be told at the moment and still calls the sink. A call that started before still completes; DisposeAsync() waits for it, and DisposeWithCompletion(Action) runs a completion after it.
Disconnect(), Dispose() and DisposeWithCompletion(Action) never wait for another thread. Disconnect() and Dispose() never throw; DisposeWithCompletion(Action) only throws the exceptions of the completions that it runs itself, and the ArgumentNullException of a null completion, which it throws before it closes the gate. The task of DisposeAsync() fails when the sink stays connected.
Properties
Cookie
Gets the cookie that identifies the connection.
public uint Cookie { get; }
Property Value
IsConnected
Gets a value indicating whether the sink is still connected: false once Disconnect() has disconnected it or given the connection up, true after a call that failed with an error that another call can succeed after.
public bool IsConnected { get; }
Property Value
Methods
Advise(object, Guid, Func<EventGate, object>)
Connects a sink that implements the event interface to the events of a COM object, created with the gate of the connection: the sink calls TryEnter() before it passes an event to its handler, and Exit() afterwards, so that no handler call starts once the connection is disconnected.
public static EventConnection Advise(object source, Guid iid, Func<EventGate, object> createSink)
Parameters
sourceobjectThe COM object: an object returned by a generated interface, a late-bound wrapper or a
dynamicobject. An object of the application that is exposed to COM, also one that such a wrapper wraps, raises the events itself, or else, when it implements IDispatchProvider, its Dispatch does.iidGuidThe interface identifier of the event interface.
createSinkFunc<EventGate, object>Creates the sink, a GeneratedComClassAttribute-attributed class that implements the event interface, with the gate of the connection. It is called for each connection, so that every sink has a gate of its own.
Returns
- EventConnection
The connection, which must be disposed to disconnect the sink.
Exceptions
- ArgumentNullException
The source or the factory is null, or the source stands for no object: an System.Runtime.InteropServices.UnknownWrapper of null, or a late-bound wrapper whose Dispatch is null.
- InvalidOperationException
The object does not raise the events of the interface, or the factory created no sink, also one that stands for no object.
- ArgumentException
The source is, or wraps, a System.Runtime.InteropServices.DispatchWrapper.
- ObjectDisposedException
The source is, or stands for, a released COM object.
- COMException
Finding the connection point or connecting the sink failed with another error, e.g. of a server that has ended or is busy.
Advise(object, Guid, object)
Connects a sink that implements the event interface to the events of a COM object. The sink has no gate, so it can get events until the COM object is told that it is disconnected; create it with the gate of the connection with Advise(object, Guid, Func<EventGate, object>) to stop its events at once.
public static EventConnection Advise(object source, Guid iid, object sink)
Parameters
sourceobjectThe COM object: an object returned by a generated interface, a late-bound wrapper or a
dynamicobject. An object of the application that is exposed to COM, also one that such a wrapper wraps, raises the events itself, or else, when it implements IDispatchProvider, its Dispatch does.iidGuidThe interface identifier of the event interface.
sinkobjectThe sink, a GeneratedComClassAttribute-attributed class that implements the event interface.
Returns
- EventConnection
The connection, which must be disposed to disconnect the sink.
Exceptions
- ArgumentNullException
The source or the sink is null, or one of them stands for no object: an System.Runtime.InteropServices.UnknownWrapper of null, or a late-bound wrapper whose Dispatch is null.
- InvalidOperationException
The object does not raise the events of the interface.
- ArgumentException
The source or the sink is, or wraps, a System.Runtime.InteropServices.DispatchWrapper.
- ObjectDisposedException
The source is, or stands for, a released COM object.
- COMException
Finding the connection point or connecting the sink failed with another error, e.g. of a server that has ended or is busy.
AdviseDispatch(object, Guid, IDispatchEventHandler)
Connects a handler to the events of a dispinterface, which the COM object raises through IDispatch::Invoke.
public static EventConnection AdviseDispatch(object source, Guid iid, IDispatchEventHandler handler)
Parameters
sourceobjectThe COM object: an object returned by a generated interface, a late-bound wrapper or a
dynamicobject. An object of the application that is exposed to COM, also one that such a wrapper wraps, raises the events itself, or else, when it implements IDispatchProvider, its Dispatch does.iidGuidThe interface identifier of the dispinterface.
handlerIDispatchEventHandlerThe handler of the invocations.
Returns
- EventConnection
The connection, which must be disposed to disconnect the handler.
Exceptions
- ArgumentNullException
The source or the handler is null, or the source stands for no object: an System.Runtime.InteropServices.UnknownWrapper of null, or a late-bound wrapper whose Dispatch is null.
- InvalidOperationException
The object does not raise the events of the interface.
- ArgumentException
The source is, or wraps, a System.Runtime.InteropServices.DispatchWrapper.
- ObjectDisposedException
The source is, or stands for, a released COM object.
- COMException
Finding the connection point or connecting the sink failed with another error, e.g. of a server that has ended or is busy.
Disconnect()
Disconnects the sink and returns the HRESULT of IConnectionPoint::Unadvise, S_OK for any
success. When the connection is already over, it returns the HRESULT that ended it: S_OK when the sink
was disconnected, the error when the connection was given up.
public int Disconnect()
Returns
Remarks
The gate of the connection is closed first, so that no handler call starts any more, whatever the result.
The errors of a call that can succeed when it is made again leave the sink connected, to be disconnected by another
call: RPC_E_WRONG_THREAD from another thread than that of the connection, RPC_E_CALL_REJECTED and
RPC_E_SERVERCALL_RETRYLATER from a busy server, and RPC_E_CANTCALLOUT_INASYNCCALL,
RPC_E_CANTCALLOUT_INEXTERNALCALL and RPC_E_CANTCALLOUT_ININPUTSYNCCALL from a call that cannot call
out at the moment. Any other error gives the connection up. The exception of a connection point that is a managed
object is returned as its HResult, as a call through COM would return it, or as
E_FAIL when that is no error code.
The method does not wait for another call that is disconnecting the sink at the moment, on another thread or as the
caller of the COM object that calls back: it returns S_FALSE at once, and that call decides whether the sink
is disconnected. S_FALSE means nothing else: a success of Unadvise is returned as S_OK.
Dispose()
Disconnects the sink like Disconnect(), but ignores the result. A disconnection that failed on the wrong thread or because the server was busy leaves the sink connected (IsConnected), and calling Dispose() or Disconnect() again retries it.
public void Dispose()
DisposeAsync()
Disconnects the sink like Dispose(), and completes when no handler call that entered the gate of the connection is running any more, after the completions of DisposeWithCompletion(Action) that the last of them runs, but not after one that runs at once on another thread. A sink that was passed to Advise(object, Guid, object) itself does not enter the gate, so the task then completes at once. Do not block on the task in a handler of the connection: the task waits for that call too.
public ValueTask DisposeAsync()
Returns
- ValueTask
A task that completes when no handler call is running any more.
Remarks
Unlike Dispose(), it reports a disconnection that failed with an error that leaves the sink connected
(see Disconnect()): once no handler call is running any more, since the gate is closed all the same, the
task fails with a COMException of the HRESULT if the sink is still connected. Another call can
have disconnected it in the meantime, e.g. Dispose() on the thread of the connection: the task then
completes, and one that is disconnecting it at that moment is waited for. When the task fails,
IsConnected is true, and Dispose(), Disconnect() or another
call of the method retries the disconnection. Any other error gives the connection up, without an exception, like
Dispose().
When another call is disconnecting the sink at the moment, it waits for that call without blocking, and reports its result like its own: the task fails with the error of that call when it leaves the sink connected, also when this call is made on the right thread. It does not disconnect the sink again then: that would have to be done on the thread of the caller, which may be blocked waiting for the task. Dispose the connection again on that thread. When that call has left the sink connected already, it disconnects the sink itself.
Like Dispose(), call it on the thread that created the connection when the COM object is a single-threaded
apartment object: an await before it can resume on another thread, from which the disconnection fails with
RPC_E_WRONG_THREAD. The exception of await using replaces an exception of its body.
DisposeWithCompletion(Action)
Disconnects the sink like Dispose(), and calls a completion once no handler call that entered the gate of the connection is running any more, without waiting for it: at once on the calling thread when none runs, otherwise on the thread of the last running call, when its handler has returned and before the call returns to the COM object.
public void DisposeWithCompletion(Action completion)
Parameters
completionActionThe completion.
Remarks
The completion runs where DisposeAsync() would complete, e.g. to complete observables once no handler pushes values any more, and needs no synchronization context: it is called in the event call of the COM object, like a handler, so that it does not depend on the thread that disposed the connection. Its exception is thrown to the caller when it runs at once, and otherwise fails the event call, like an exception of a handler. Do not block on DisposeAsync() in the completion: the task completes after it.
A sink that was passed to Advise(object, Guid, object) itself does not enter the gate, so the completion then runs at once. Every call adds its completion; they run one at a time, in the order of the calls. An exception does not stop the others, which other calls may have added: once all of them ran, the exception of the one that failed is thrown, or an AggregateException of them when several failed. A completion that is added while others run, also by one of them, is left to the thread that runs them, which runs it after them; its exception is then that thread's.
DisposeAsync() waits for the completions that the last running handler call runs, not for one that runs at once: on another thread, it can complete while such a completion still runs, e.g. while the Dispose() of a generated sink of the observable style completes the observables.
Exceptions
- ArgumentNullException
completionis null; then the sink is not disconnected, and the gate stays open.