資源簡介
本示例完美演示了如何使用BackgroundWorker進行耗時的操作(如下載和數據庫事務)。示例中,指定圓周率pi的結果位數,即可計算對應的pi的值,并實時輸出當前計算的結果。由于采用多線程,用戶體驗非常好。

代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Data;
using?System.Diagnostics;
using?System.Drawing;
using?System.Text;
using?System.Threading;
using?System.Windows.Forms;
namespace?AsyncCalcPiWithBackgroundWorkerSample?{
??partial?class?AsyncCalcPiForm?:?Form?{
????public?AsyncCalcPiForm()?{
??????InitializeComponent();
????}
????void?ShowProgress(string?pi?int?totalDigits?int?digitsSoFar)?{
??????//?Make?sure?we‘re?on?the?UI?thread
??????Debug.Assert(this.InvokeRequired?==?false);
??????//?Display?progress?in?UI
??????this.resultsTextBox.Text?=?pi;
??????this.calcToolStripProgressBar.Maximum?=?totalDigits;
??????this.calcToolStripProgressBar.Value?=?digitsSoFar;
????}
????class?CalcPiUserState?{
??????public?readonly?string?Pi;
??????public?readonly?int?TotalDigits;
??????public?readonly?int?DigitsSoFar;
??????public?CalcPiUserState(string?pi?int?totalDigits?int?digitsSoFar)?{
????????this.Pi?=?pi;
????????this.TotalDigits?=?totalDigits;
????????this.DigitsSoFar?=?digitsSoFar;
??????}
????}
????void?CalcPi(int?digits)?{
??????StringBuilder?pi?=?new?StringBuilder(“3“?digits?+?2);
??????//?Report?initial?progress
??????this.backgroundWorker.ReportProgress(0
????????new?CalcPiUserState(pi.ToString()?digits?0));
??????if(?digits?>?0?)?{
????????pi.Append(“.“);
????????for(?int?i?=?0;?i???????????int?nineDigits?=?NineDigitsOfPi.StartingAt(i?+?1);
??????????int?digitCount?=?Math.Min(digits?-?i?9);
??????????string?ds?=?string.Format(“{0:D9}“?nineDigits);
??????????pi.Append(ds.Substring(0?digitCount));
??????????//?Report?continuing?progress
??????????this.backgroundWorker.ReportProgress(0
????????????new?CalcPiUserState(pi.ToString()?digits?i?+?digitCount));
??????????//?Check?for?cancelation
??????????if(?this.backgroundWorker.CancellationPending?)?{?return;?}
????????}
??????}
????}
????void?calcButton_Click(object?sender?EventArgs?e)?{
??????//?Don‘t?process?if?cancel?request?pending
??????//?(Should?not?be?called?since?we?disabled?the?button...)
??????if(?this.backgroundWorker.CancellationPending?)?{?return;?}
??????//?If?worker?thread?currently?executing?cancel?it
??????if(?this.backgroundWorker.IsBusy?)?{
????????this.calcButton.Enabled?=?false;
????????this.backgroundWorker.CancelAsync();
????????return;
??????}
??????//?Set?calculating?UI
??????this.calcButton.Text?=?“Cancel“;
??????this.calcToolStripProgressBar.Visible?=?true;
??????this.calcToolStripStatusLabel.Text?=?“Calculating...“;
??????//?Begin?calculating?pi?asynchronously
??????this.backgroundWorker.RunWorkerAsync(
????????(int)this.decimalPlacesNumericUpDown.Value);
????}
????void?backgroundWorker_DoWork(object?sender?DoWorkEventArgs?e)?{
??????//throw?new?Exception(“Ooops!“);
??????//?Track?start?time
??????DateTime?start?=?DateTime.Now;
??????CalcPi((int)e.Argument);
??????//?Indicate?
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2013-11-07?17:53??AsyncCalcPiWithBackgroundWorkerWithCancelationSample\
?????文件????????4132??2005-12-16?16:32??AsyncCalcPiWithBackgroundWorkerWithCancelationSample\AsyncCalcPiForm.cs
?????文件????????8297??2005-12-16?16:32??AsyncCalcPiWithBackgroundWorkerWithCancelationSample\AsyncCalcPiForm.designer.cs
?????文件????????6399??2005-12-16?16:32??AsyncCalcPiWithBackgroundWorkerWithCancelationSample\AsyncCalcPiForm.resx
?????文件????????3070??2005-12-16?16:32??AsyncCalcPiWithBackgroundWorkerWithCancelationSample\AsyncCalcPiWithBackgroundWorkerSample.csproj
?????文件?????????962??2005-12-16?16:32??AsyncCalcPiWithBackgroundWorkerWithCancelationSample\AsyncCalcPiWithBackgroundWorkerSample.sln
?????文件???????14848??2013-11-07?16:50??AsyncCalcPiWithBackgroundWorkerWithCancelationSample\AsyncCalcPiWithBackgroundWorkerSample.suo
?????目錄???????????0??2013-11-07?17:53??AsyncCalcPiWithBackgroundWorkerWithCancelationSample\bin\
?????目錄???????????0??2013-11-07?17:53??AsyncCalcPiWithBackgroundWorkerWithCancelationSample\bin\Debug\
?????文件???????24576??2013-11-07?10:12??AsyncCalcPiWithBackgroundWorkerWithCancelationSample\bin\Debug\AsyncCalcPiWithBackgroundWorkerSample.exe
?????文件???????32256??2013-11-07?10:12??AsyncCalcPiWithBackgroundWorkerWithCancelationSample\bin\Debug\AsyncCalcPiWithBackgroundWorkerSample.pdb
?????文件????????5632??2013-11-07?10:12??AsyncCalcPiWithBackgroundWorkerWithCancelationSample\bin\Debug\AsyncCalcPiWithBackgroundWorkerSample.vshost.exe
?????文件????????3791??2005-12-16?16:32??AsyncCalcPiWithBackgroundWorkerWithCancelationSample\NineDigitsOfPiAt.cs
?????目錄???????????0??2013-11-07?17:53??AsyncCalcPiWithBackgroundWorkerWithCancelationSample\obj\
?????文件????????1312??2013-11-07?10:12??AsyncCalcPiWithBackgroundWorkerWithCancelationSample\obj\AsyncCalcPiWithBackgroundWorkerSample.csproj.FileListAbsolute.txt
?????目錄???????????0??2013-11-07?17:53??AsyncCalcPiWithBackgroundWorkerWithCancelationSample\obj\Debug\
?????文件?????????180??2013-11-07?10:12??AsyncCalcPiWithBackgroundWorkerWithCancelationSample\obj\Debug\AsyncCalcPiWithBackgroundWorkerSample.AsyncCalcPiForm.resources
?????文件?????????852??2013-11-07?10:12??AsyncCalcPiWithBackgroundWorkerWithCancelationSample\obj\Debug\AsyncCalcPiWithBackgroundWorkerSample.csproj.GenerateResource.Cache
?????文件???????24576??2013-11-07?10:12??AsyncCalcPiWithBackgroundWorkerWithCancelationSample\obj\Debug\AsyncCalcPiWithBackgroundWorkerSample.exe
?????文件???????32256??2013-11-07?10:12??AsyncCalcPiWithBackgroundWorkerWithCancelationSample\obj\Debug\AsyncCalcPiWithBackgroundWorkerSample.pdb
?????文件?????????180??2013-11-07?10:12??AsyncCalcPiWithBackgroundWorkerWithCancelationSample\obj\Debug\AsyncCalcPiWithBackgroundWorkerSample.Properties.Resources.resources
?????目錄???????????0??2013-11-07?09:55??AsyncCalcPiWithBackgroundWorkerWithCancelationSample\obj\Debug\Refactor\
?????目錄???????????0??2013-11-07?09:36??AsyncCalcPiWithBackgroundWorkerWithCancelationSample\obj\Debug\TempPE\
?????文件?????????456??2005-12-16?16:32??AsyncCalcPiWithBackgroundWorkerWithCancelationSample\Program.cs
?????目錄???????????0??2013-11-07?17:53??AsyncCalcPiWithBackgroundWorkerWithCancelationSample\Properties\
?????文件????????1333??2005-12-16?16:32??AsyncCalcPiWithBackgroundWorkerWithCancelationSample\Properties\AssemblyInfo.cs
?????文件????????3313??2005-12-16?16:32??AsyncCalcPiWithBackgroundWorkerWithCancelationSample\Properties\Resources.Designer.cs
?????文件????????5612??2005-12-16?16:32??AsyncCalcPiWithBackgroundWorkerWithCancelationSample\Properties\Resources.resx
?????文件????????1119??2005-12-16?16:32??AsyncCalcPiWithBackgroundWorkerWithCancelationSample\Properties\Settings.Designer.cs
?????文件?????????249??2005-12-16?16:32??AsyncCalcPiWithBackgroundWorkerWithCancelationSample\Properties\Settings.settings
評論
共有 條評論