multithreading - Can Prism EventAggregator be used for threading needs? -
itemprop = "text">
I was looking at the Prism EventAgator and its' Great part I was the most worried UI thread correctly for martial threads It had its potential.
I was thinking that I can use this capability to provide a class to module developers, which can be used to make the thread similar to the background worker. Class interface to some extent
Public Interface IMyTask {event DoWorkEventHandler DoWork; Can be similar to; Event RunWorkerCompletedEventHandler RunWorker Complete; Zero run-task async (object obj); }
I have created similar types of backgroundworkers to do better. In the implementation I am registering TaskStart and Functional Events
Public class TaskStartEventPayload { Public Subscription Token Token {get; Set; } The argument of the public object {get; Set; }} Public Class TaskStart Document: Composite Presentation Event & amp; Task Start AventPeload & gt; {} Public Sector Efficient EventPayload {Public Subscription Token Token {get; Set; } The argument of the public object {get; Set; } The result of public content {get; Set; }} Public Sector Efficient Event: Composite Presentation Event & Tt; TaskCompleteEventPayload & gt; {}
MyTask class I do not require which yarn is complete as
in the manufacturer for public MyTask (IEventAggregator eventAggregator, bool isUICompletion ) {If (eventAggregator == faucet) {New argument NullException ("eventAggregator"); } _eventAggregator = eventAggregator; _eventAggregator.GetEvent & LT; TaskStartEvent & gt; (TaskStartHandler, ThreadOption.BackgroundThread, False, New T & # 39; TaskStartEventPayload> (StartTokenFilter)); If (isUICompletion) _token = _eventAggregator.GetEvent & LT; TaskCompleteEvent & gt; (TaskCompleteHandler, ThreadOption.UIThread, is true, the new predicate & lt; TaskCompleteEventPayload & gt; (CompleteTokenFilter)); The rest _token = _eventAggregator.GetEvent & LT; TaskCompleteEvent & gt; (TaskCompleteHandler, ThreadOption.BackgroundThread, true, new predicate & lt; TaskCompleteEventPayload & gt; (CompleteTokenFilter)); }
Here I am registering with filters where the filter function event is only received if it is received while subscribing to the same token as while payload.
Next I use
create public Zero RunTaskAsync (object obj) {// payload _eventAggregator.GetEvent < TaskStartEvent & gt; () Published (payload); } Public Zero StartStart Handler (TaskTourAventPeload T) {// Create Fireworks and Payload Dockwork (this, Args); _eventAggregator.GetEvent & LT; TaskCompleteEvent & gt; () Published (TC) } Public Zero Functional Handler (TaskCompleteEventPayload T) {RunWorkerCompleted (this, Args); }
This class can be used as a
MyTask et = new MyTesign, true); Et.DoWork + = New System. ComportModel.DownerHandler (ATDOverker); Et.RunWorkerCompleted + = New System.ComponentModel.RunWorkerCompletedEventHandler (et_RunWorkerCompleted); Et.RunTaskAsync ("Test");
The advantage I see in this approach is 1. It uses the threadspace, so there is no upper part of creating threads in the form of background workers. In the RunWorkerCompleted case 2. The appropriate thread is to be executed on the Marshal UI thread.
Please advice if it will be correct to use eventaggregator as threader. Text after "
This will work, however this code is for debugging you for very low performance benefits. Micro-optimizing is rarely worth the effort and support costs in my opinion.
EventAggregator is just a message for your application and I generally do not, that I have a lot of code Debug likes to use things for their original intention, but that's my personal choice.
The event aggregator is going to do a little harder work than it is to clean those subscriptions, which will likely be more than any performance gain you gain from thread pooling But that's just an estimate.
Comments
Post a Comment