Wait Control
来源:互联网 发布:观澜网络丁香园怎么样 编辑:程序博客网 时间:2024/05/16 02:21
现在写个小项目,项目中需要使用的异步处理大数据,往往在系统处理的过程中需要很长的时间等待,但是在系统处理过程中,窗体就如同死机一样。无法进行其他的操作,所以我想到使用多线程去处理这些事情。于是我翻阅了很多的资料,选择了微软带有的BackGroundWorker去处理,使用BackGroundWorker去异步处理长时间等待的方法。
1、CancellationPending 获取一个值,指示应用程序是否已请求取消后台操作。通过在DoWork事件中判断CancellationPending属性可以认定是否需要取消后台操作(也就是结束线程);
2、IsBusy 获取一个值,指示 BackgroundWorker 是否正在运行异步操作。程序中使用IsBusy属性用来确定后台操作是否正在使用中;
3、WorkerReportsProgress 获取或设置一个值,该值指示BackgroundWorker能否报告进度更新
4、WorkerSupportsCancellation 获取或设置一个值,该值指示 BackgroundWorker 是否支持异步取消。设置WorkerSupportsCancellation为true使得程序可以调用CancelAsync方法提交终止挂起的后台操作的请求;
重要方法:
1、CancelAsync 请求取消挂起的后台操作
2、RunWorkerAsync 开始执行后台操作
3、ReportProgress 引发ProgressChanged事件
重要事件:
1、DoWork 调用 RunWorkerAsync 时发生
2、ProgressChanged 调用 ReportProgress 时发生
3、RunWorkerCompleted 当后台操作已完成、被取消或引发异常时发生
下面是BackGroundWorker控件的介绍:
BackgroundWorker类中主要用到的有这列属性、方法和事件:
重要属性:1、CancellationPending 获取一个值,指示应用程序是否已请求取消后台操作。通过在DoWork事件中判断CancellationPending属性可以认定是否需要取消后台操作(也就是结束线程);
2、IsBusy 获取一个值,指示 BackgroundWorker 是否正在运行异步操作。程序中使用IsBusy属性用来确定后台操作是否正在使用中;
3、WorkerReportsProgress 获取或设置一个值,该值指示BackgroundWorker能否报告进度更新
4、WorkerSupportsCancellation 获取或设置一个值,该值指示 BackgroundWorker 是否支持异步取消。设置WorkerSupportsCancellation为true使得程序可以调用CancelAsync方法提交终止挂起的后台操作的请求;
重要方法:
1、CancelAsync 请求取消挂起的后台操作
2、RunWorkerAsync 开始执行后台操作
3、ReportProgress 引发ProgressChanged事件
重要事件:
1、DoWork 调用 RunWorkerAsync 时发生
2、ProgressChanged 调用 ReportProgress 时发生
3、RunWorkerCompleted 当后台操作已完成、被取消或引发异常时发生
根据BackGroundWorker的这些特性,我写了下面的控件:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WaitFormControl
{
/// <summary>
/// Wait Control
/// </summary>
public partial class WaitControl : Form
{
/// <summary>
/// Complete Event Handler
/// </summary>
private EventHandler _completeEventHandler = null;
/// <summary>
/// Initializes a new instance of the <see cref="WaitControl"/> class.
/// </summary>
public WaitControl(DoWorkEventHandler doWorkEvent, EventHandler completeEventHandler)
{
InitializeComponent();
this.backgroundWorkerWait.DoWork += doWorkEvent;
this._completeEventHandler += completeEventHandler;
}
/// <summary>
/// Handles the Click event of the buttonCancel control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
private void buttonCancel_Click(object sender, EventArgs e)
{
this.backgroundWorkerWait.CancelAsync();
this.Close();
}
/// <summary>
/// Handles the Shown event of the WaitControl control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
private void WaitControl_Shown(object sender, EventArgs e)
{
this.backgroundWorkerWait.RunWorkerAsync();
}
/// <summary>
/// Handles the RunWorkerCompleted event of the backgroundWorkerWait control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.ComponentModel.RunWorkerCompletedEventArgs"/> instance containing the event data.</param>
private void backgroundWorkerWait_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (this._completeEventHandler != null)
{
EventHandler handler = this._completeEventHandler;
handler(this, EventArgs.Empty);
}
this.Close();
}
}
}
使用过程就非常简单了:
private void backgroundWorkerWait_DoWork(object sender, DoWorkEventArgs e)
{
Thread.Sleep(5000);
}
private void button1_Click(object sender, EventArgs e)
{
WaitFormControl.WaitControl wait = new WaitFormControl.WaitControl(
backgroundWorkerWait_DoWork,
null
);
wait.ShowDialog();
}
现在发布一个简单的版本,还有许多要进行改进。
- Wait Control
- Wait
- wait
- wait
- wait()
- wait
- wait
- wait()
- wait
- wait()
- Control
- Control
- Control
- Control
- UNIX高级环境编程(9)进程控制(Process Control)- fork,vfork,僵尸进程,wait和waitpid
- Wait for
- wait&exec
- wait && waitpid
- 最近的郁闷!
- SQL Server中有没有类似Oracle中 的 DUAL 的表
- hibernate多对一关联映射
- JS常识:setTimeout和setInterval 的区别
- 通过正则实现Java记事本中查找和替换功能
- Wait Control
- Response.ContentType 控制输出文件类型(讨论下载文件问题)
- mysql语法必备
- asp.net下Response.ContentType类型汇总
- HelloWord
- 焦点图片轮换
- JTree右键菜单实现编辑、添加、删除节点
- gridview动态绑定dropdownlist
- sql中exist与in 的区别