资源简介
本示例完美演示了如何使用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
相关资源
- Asp.net本地调试工具
- asp.net留言板源码+设计说明
- C# Winform 图形缩放平移
- asp.net c#编写的机票预订系统
- 基于ASP.NET在线电脑报修系统
- asp.net学生公寓信息管理系统
- winform 很好用的分页控件带SQL数据库
- C# 写得使用固高的运动控制卡写得控
- ASP.NET从入门到精通随书光盘.iso
- csWPF井字棋游戏
- 基于asp.net的BBS论坛
- C#使用IBatisNet操作Oracle10g数据库
- Devexpress GridControl冻结头部几行
- .NET C#研发的授权工具winform
- Winform C#Socket异步通信
- 在winform下,利用控件ZedGraph控件绘制
- FileUpload控件上传文件客户端验证格式
- WPF 关于C#代码实现ControlTemplate
- C#WinForm获取子窗口返回值
- .net实现购物车
- C#Winform温度计控件
- ASP.NET jquery ajax无刷新上传文件demo
- VS2010构建ASP.NET三层架构演示代码
- asp.net 通用网站后台模板
- winForm word转pdf excel转pdf
- 招聘管理系统 asp.net sqlserver
- 小说网站,ASP.net
- Textbox背景透明(winform)
- c#实现SVM源码内有简单使用Demo
- asp.net+ajax 写的无刷新登录技术没有使
评论
共有 条评论