System.ComponentModel.BackgroundWorker在WinForm中的异步使用
为了防止操作过程中界面卡死,和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中的异步使用的更多相关文章
- System.Web.Security 在winform中是什么命名空间呢
des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStorin ...
- C# winform进度条 (异步)
进度条页面: http://www.cnblogs.com/Deckard/archive/2009/06/24/1510451.html //============================ ...
- Winform中实现ZedGraph滚轮缩放后自动重新加载数据
场景 Winforn中设置ZedGraph曲线图的属性.坐标轴属性.刻度属性: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/10 ...
- WinForm中使用BackgroundWorker异步加载数据并使用进度条
在WinForm程序中,有时会因为加载大量数据导致UI界面假死,这种情况对于用户来说是非常不友好的.因此,在加载大量数据的情况下,首先应该将数据加载放在另一线程中进行,这样保证了UI界面的响应:其次可 ...
- WinForm中异步加载数据并使用进度条
在WinForm程序中,有时会因为加载大量数据导致UI界面假死,这种情况对于用户来说是非常不友好的.因此,在加载大量数据的情况下,首先应该将数据加载放在另一线程中进行,这样保证了UI界面的响应:其次可 ...
- 在运行时切换 WinForm 程序的界面语言 System.ComponentModel.ComponentResourceManager .ApplyResources
Download the code for this article: WinForm-Multilanguages-2.rar (11 KB). 方法二: 下面介绍一种只需对现有代码做较小改动的方法 ...
- C#中异步及winform中界面假死
c#中可以用BeginInvoke去启动异步调用,但是有两个BeginInvoke一个是controller的BeginInvoke还有一个是委托的BeginInvoke. 主要区别是controll ...
- 【Winform】 无法将类型为“System.Windows.Forms.SplitContainer”的对象强制转换为类型“System.ComponentModel.ISupportInitialize”。
问题:将dotnet framework 4.0 切换到2.0时,编译没有问题,在运行时出现如下错误:System.InvalidCastException: 无法将类型为“System.Window ...
- System.ComponentModel.DataAnnotations.Schema.TableAttribute 同时存在于EntityFramework.dll和System.ComponentModel.DataAnnotations.dll中
Entity Framework 与 .net4.5 的 System.ComponentModel.DataAnnotations 都有 System.ComponentModel.DataAnno ...
随机推荐
- C# 使用Tuple传递多个参数
Tuple是基于.NET Framework 4.0 及以上版本才有的.微软称它为元组,如果有三个参数那就是三元组.如 Tuple(T1, T2, T3) Tuple的命名空间在 System 很短吧 ...
- shufe前辈名师
前辈名师 姓名 现职/原职 郭秉文 中国现代大学之父.国立东南大学校长.哥伦比亚大学教育学博士,该校第一任校长.为了纪念郭秉文先生,勉励优秀学子,郭夏瑜女士在上海财经大学等校设立了“郭秉文奖学金” 马 ...
- [置顶] ios 时间定时器 NSTimer应用demo
原创文章,转载请注明出处:http://blog.csdn.net/donny_zhang/article/details/9251917 demo功能:ios NSTimer应用demo .ipho ...
- 一篇哥们自己的写的IBM电话面试感想-@大国
两天没写博了,还是没有养成一个习惯.前天和昨天休息,和哥们几个出去打球,运动一下,放松下脑子.今天就补一篇我哥们自己的写的关于他的IBM电话面试的感想,希望能帮到有需要的人. ------------ ...
- fragment android
在Eoe中看到了 一个关于的 详细讲解,相信对 学Fragment 有帮助 android fragment基础与源码案例: Fragment动画效果 http://www.eoeandroid.co ...
- PHP+Mysql————表单数据插入数据库及数据提取
站点在进行新用户注冊时,都会将用户的注冊信息存入数据库中,须要的时候再进行提取.今天写了一个简单的实例. 主要完毕下面几点功能: (1)用户进行注冊,实现password反复确认,验证码校对功能. ( ...
- android 如何分析java.lang.IllegalArgumentException: Cannot draw recycled bitmaps异常
这类问题的分析,通常你需要找到bitmap对象已经在那个位置recyle,然后检查代码. 如何定位的位置,其中代码具有对bitmap 目的recyle.能够 Bitmap.java的recycle方法 ...
- WindowsPhone 在 根据公历 获取月球日期数据
WindowsPhone 在 根据公历 获取月球日期数据 WindowsPhone 在 它们的定义 类,根据公历 获取月球日期数据 using System; using System.Collect ...
- 数据结构:Binary and other trees(数据结构,算法及应用(C++叙事描述语言)文章8章)
8.1 Trees -->root,children, parent, siblings, leaf; level, degree of element 的基本概念 8.2 Binary Tre ...
- 判断闰年(go语言版本)
import "strconv" func IsLeapYear(y string) bool { //y == 2000, 2004 //判断是否为闰年 year, _ := s ...