资源简介
本示例完美演示了如何使用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 < digits; i += 9 ) {
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
相关资源
- C# OCR数字识别实例,采用TessnetOcr,对
- Asp.net学生信息管理系统源码
- asp.net C#购物车源代码
- ASP.NET实验室预约管理系统
- Winform可视化打印模板设计工具含源码
- 020ASP.NET车辆综合管理系统.zip
- c# winform 自动登录 百度账户 源代码
- DirectX.Capturec# winform 操作摄像头录像附
- c# 高校档案信息管理系统
- C# 五子棋程序 附带编程日记
- C# winform实现表数据导出到Excel表格
- asp.net中c#做的躲避小游戏,希望大家
- ASP.NET C# 工资管理系统
- 一款漂亮的灯光闪烁的圣诞树(C# V
- C# WinForm读写INI文件
- C#笔试题大全C#面试集合包括了,.ne
- ASP.NET客户管理系统(毕业设计 C#
- 在线求职系统(C#ASP.NET源码)
- .NET C# Custom Form Designer (附源碼)
- C#处理png图片位深度和交错属性
- C#冒泡排序动态演示程序(看了就会)
- 酒店管理系统(c#.net源码)
- winform(c#)最全73种非常漂亮界面样式
- winform与内嵌echarts的数据交互,让数据
- MvCodeReaderSDKNet海康相机SDK二次开发,
- winform分页控件
- winform materialskin好看的皮肤组件
- winform 皮肤
- C#全套皮肤一共73款(IrisSkin4)
- UI界面皮肤(winform)
评论
共有 条评论