一个支持Abort的BackgroundWorker
生活随笔
收集整理的這篇文章主要介紹了
一个支持Abort的BackgroundWorker
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
.net平臺提供的BackgroundWorker不支持強制終止操作,所以這邊實現(xiàn)了一個支持Abort操作的BackgroundWorker.
1 using System; 2 using System.Threading; 3 using System.ComponentModel; 4 using System.Windows.Forms; 5 namespace AbortableWorker 6 { 7 /// <summary> 8 /// backgroundworker 9 /// </summary> 10 class tbBackgroundWorker 11 { 12 private Thread mThread=null; 13 private bool mIsBusy = false; 14 private AsyncOperation mAsyncOperation = null; 15 private DoWorkEventArgs mArgs = null; 16 17 public event DoWorkEventHandler DoWork; 18 public event RunWorkerCompletedEventHandler RunWorkerCompleted; 19 public event ProgressChangedEventHandler ProgressChanged; 20 21 /// <summary> 22 /// 開始異步運行線程,請在界面線程中調(diào)用 23 /// </summary> 24 public void RunWorkerAsync() 25 { 26 RunWorkerAsync(null); 27 } 28 29 /// <summary> 30 /// 開始異步運行線程,請在界面線程中調(diào)用 31 /// <param name="args">線程運行的參數(shù),作為DoWork里面的args成員</param> 32 /// </summary> 33 public void RunWorkerAsync(object args) 34 { 35 mAsyncOperation = AsyncOperationManager.CreateOperation(null); 36 mThread = new Thread(new ParameterizedThreadStart(RunDowork)); 37 mThread.IsBackground=true; 38 mThread.Start(args); 39 } 40 41 /// <summary> 42 /// 發(fā)送取消請求 43 /// </summary> 44 public void CancelAsync() 45 { 46 this.mArgs.Cancel = true; 47 } 48 49 /// <summary> 50 /// 終止后臺進程,不會觸發(fā)Complete事件,后臺進程直接結(jié)束 51 /// </summary> 52 public void Abort() 53 { 54 System.Diagnostics.Debug.WriteLine(mThread.ThreadState.ToString()); 55 if (this.mThread != null && (mThread.IsAlive)) 56 { 57 try 58 { 59 this.mThread.Abort(); 60 61 } 62 catch (Exception e) 63 { 64 65 } 66 67 this.mIsBusy = false; 68 } 69 } 70 71 /// <summary> 72 /// 指示后天線程是否還在運行 73 /// </summary> 74 public bool IsBusy 75 { 76 get 77 { 78 return this.mIsBusy; 79 } 80 } 81 82 /// <summary> 83 /// 匯報進度,觸發(fā)ProgressChanged事件 84 /// </summary> 85 public void ReportProgress(int precent) 86 { 87 ReportProgress(precent, null); 88 } 89 90 /// <summary> 91 /// 匯報進度,觸發(fā)ProgressChanged事件 92 /// </summary> 93 public void ReportProgress(int precent,object userstate) 94 { 95 ProgressChangedEventArgs args = new ProgressChangedEventArgs(precent, userstate); 96 mAsyncOperation.Post(RunReportProgress, args); 97 } 98 99 /// <summary> 100 /// 實際線程函數(shù) 101 /// </summary> 102 /// <param name="args"></param> 103 private void RunDowork(object args) 104 { 105 Exception exception=null; 106 DoWorkEventArgs dowork=new DoWorkEventArgs(args); 107 mArgs = dowork; 108 mIsBusy = true; 109 try 110 { 111 DoWork(this, dowork); 112 } 113 catch(Exception e) 114 { 115 exception=e; 116 } 117 RunWorkerCompletedEventArgs completeArgs = new RunWorkerCompletedEventArgs(dowork.Result, exception, dowork.Cancel); 118 mIsBusy = false; 119 mAsyncOperation.Post(RunReportFinish, completeArgs); 120 } 121 122 private void RunReportProgress(object args) 123 { 124 ProgressChangedEventArgs progressargs=(ProgressChangedEventArgs)args; 125 if (ProgressChanged != null) 126 { 127 ProgressChanged(this, progressargs); 128 } 129 130 } 131 132 private void RunReportFinish(object args) 133 { 134 RunWorkerCompletedEventArgs completeArgs = (RunWorkerCompletedEventArgs)args; 135 if (RunWorkerCompleted != null) 136 { 137 RunWorkerCompleted(this, completeArgs); 138 } 139 } 140 141 } 142 }?
轉(zhuǎn)載于:https://www.cnblogs.com/ngxianyu/p/3359088.html
總結(jié)
以上是生活随笔為你收集整理的一个支持Abort的BackgroundWorker的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vim 代码注释插件
- 下一篇: 给地址栏添加图标