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.

最新文章

  1. iOS 使用AFN 进行单图和多图上传
  2. 项目Windows服务安装命令:
  3. robotframework笔记11
  4. mysql 用户权限设置【转】
  5. 在viewPager中的textview参数singleLine和gravity为center冲突bug
  6. 纯C++ 连接SQL Server2005 数据库读写操作的小例子
  7. 鼠标滑过提示title
  8. GodSon Easyui 结合Pluplaod插件的文件分割上传
  9. 【HDOJ】2440 Watch out the Animal
  10. android假设重写onDraw实现一个相似TextView能够显示表情和链接的控件(一)
  11. C++自定义命名空间
  12. zf-关于表单不能提交的bug修改
  13. STM32驱动AT24CXX系列芯片
  14. 5-15 QQ帐户的申请与登陆 (25分) HASH
  15. Teching Yourself Programming in Ten Years -Peter Norvig
  16. NOIP2010普及组 导弹拦截
  17. wire [7:0] regAddr; 理解
  18. J-Link Version
  19. JS遍历Table的所有单元格内容
  20. DevExpress控件安装和初次使用图解

热门文章

  1. COM ,Threading Models,apartments,RPC
  2. mrtg
  3. wikioi1012 最大公约数和最小公倍数问题(2001年NOIP全国联赛普及组)
  4. wikioi 1973 Fibonacci数列【输出第N项的值】
  5. 【转】PHP error_reporting() 错误控制函数功能详解
  6. Android监听点击事件实现的三种方法
  7. Java OCR 图像智能字符识别技术,可识别中文
  8. jquery的$.extend和$.fn.extend作用及区别
  9. myBatis抛出异常Result Maps collection already contains value ...
  10. linux中常用目录的作用