为了防止操作过程中界面卡死,和WinForm搭配最适合的就是BackgroundWorker了。BackgroundWorker 类

using System;
using System.ComponentModel;
using System.Threading;
using System.Windows.Forms; namespace ProcessImpactID
{
public partial class Form1 : Form
{
BackgroundWorker worker = new BackgroundWorker(); public Form1()
{
InitializeComponent(); worker.DoWork += new DoWorkEventHandler(worker_DoWork);
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
worker.ProgressChanged += Worker_ProgressChanged;
//set it to true for support cancel event
//when call CancelAsync() it can set property CancellationPending to true.
worker.WorkerSupportsCancellation = true;
//set it to true for raise Progress Report.
worker.WorkerReportsProgress = true;
} private void btnStart_Click(object sender, EventArgs e)
{
if (worker.IsBusy)
{
MessageBox.Show("the process has been started");
return;
}
worker.RunWorkerAsync("e.argument");
} private void btnStop_Click(object sender, EventArgs e)
{
if (worker.IsBusy)
{
worker.CancelAsync();
}
} private void Worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
//this is UI Thread
this.label1.Text = e.ProgressPercentage.ToString();
} void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
//bool isThreadPoolThread = System.Threading.Thread.CurrentThread.IsThreadPoolThread;
//this is UI Thread
if (e.Error != null)
{
MessageBox.Show(e.Error.Message, "Error");
return;
}
if (e.Cancelled)
{
MessageBox.Show("the process has been cancelled");
return;
}
MessageBox.Show(e.Result.ToString());
} void worker_DoWork(object sender, DoWorkEventArgs e)
{
//bool isThreadPoolThread = System.Threading.Thread.CurrentThread.IsThreadPoolThread;
string str = (string)e.Argument;
string result = "work result";
worker.ReportProgress(0);
for (int i = 0; i < 100; i++)
{
//if CancellationPending is true, stop process.
//and report process result.
if (worker.CancellationPending)
{
e.Cancel = true;
break;
}
Thread.Sleep(1000);
//Report Progress
worker.ReportProgress(i * 1);
} //set the RunWorkerCompleted result
e.Result = string.Format("{0} => {1}", str, result);
}
}
}

System.ComponentModel.BackgroundWorker在WinForm中的异步使用的更多相关文章

  1. System.Web.Security 在winform中是什么命名空间呢

    des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStorin ...

  2. C# winform进度条 (异步)

    进度条页面: http://www.cnblogs.com/Deckard/archive/2009/06/24/1510451.html //============================ ...

  3. Winform中实现ZedGraph滚轮缩放后自动重新加载数据

    场景 Winforn中设置ZedGraph曲线图的属性.坐标轴属性.刻度属性: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/10 ...

  4. WinForm中使用BackgroundWorker异步加载数据并使用进度条

    在WinForm程序中,有时会因为加载大量数据导致UI界面假死,这种情况对于用户来说是非常不友好的.因此,在加载大量数据的情况下,首先应该将数据加载放在另一线程中进行,这样保证了UI界面的响应:其次可 ...

  5. WinForm中异步加载数据并使用进度条

    在WinForm程序中,有时会因为加载大量数据导致UI界面假死,这种情况对于用户来说是非常不友好的.因此,在加载大量数据的情况下,首先应该将数据加载放在另一线程中进行,这样保证了UI界面的响应:其次可 ...

  6. 在运行时切换 WinForm 程序的界面语言 System.ComponentModel.ComponentResourceManager .ApplyResources

    Download the code for this article: WinForm-Multilanguages-2.rar (11 KB). 方法二: 下面介绍一种只需对现有代码做较小改动的方法 ...

  7. C#中异步及winform中界面假死

    c#中可以用BeginInvoke去启动异步调用,但是有两个BeginInvoke一个是controller的BeginInvoke还有一个是委托的BeginInvoke. 主要区别是controll ...

  8. 【Winform】 无法将类型为“System.Windows.Forms.SplitContainer”的对象强制转换为类型“System.ComponentModel.ISupportInitialize”。

    问题:将dotnet framework 4.0 切换到2.0时,编译没有问题,在运行时出现如下错误:System.InvalidCastException: 无法将类型为“System.Window ...

  9. System.ComponentModel.DataAnnotations.Schema.TableAttribute 同时存在于EntityFramework.dll和System.ComponentModel.DataAnnotations.dll中

    Entity Framework 与 .net4.5 的 System.ComponentModel.DataAnnotations 都有 System.ComponentModel.DataAnno ...

随机推荐

  1. IBM AIX Shell编写遭遇错误一2

    在IBM AIX 5.3平台上,编写一个shell的时候遇到几个错误. 0. exp: 0403-027 The parameter list is too long 看这个提示是说命令行长度太长了, ...

  2. HDU5015 233 Matrix(矩阵高速幂)

    HDU5015 233 Matrix(矩阵高速幂) 题目链接 题目大意: 给出n∗m矩阵,给出第一行a01, a02, a03 ...a0m (各自是233, 2333, 23333...), 再给定 ...

  3. ubuntu 12.04英文版设置成中文版

    适用于ubuntu 12.04英文版的系统,其他版本号的设置应该是大同小异的. 进入ubuntu系统,在顶部齿状标志找到system... 2.在personal找到Language Support ...

  4. tab功能菜单——使用tab之间不同的交换机div

    需求:在web实现类型的接口tab标签效应 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvem91eXVqaWUxMTI3/font/5a6L5L2T/fo ...

  5. java名词,关键字

    抽象类:规定一个或多个抽象方法的类别本身必须定义为abstract,抽象类只是用来派生子类,而不能用它来创建对象. final类:又称“最终类”,它只能用来创建对象,而不能被继承,与抽象类刚好相反,而 ...

  6. Python学习入门基础教程(learning Python)--3.2 if-else分支语句

    if-else分支语句结构的特点是当conditon条件满足时,执行if下的语句块,当condition条件不满足时执行else下的语句块,也就是说根据条件来控制让某些语句执行,某些语句不被执行. i ...

  7. c# 通过解析mp3规范命名并上传服务器

    引用using Shell32; private void Open_Click(object sender, RoutedEventArgs e) { OpenFileDialog ofd = ne ...

  8. SpringMVC(转)

    http://www.cnblogs.com/liukemng/p/3725582.html

  9. CMD经常使用的命令

    Win7Excuting订单 win+R.运行该快捷方式.下面3一个人必须知道: ping 它是用来检查网络是否通畅或者网络连接速度的命令. 作为一个生活在网络上的管理员或者黑客来说,ping命令是第 ...

  10. poj 2369 Permutations 更换水称号

    寻找循环节求lcm够了,如果答案是12345应该输出1.这是下一个洞. #include<iostream> #include<cstdio> #include<cstr ...