C#在后台运行操作:BackgroundWorker的用法
在我们的程序中,经常会有一些耗时较长的运算,为了保证用户体验,不引起界面不响应,我们一般会采用多线程操作,让耗时操作在后台完成,完成后再进行处理或给出提示,在运行中,也会时时去刷新界面上的进度条等显示,必要时还要控制后台线程中断当前操作。
以前,类似的应用会比较麻烦,需要写的代码较多,也很容易出现异常。在.net中,提供了一个组件BackgroundWorker就是专门解决这个问题的。BackgroundWorker类允许在单独的专用线程上运行操作。 耗时的操作(如下载和数据库事务)在长时间运行时可能会导致用户界面(UI)似乎处于停止响应状态。如果需要能进行响应的用户界面,而且面临与这类操作相关的长时间延迟,则可以使用BackgroundWorker类方便地解决问题。
使用这个组件其实非常简单,例如,我们做一个类似如下界面的进度条的小例子,在后台线程中进行耗时运算,同时刷新界面上的进度条。
过程如下:
1.新建一个windows窗体应用程序,如:BackgroundWorkerProgressBarDemo
2.拖一个ProgressBar(progressBar1)和一个BackgroundWorker (backgroundWorker1)到Form上。
3.把下面的代码copy过去就ok了,代码注释的很详细,可以按照需要修改。
namespace BackgroundWorkerProgressBarDemo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent(); Shown += new EventHandler(Form1_Shown); // To report progress from the background worker we need to set this property
backgroundWorker1.WorkerReportsProgress = true; // This event will be raised on the worker thread when the worker starts
backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork); // This event will be raised when we call ReportProgress
backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
} void Form1_Shown(object sender, EventArgs e)
{
// Start the background worker
backgroundWorker1.RunWorkerAsync();
} // On worker thread so do our thing!
void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
// Your background task goes here
for (int i = ; i <= ; i++)
{
// Report progress to 'UI' thread
backgroundWorker1.ReportProgress(i);
// Simulate long task
System.Threading.Thread.Sleep();
}
} // Back on the 'UI' thread so we can update the progress bar
void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
// The progress percentage is a property of e
progressBar1.Value = e.ProgressPercentage;
}
}
}
若要为后台操作做好准备,请添加DoWork事件的事件处理程序,在此事件处理程序中调用耗时的操作。
若要开始此操作,请调用RunWorkerAsync。
若要收到进度更新的通知,请处理ProgressChanged 事件。
若要在操作完成时收到通知,请处理RunWorkerCompleted 事件。
注意:
您必须非常小心,确保在 DoWork 事件处理程序中不操作任何用户界面对象。 而应该通过 ProgressChanged 和 RunWorkerCompleted 事件与用户界面进行通信。
BackgroundWorker 事件不跨 AppDomain 边界进行封送处理。 请不要使用 BackgroundWorker 组件在多个 AppDomain 中执行多线程操作。
如果后台操作需要参数,请在调用 RunWorkerAsync 时给出参数。 在 DoWork 事件处理程序内部,可以从 DoWorkEventArgs.Argument 属性中提取该参数。
参考:
C#在后台运行操作:BackgroundWorker的用法的更多相关文章
- 后台运行之BackgroundWorker
BackgroundWorker 类允许您在单独的专用线程上运行操作. 耗时的操作(如下载和数据库事务)在长时间运行时可能会导致用户界面 (UI) 似乎处于停止响应状态. 如果您需要能进行响应的用户界 ...
- app额外后台运行操作
//在视图中运行操作中进行周期操作 - (void)applicationDidEnterBackground:(UIApplication *)application { [self beingBa ...
- iOS7程序后台运行
介绍 这次 iOS7 对程序后台运行进行了加强,但是仅仅是加强而已,要想像 Android 程序那样自由当然就别想了,苹果这么做主要还是出于电池使用时间考虑,但是这次的加强对大部分程序基本够用. 在介 ...
- linux的nohup命令的用法(后台运行程序命令)
linux的nohup命令的用法. 在应用Unix/Linux时,我们一般想让某个程序在后台运行,于是我们将常会用 & 在程序结尾来让程序自动运行.比如我们要运行mysql在后台: /usr/ ...
- 【原】无脑操作:Centos 7后台运行及终止jar包程序
1.后台运行jar包程序,输入:nohup java -jar /路径/程序.jar & 2.后台终止jar包程序,输入:ps -ef | grep java,查看使用java命令的进程,再输 ...
- 转:CentOS下后台运行Python脚本及关闭脚本的一些操作
自己写了一个python脚本,但是直接远程用putty连接后#python xxx.py执行,关闭putty脚本也随之关闭了,这里需要用到‘setsid’这个命令. #setsid python xx ...
- Linux 后台运行python .sh等程序,以及查看和关闭后台运行程序操作
1.运行.sh文件 直接用./sh 文件就可以运行,但是如果想后台运行,即使关闭当前的终端也可以运行的话,需要nohup命令和&命令. (1)&命令 功能:加在一个命令的最后,可以把这 ...
- centos shell基础 alias 变量单引号 双引号 history 错误重定向 2>&1 jobs 环境变量 .bash_history source配置文件 nohup & 后台运行 cut,sort,wc ,uniq ,tee ,tr ,split, paste cat> 2.txt <<EOF 通配符 glob模式 发邮件命令mail 2015-4-8 第十二节课
centos shell基础知识 alias 变量单引号 双引号 history 错误重定向 2>&1 jobs 环境变量 .bash_history source配置文件 ...
- IOS开发使用GCD后台运行
什么是GCD Grand Central Dispatch (GCD)是Apple开发的一个多核编程的解决方法.该方法在Mac OS X 10.6雪豹中首次推出,并随后被引入到了iOS4.0中.GCD ...
随机推荐
- 17.3---阶乘尾多少个0(CC150)
思路,其实这题easy,就是看多少个5. 答案: public static int getFactorSuffixZero(int n) { // write code here int res = ...
- 向SqlServer数据库插入数据
Insert Values Insert Select Insert Exec Select Into Bulk Insert Insert Values是最常用的一种插入数据的方式,基本语法如下,表 ...
- Silverlight datagrid 排序 (转)
Silverlight的DataGrid有很多强大之处,其中一个便是排序. DataGrid指定过ItemsSource之后,通过点击列头就可以实现排序,不用写任何代码.这对我这种懒人来说实在是太爽了 ...
- ajax调用aspx.cs中的WebMethod
前台: <script language="javascript" src="../js/jquery-1.8.2.js"></script& ...
- js 为label标签和div标签赋值
<label id="ttile"></label> document.getElementById('ttile').innerText="&q ...
- 【leetcode】Maximum Gap
Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...
- dubbo main方法启动
public static void main(String[] args) { com.alibaba.dubbo.container.Main.main(args); } 以上就可以简单本地启动了
- selenium源码分析-webdriver(二)
最近比较空闲就仔细看了一下Selenium的源码,因为主要是使用WebDriver所以重点关注了一下WebDriver的工作原理.在前一篇blog里已经解释过了WebDriver与之前Selenium ...
- linux学习之一些琐碎知识点
一.python 问:django中project和app之间到底有什么不同? 答:他们的区别就是一个是配置,另一个是代码. 一个project包含很多个django app以及对它们的配置.技术上, ...
- 【python】入门学习(七)
设置字符串格式: format % values >>> x =/ >>> print(x) 0.012345679012345678 >>> p ...