Handlers
You have to define how the event should be handled in the event subscription:
This tells the event broker to execute the handler method on the same thread as the publisher fired the event. There will no thread switching.
Out of the box, Appccelerate provides you with these handler types:
OnPublisher
: handle on same thread as event was firedOnBackground
: handle asynchronously on a thread-pool worker threadOnUserInterface
: handle on user interface thread and block publisher until executedOnUserInterfaceAsync
: handle asynchronously on user interface thread.
When using a handler that synchronizes onto the user interface thread, the subscriber has to be register on the event broker on the user interface thread. Otherwise, the event broker will throw an excpetion during registration because it would not be able to switch threads.
Handler Restrictions
Publishers can restrict how the subscribers have to handle the event - synchronously or asynchronously - with a handler restriction.
If for example a publisher fires an event that can be cancelled (CancelEventArgs
), all subscribers have to handle it
synchronously. Otherwise the publisher cannot evaluate the result.
On the other side, a publisher may require all subscribers to run asynchronously because it cannot risk to be blocked by handlers taking a long time to execute.
Therefore the publisher can use a handler restriction:
There is also HandlerRestriction.None
that you can use in cases when you need to specify a restriction but the publisher
works with eihter synchronous or asynchronous handlers.
If a subscriber does not meet the restriction, an exception is thrown during registration.
Writing your own handler
You can use your own handler by implementing the interface IHandler
.