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(
         "topic://EventBrokerSample/SimpleEvent" ,
         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.

转载于:https://www.cnblogs.com/zeroone/p/4979313.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值