All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
ASP.NET中实现多文件上传的实例程序代码_[Asp.Net教程]
C#中ListBox控件应用实例
PHP,Shell的编写(改进版)_php资料_编程技术
简单示例:AJAX结合PHP代码实现登录_[PHP教程]
ASP.NET如何保留两位小数点_[Asp.Net教程]
Visual C++ 6.0中的应用程序向导
PHP程序指定目录里的指定后缀名文件为超连接_[PHP教程]
xhtml+css网页制作教程_[Html教程]
手把手教你写Ajax驱动的DataGrid控件
HTML网页制作基础教程(3):常用标记讲解_[Html教程]
Reading number is top 10 pictures
Ashlynn Brooke show proud chest measurement3
The household of rural style is designed
中国处女图鉴2
Compared GDP and per capita income in China for 40 years
Kim jong il's mistress, national beauty JinYuJi actor3
India's national beauty of the college students
The money of more than 100 countries and regions21
So beauty, will let you spray blood2
姑娘手慢了,已经走光了
Sora aoi in China1
Download software ranking
Ashlynn Video1
Unix video tutorial11
虚拟机汉化软件
Unix video tutorial2
Adobe Flash Player(IE) 10.0.32.18 浏览器专用的FLASH插件
jdk1.5
变速齿轮3.26
Boxer's Top ten classic battle6
天龙八部十二宫服务端
asp.netWeb服务器高级编程
qq published in(发表于) 2014/7/11 9:15:50 Edit(编辑)
C#中BackgroundWorker组件的用法以及实例

C#中BackgroundWorker组件的用法以及实例

C#中BackgroundWorker组件的用法以及实例

BackgroundWorker组件

功能

在运行程序时,有些操作可能需要很长的执行时间,例如,图像下载、Web服务调用和文件下载、上传等。这些操作可能导致用户界面在操作运行时挂起,如果此时需要用户界面的响应,可以使用BackgroundWorker组件,在不同于应用程序的主用户界面线程的另一线程上异步(“在后台”)执行耗时的操作。图1所示为BackgroundWorker组件。



图1 BackgroundWorker组件

2.属性

BackgroundWorker组件常用属性及说明如表1所示。



表1 BackgroundWorker 组件常用属性及说明

下面详细介绍WorkerSupportsCancellation属性,该属性用于获取或设置一个值,该值指示BackgroundWorker是否支持异步取消。

语法:

public bool WorkerSupportsCancellation { get; set; }
属性值:如果BackgroundWorker支持取消,则为True;否则为False。默认值为False。

示例

WorkerSupportsCancellation属性的使用

本示例主要是使用BackgroundWorker组件的WorkerSupportsCancellation属性,判断是否取消了异步操作。

程序主要代码如下:

if (this.backgroundWorker1.WorkerSupportsCancellation)

{

// The user canceled the operation.

MessageBox.Show("后台操作取消");

}

★★★★★主程序文件完整程序代码★★★★★

using System;

using System.Collections.Generic;

using System.Windows.Forms;

namespace _8_25

{

static class Program

{

///



/// 应用程序的主入口点。

///


[STAThread]

static void Main()

{

Application.EnableVisualStyles();

Application.SetCompatibleTextRenderingDefault(false);

Application.Run(new frmBackgroundWorker());

}

}

}

★★★★★frmBackgroundWorker窗体设计文件完整程序代码★★★★★

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Threading;

namespace _8_25

{

public partial class frmBackgroundWorker : Form

{

public frmBackgroundWorker()

{

InitializeComponent();

}

private int numberToCompute = 0;

private int highestPercentageReached = 0;

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)

{

e.Result = ComputeFibonacci((int)e.Argument, backgroundWorker1, e);

}

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)

{

this.progressBar1.Value = e.ProgressPercentage;

}

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)

{

if (e.Error != null)

{

MessageBox.Show(e.Error.Message);

}

else if (e.Cancelled)

{

resultLabel.Text = "Canceled";

}

else

{

resultLabel.Text = e.Result.ToString();

}

this.numericUpDown1.Enabled = true;

startAsyncButton.Enabled = true;

cancelAsyncButton.Enabled = false;

}

private void startAsyncButton_Click(object sender, EventArgs e)

{

resultLabel.Text = String.Empty;

this.numericUpDown1.Enabled = false;

this.startAsyncButton.Enabled = false;

this.cancelAsyncButton.Enabled = true;

numberToCompute = (int)numericUpDown1.Value;

highestPercentageReached = 0;

this.backgroundWorker1.RunWorkerAsync(numberToCompute);

}

private void cancelAsyncButton_Click(object sender, EventArgs e)

{

this.backgroundWorker1.CancelAsync();

this.progressBar1.Value = 0;

this.startAsyncButton.Enabled = true;

this.cancelAsyncButton.Enabled = false;

this.resultLabel.Text = "No Result";

}

long ComputeFibonacci(int n, BackgroundWorker worker, DoWorkEventArgs e)

{

if ((n < 0) || (n > 91))

{

throw new ArgumentException(

"value must be >= 0 and <= 91", "n");

}

long result = 0;

if (worker.CancellationPending)

{

e.Cancel = true;

}

else

{

if (n < 2)

{

result = 1;

}

else

{

result = ComputeFibonacci(n - 1, worker, e) +

ComputeFibonacci(n - 2, worker, e);

}

int percentComplete =

(int)((float)n / (float)numberToCompute * 100);

if (percentComplete > highestPercentageReached)

{

highestPercentageReached = percentComplete;

worker.ReportProgress(percentComplete);

}

}

return result;

}

private void frmBackgroundWorker_Load(object sender, EventArgs e)

{

}

}

}

★★★★★frmBackgroundWorker窗体设计文件完整程序代码★★★★★


添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.