Http://www.cnblogs.com/Charltsing/p/publisher.html
The difference between the efficiency of the recently tested task concurrency task and the thread pool found another problem. Task creation is fast, and the information that is output to the UI is too frequent, causing the UI to feign death.
private void Testmaketasks (Object obj) {string msg = ""; Loop create task for (int i = 0; i < i++) {//main thread (WinForm)--Child thread (thread)--worker thread (task )//Can not continuously access the UI in the loop, will cause the UI too late processing, resulting in suspended animation. task<string> TT = task.factory.startnew<string> (test, I); Tt. ContinueWith ((t) + = {if (t.isfaulted) {St Ring MSGEXCEP = (string. Format ("I have observed a {0}", T.exception.innerexception.gettype (). Name)); Interlocked.Increment (ref Responsecount); Interlocked.decrement (ref Threadscount); } }); Thread.Sleep (1); Avoid sending UI update requests too quickly, resulting in suspended animation. msg = "Number of requests Sent:" + RequestCount. ToString () + "Number of Completed requests:" + Responsecount. ToString (); The output thread establishes the information to the UI, which causes the animation to occur too frequently.
Publishstatus (msg); Interlocked.Increment (ref Threadscount); Interlocked.Increment (ref RequestCount); if (_isstop) {publishmessage ("exiting ... \ r \ n"); Break }} publishmessage ("Send finished, wait for thread to run!"} \ r \ n "); while (Responsecount < RequestCount) {Thread.Sleep (300); msg = "! Number of Sent requests:" + RequestCount. ToString () + "Number of Completed requests:" + Responsecount. ToString (); Publishstatus (msg); } _isrunning = false; PublishMessage ("\ r \ n All threads run! \ r \ n "); }
To address this issue, consider using Publisher mode to control the UI output
public class Publisher {public bool IsBusy = FALSE; Flag public delegate void Publisheventhander (object sender, Pubeventargs e); public event Publisheventhander PublishMessage; public event Publisheventhander Publishstatus; Declares a overridable Onpublish protection function protected virtual void Onpublishmessage (Pubeventargs e) {if (publishmes Sage! = null) {//sender = this, which is publisher this. PublishMessage (this, e); }} protected virtual void Onpublishstatus (Pubeventargs e) {if (publishstatus! = null) {//sender = this, which is publisher this. Publishstatus (this, e); }}///<summary>//Trigger Publish Message Event///</summary> public void Issuem Essage (String message) {Onpublishmessage (new Pubeventargs (message)); }//<summary>/Trigger Publish Status Event///</summary> public void Issuestatus (String message) {if (! IsBusy) Onpublishstatus (new Pubeventargs (message)); } }
Also, in the UI output section, invoke is used to block more UI operations (you cannot use BeginInvoke).
private void Writestatus (String msg) { if (this.lblStatus.InvokeRequired) { Invoke (cdwritestatus, msg); } else { updatelblmethod (msg); } }
The general idea is that after a task is created, a notification is sent to publisher (using the Issuestatus function), and Publisher is notified to Subscriber via the Onpublishstatus event. The UI interface acts as a subscriber by setting Publisher's busy state so that publisher cancels the extra output events that follow.
If you have any questions, please contact QQ 564955427
Use Publisher mode to control frequent UI output and avoid WinForm screen animation