欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

一个支持Abort的BackgroundWorker

发布时间:2024/8/23 40 豆豆
生活随笔 收集整理的这篇文章主要介绍了 一个支持Abort的BackgroundWorker 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

  .net平台提供的BackgroundWorker不支持强制终止操作,所以这边实现了一个支持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 /// 开始异步运行线程,请在界面线程中调用 23 /// </summary> 24 public void RunWorkerAsync() 25 { 26 RunWorkerAsync(null); 27 } 28 29 /// <summary> 30 /// 开始异步运行线程,请在界面线程中调用 31 /// <param name="args">线程运行的参数,作为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 /// 发送取消请求 43 /// </summary> 44 public void CancelAsync() 45 { 46 this.mArgs.Cancel = true; 47 } 48 49 /// <summary> 50 /// 终止后台进程,不会触发Complete事件,后台进程直接结束 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 /// 汇报进度,触发ProgressChanged事件 84 /// </summary> 85 public void ReportProgress(int precent) 86 { 87 ReportProgress(precent, null); 88 } 89 90 /// <summary> 91 /// 汇报进度,触发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 /// 实际线程函数 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 }

 

转载于:https://www.cnblogs.com/ngxianyu/p/3359088.html

总结

以上是生活随笔为你收集整理的一个支持Abort的BackgroundWorker的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。