1、在Form中添加 指针控件:BackgroundWorker-bgwork;进度条控件progressBar1 以及开始、取消按钮

2、开始按钮启动异步线程

private void button1_Click(object sender, EventArgs e)

{

bgwork.WorkerReportsProgress = true; //允许异步控件报告状态

bgwork.WorkerSupportsCancellation = true;//允许终止

bgwork.RunWorkerAsync(new string[3] { "1", "2", "3" });//启动异步事件,其中可带任意参数e

}

3、执行异步函数

private void bgwork_DoWork(object sender, DoWorkEventArgs e)

{

for (int i = 1; i < 101; i++)

{

Thread.Sleep(100);

bgwork.ReportProgress(i); //触发bgwork_ProgressChanged事件,显示百分比i%

if (bgwork.CancellationPending) //随时监测有无中止此后台进程

{

e.Cancel = true;//异步线程主动报告终止线程

return;

}

}

}

private void bgwork_ProgressChanged(object sender, ProgressChangedEventArgs e)

{

progressBar1.Value = e.ProgressPercentage; //主界面上显示进度条

}

中止后台进程

private void button2_Click(object sender, EventArgs e)

{

bgwork.CancelAsync();  //主进程要求异步线程终止 此时bgwork.CancellationPending由false变为ture

}

异步进程中止和结构后触发事件

private void bgwork_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)

{

if (e.Cancelled) MessageBox.Show("取消");//异步线程终止后,主进程判断退出原因,是主动取消还是出错退出。

else MessageBox.Show(e.r);

}

2015.3.11 VS异步控件及进度条结合应用的更多相关文章

  1. MFC控件编程进度条编写

    MFC控件编程进度条编写 一丶进度条编程需要用到的方法 进度条MFC已经帮我们封装好类了. 叫做 CProgressCtrl  进度条编程也很简单. 封装的方法也就那个那几个. GetPos()  获 ...

  2. C# 根据BackgroundWoker异步模型和ProgressBar控件,自定义进度条控件

    前言 程序开发过程中,难免会有的业务逻辑,或者算法之类产生让人能够感知的耗时操作,例如循环中对复杂逻辑处理;获取数据库百万乃至千万级数据;http请求的时候等...... 用户在使用UI操作并不知道程 ...

  3. Android自己定义控件:进度条的四种实现方式

    前三种实现方式代码出自: http://stormzhang.com/openandroid/2013/11/15/android-custom-loading/ (源代码下载)http://down ...

  4. Winform之跨线程访问控件(在进度条上显示字体)

    此文章对于遇到必须使用线程但是没有办法在线程内操作控件的问题的处理  有很好的解决方案(个人认为的.有更好的方案欢迎交流.) 在做跨线程访问之前我们先了解下我们所做的需要达到的效果: 这个是批量的将x ...

  5. 怎样控制界面控件之进度条(ProgressBar)功能

    一.基础知识: 1.ProgressBar在界面文件XML中的布局: [html] <progressBar android:id="@+id/progressbar_updown&q ...

  6. Android中的常用控件之进度条(ProgressBar)

    ProgressBar的常用属性:style,进度条的样式,默认为圆形,用style="?android:attr/progressBarStyleHorizontal"可以将进度 ...

  7. Android基础控件ProgressBar进度条的使用

    1.简介 ProgressBar继承与View类,直接子类有AbsSeekBar和ContentLoadingProgressBar, 其中AbsSeekBar的子类有SeekBar和RatingBa ...

  8. 使用timer控件控制进度条

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

  9. Android:控件ProgressBar进度条

    各种进度条属于 ProgressBar的子类 设置style: 环形进度条   style="?android:attr/progressBarStyleLarge" 横向进度条, ...

随机推荐

  1. How to use Jenkins

    一.关键点 1.how to start the build server? do i need to start some app to do this? I don't believe so... ...

  2. eclipse添加删除插件-eclipse marketplace

    源文地址:http://jingyan.baidu.com/article/cdddd41c5c883353cb00e19e.html 在有些版本的eclips上并没有eclipse marketpl ...

  3. MySql基础学习-mysql安装

    Linux环境下的安装 1检查是否已经安装 sudo service mysql start #若未安装,则提示: mysql: unrecognized service 2安装MySql #安装 M ...

  4. 中文译文:Minerva-一种可扩展的高效的深度学习训练平台(Minerva - A Scalable and Highly Efficient Training Platform for Deep Learning)

    Minerva:一个可扩展的高效的深度学习训练平台 zoerywzhou@gmail.com http://www.cnblogs.com/swje/ 作者:Zhouwan  2015-12-1 声明 ...

  5. L121

    今天上午签字仪式的布置与该场合的严肃性非常协调.The setting for this morning's signing ceremony matched the solemnity of the ...

  6. BEC listen and translation exercise 7

    Maybe I ought to subscribe to the Engineering Quarterly.也许我应该订阅<工程学季刊>. The performance is sai ...

  7. New Concept English three (56)

    The river which forms the eastern boundary of our farm has always played an important part in our li ...

  8. 网络基础之网络协议篇---CS架构--网络通信--osi 协议---套接字socket--粘包

    1 C\S 客户端/服务器架构: .硬件 C/S架构 (打印机) .软件 C/S 架构 互联网中处处是C/S架构 如黄色网站是服务端,你的浏览器是客户端(B/S架构也是C/S架构的一种) 腾讯作为服务 ...

  9. C++中预定义的宏

    以下信息摘自与标准C++的文档中. 如果把这些宏加在程序的日志中,它将为开发人员进行问题分析提供了很好的帮助. standard c++ 1998版The following macro names ...

  10. LeetCode Beautiful Arrangement II

    原题链接在这里:https://leetcode.com/problems/beautiful-arrangement-ii/description/ 题目: Given two integers n ...