C# · 12月 20, 2021

C# BackgroundWorker 详解

BackgroundWorker _demoBGWorker = += BGWorker_DoWork( sum = ( i = ; i <= ; i+++=

.progressBarSum.Maximum = BGWorker_DoWork( endNumber = (e.Argument != = ( sum = ( i = ; i <= endNumber; i+++=

_demoBGWorker.WorkerReportsProgress = += BGWorker_ProgressChanged;

BGWorker_ProgressChanged( .progressBarSum.Value =</span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;如果有更多的信息需要传递,可以使用 e.UserState 传递<a href=”https://www.jb51.cc/tag/yige/” target=”_blank” class=”keywords”>一个</a><a href=”https://www.jb51.cc/tag/zidingyi/” target=”_blank” class=”keywords”>自定义</a>的类型。</span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;这是<a href=”https://www.jb51.cc/tag/yige/” target=”_blank” class=”keywords”>一个</a> object 类型的对象,您可以通过它传递任何类型。</span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;我们仅把当前 sum 的值通过 e.UserState 传回,并通过<a href=”https://www.jb51.cc/tag/xianshi/” target=”_blank” class=”keywords”>显示</a>在窗口上。</span><span style=”color: #0000ff;”&gt;string</span> message =<span style=”color: #000000;”&gt; e.UserState.ToString();</span><span style=”color: #0000ff;”&gt;this</span>.labelSum.Text =<span style=”color: #000000;”&gt; message;

}

BGWorker_DoWork(= sender endNumber = (e.Argument != = (</span><span style=”color: #0000ff;”&gt;int</span> sum = <span style=”color: #800080;”&gt;0</span><span style=”color: #000000;”&gt;;</span><span style=”color: #0000ff;”&gt;for</span> (<span style=”color: #0000ff;”&gt;int</span> i = <span style=”color: #800080;”&gt;0</span>; i <= endNumber; i++<span style=”color: #000000;”&gt;){ sum </span>+=<span style=”color: #000000;”&gt; i; </span><span style=”color: #0000ff;”&gt;string</span> message = <span style=”color: #800000;”&gt;”</span><span style=”color: #800000;”&gt;Current sum is: </span><span style=”color: #800000;”&gt;”</span> +<span style=”color: #000000;”&gt; sum.ToString(); </span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;ReportProgress <a href=”https://www.jb51.cc/tag/fangfa/” target=”_blank” class=”keywords”>方法</a>把信息传递给 ProcessChanged 事件处理<a href=”https://www.jb51.cc/tag/hanshu/” target=”_blank” class=”keywords”>函数</a>。 </span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;第<a href=”https://www.jb51.cc/tag/yige/” target=”_blank” class=”keywords”>一个</a>参数类型为 int,表示执行进度。 </span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;如果有更多的信息需要传递,可以使用 ReportProgress 的第二个参数。 </span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;这里我们给第二个参数传进去一条消息。</span>

<span style=”color: #000000;”> bgWorker.ReportProgress(i,message);
Thread.Sleep(<span style=”color: #800080;”>600<span style=”color: #000000;”>);
}
}

_demoBGWorker.WorkerSupportsCancellation = ;

<span style=”color: #008000;”>//<span style=”color: #008000;”>在操作的过程中需要检查用户是否取消了当前的操作。
<span style=”color: #0000ff;”>if (bgWorker.CancellationPending == <span style=”color: #0000ff;”>true<span style=”color: #000000;”>)
{
e.Cancel = <span style=”color: #0000ff;”>true<span style=”color: #000000;”>;
<span style=”color: #0000ff;”>break<span style=”color: #000000;”>;
}

_demoBGWorker.CancelAsync();

_demoBGWorker.RunWorkerCompleted += BGWorker_RunWorkerCompleted( </span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;计算已经结束,需要禁用取消按钮。</span><span style=”color: #0000ff;”&gt;this</span>.btnCancel.Enabled = <span style=”color: #0000ff;”&gt;false</span><span style=”color: #000000;”&gt;;</span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;计算过程中的异常会被抓住,<a href=”https://www.jb51.cc/tag/zaizheli/” target=”_blank” class=”keywords”>在这里</a>可以进行处理。</span><span style=”color: #0000ff;”&gt;if</span> (e.Error != <span style=”color: #0000ff;”&gt;null</span><span style=”color: #000000;”&gt;){ Type errorType </span>=<span style=”color: #000000;”&gt; e.Error.GetType(); </span><span style=”color: #0000ff;”&gt;switch</span><span style=”color: #000000;”&gt; (errorType.Name) { </span><span style=”color: #0000ff;”&gt;case</span> <span style=”color: #800000;”&gt;”</span><span style=”color: #800000;”&gt;ArgumentNullException</span><span style=”color: #800000;”&gt;”</span><span style=”color: #000000;”&gt;: </span><span style=”color: #0000ff;”&gt;case</span> <span style=”color: #800000;”&gt;”</span><span style=”color: #800000;”&gt;MyException</span><span style=”color: #800000;”&gt;”</span><span style=”color: #000000;”&gt;: </span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;do something.</span> <span style=”color: #0000ff;”&gt;break</span><span style=”color: #000000;”&gt;; </span><span style=”color: #0000ff;”&gt;default</span><span style=”color: #000000;”&gt;: </span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;do something.</span> <span style=”color: #0000ff;”&gt;break</span><span style=”color: #000000;”&gt;; }}</span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;计算结果信息:e.Result</span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;use it do something.</span>

}

e.Result = sum;