zoukankan      html  css  js  c++  java
  • EventBroker

    Sample publisher

    Publish an event topic:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    public class Publisher
    {
        [EventPublication("topic://EventBrokerSample/SimpleEvent")]
        public event EventHandler SimpleEvent;
         
        public void FireSimpleEvent()
        {
            SimpleEvent(this, EventArgs.Empty);
        }
    }

    Register the publisher with your event broker (you have to hold an instance of the event broker somewhere in your code).

    1
    2
    3
    EventBroker eventBroker = ...;
    Publisher publisher = new Publisher();
    eventBroker.Register(publisher);

    On registration of the publisher, the event broker inspects the publisher for published events (events with the EventPublication attribute).

    Sample subscriber

    Subscribe to an event topic:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    public class Subscriber
    {
        [EventSubscription(
            typeof(OnPublisher))]
     
        public void SimpleEvent(object sender, EventArgs e)
        {
            // do something useful or at least funny
        }
    }

    Register the subscriber with the event broker:

    1
    2
    3
    EventBroker eventBroker = ...;
    Subscriber subscriber = new Subscriber();
    eventBRoker.Register(subscriber);

    The event broker will inspect the subscriber on registration for subscription to event topics (methods with the EventSubscription attribute).

    If a publisher fires an event topic for that subscribers are registered, then the event broker will relay them to the subscribers by calling the subscription handler methods with the sender and EventArgs the publisher used to fire its event.

    Publication options

    Simple

    1
    2
    [EventPublication("topic://Simple")]
    public event EventHandler SimpleEvent;

    With custom Eventargs

    Note: CustomEventArgs has simply to be derived from EventArgs.

    1
    2
    [EventPublication("topic://CustomEventArgs")]
    public event EventHandler<CustomEventArguments> CustomEventArgs;

    Publish multiple event topics with one single event

    1
    2
    3
    4
    [EventPublication("Event1")]
    [EventPublication("Event2")]
    [EventPublication("Event3")]
    public event EventHandler MultiplePublicationTopics;

    Allow only synchronous subscription handlers

    1
    2
    [EventPublication("topic://Simple", HandlerRestriction.Synchronous)]
    public event EventHandler AnEvent;

    Allow only asynchronous subscription handlers

    1
    2
    [EventPublication("topic://Simple", HandlerRestriction.Asynchronous)]
    public event EventHandler AnEvent;

    Subscription options

    Simple

    1
    2
    [EventSubscription("topic://Simple", typeof(OnPublisher)]
    public void SimpleEvent(object sender, EventArgs e) {}

    Custom EventArgs

    1
    2
    [EventSubscription("topic://CustomEventArgs"), typeof(OnPublisher))]
    public void CustomEventArgs(object sender, CustomEventArgs e) {}

    Subscribe multiple event topics

    1
    2
    3
    4
    [EventSubscription("Event1", typeof(OnPublisher))]
    [EventSubscription("Event2", typeof(OnPublisher))]
    [EventSubscription("Event3", typeof(OnPublisher))]
    public void MultipleSubscriptionTopics(object sender, EventArgs e) {}

    Execute handler on background thread (asynchronous)

    The event broker creates a worker thread to execute the handler method on. The publisher can immediately continue processing.

    1
    2
    [EventSubscription("topic://Background", typeof(OnBackground))]
    public void BackgroundThread(object sender, EventArgs e) {}

    Execute handler on UI thread

    Use this option if calling from a background worker thread to a user interface component that updates the user interface - no need for Control.Invoke(...) anymore.

    1
    2
    [EventSubscription("topic://UI", typeof(OnUserInterface))]
    public void UI(object sender, EventArgs e) {}

    Note that if you use the OnUserInterface handler, you have to make sure that you register the subscriber on the user interface thread. Otherwise, the EventBroker won't be able to switch to the user interface thread, and will throw an exception.

    Execute handler on UI thread asynchronously

    The same as above, but the publisher is not blocked until the subscriber has processed the event.

    1
    2
    [EventSubscription("topic://UIAsync", typeof(OnUserInterfaceAsync))]
    public void UI(object sender, EventArgs e) {} 

    Simplified subscription handler signatures

    If you are not interested in the sender of the event, you can leave the sender out in the handler method:

    1
    2
    [EventSubscription("topic://handle.me", typeof(OnPublisher))]
    public void Handle(EventArgs e) {} 

    If you also don't need the event arguments, you can ignore them, too:

    1
    2
    [EventSubscription("topic://handle.me", typeof(OnPublisher))]
    public void Handle() {} 

    And if you have a generic event argument EventArgs<T>, you can directly define the content value of the event arguments:

    1
    2
    3
    4
    5
    [EventPublication("topic://handle.me")]
    public event EventHandler<EventArgs<string>> Event;
     
    [EventSubscription("topic://handle.me", typeof(OnPublisher))]
    public void Handle(string value) {} 

    These are the basics about the EventBroker. See the rest of the documentation for more options and details.

  • 相关阅读:
    转载:【Oracle 集群】RAC知识图文详细教程(三)--RAC工作原理和相关组件
    转载:【Oracle 集群】RAC知识图文详细教程(一)--集群概念介绍
    转载:【Oracle 集群】RAC知识图文详细教程(二)--Oracle 集群概念及原理
    题目总结
    面试题(包含答案)
    ElementUI动态表格数据转换formatter
    父组件搜索列表 给 子组件传值问题
    项目提取公共接口方法
    数组常用方法总结
    vue全局注册
  • 原文地址:https://www.cnblogs.com/zeroone/p/4979313.html
Copyright © 2011-2022 走看看