为了防止操作过程中界面卡死,和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. 成为JAVA软件开发工程师要学哪些东西

    2010-04-22 15:34 提问者采纳 Java EE(旧称j2ee)   第一阶段:Java基础,包括java语法,面向对象特征,常见API,集合框架: *第二阶段:java界面编程,包括AW ...

  2. jstl的小问题

    jstl试了半天,终于知道错在哪里了! 这是jsp中的代码 从select中取得user_id:看清楚了 是user_id:小写第一位; <table width="50%" ...

  3. MYSQL经常使用命令列表

    MYSQL经常使用命令列表 1.系统管理 mysql -h主机地址 -uusername -p 连接MYSQL(在mysql/bin) exit 退出MYSQL命令 mysqladmin -uuser ...

  4. 策略模式——MFC样例

    Context(应用场景): 1.须要使用ConcreteStrategy提供的算法. 2.内部维护一个Strategy的实例. 3. 负责动态设置执行时Strategy详细的实现算法. 4.负责跟S ...

  5. sql server 常用语法

    --1 创建数据库 DROP DATABASE mydb1 CREATE DATABASE mydb1 ON ( NAME ='mydb1',FILENAME='D:\mydb1.mdf') LOG ...

  6. 深入浅出KnockoutJS

    深入浅出KnockoutJS 写在前面,本文资料大多来源网上,属于自己的学习笔记整理. 其中主要内容来自learn.knockoutjs.com,源码解析部分资料来自司徒正美博文<knockou ...

  7. windows phone (27) 基础Button

    原文:windows phone (27) 基础Button Button 在wp7中因其灵活性经常会用到,我们在ContentPanel中直接添加Button,button默认状态下是把整个grid ...

  8. 简述负载均衡&CDN技术(转)

    曾经见到知乎上有人问“为什么像facebook这类的网站需要上千个工程师维护?”,下面的回答多种多样,但总结起来就是:一个高性能的web系统需要从无数个角度去考虑他,大到服务器的布局,小到软件中某个文 ...

  9. Java并发学习之中的一个——线程的创建

    本文是学习网络上的文章时的总结,感谢大家无私的分享. 1.与每一个Java语言中的元素一样,线程是对象.在Java中,我们有两种方式创建线程: a.通过直接继承thread类,然后覆盖run方法. b ...

  10. Spring官方网站的改版后下载

    Spring官方网站改版很长一段时间后还没有找到直接下载Jar链接包,下面总结了一些方法,可在网上,亲測可用. 1.直接输入地址,改对应版本号就可以:http://repo.springsource. ...