Using Asynchronous Events

The AsAsync() method is extended to EventHandler<> derivatives, PropertyChangingEventHandler and PropertyChangedEventHandler. It always invokes the delegate asynchronously... meaning that the target method does not need to complete before control is passed back to the object which fired the event (or invoked the delegate).
        var propertyChangingHandler = new PropertyChangingEventHandler(listener.HandleChange);
        observable.PropertyChanging += propertyChangingHandler.AsAsync();
        // listener.HandleChange can take ages to complete, and it won't hold up observable's thread.

AsAsync checks to see whether a SynchronizationContext is available... if so SynchronizationContext.Post is used to invoke the delegate, otherwise ThreadPool.QueueUserWorkItem is used to schedule the delegate for execution.

Last edited Oct 28, 2010 at 6:43 PM by maranite, version 1


No comments yet.