进度条页面:

http://www.cnblogs.com/Deckard/archive/2009/06/24/1510451.html

//===============================================================================
// Microsoft patterns & practices
// CompositeUI Application Block
//===============================================================================
// Copyright ?Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BackgroudWokerUI
{
    public partial class ProgressForm : Form
    {
        public ProgressForm()
        {
            InitializeComponent();
        }

//工作完成后执行的事件
        public void OnProcessCompleted(object sender, EventArgs e)
        {
            this.Close();
        }

//工作中执行进度更新
        public void OnProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressWork.Value = e.ProgressPercentage;
        }

private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}

主页面:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

//Note You must be careful not to manipulate any user-interface objects
//in your System.ComponentModel.BackgroundWorker.DoWork event handler.
//Instead, communicate to the user interface through the
//System.ComponentModel.BackgroundWorker.ProgressChanged and
//System.ComponentModel.BackgroundWorker.RunWorkerCompleted events.

namespace BackgroudWokerUI
{
    public partial class MainForm : Form
    {
        //BindingList is useful list for UI
        private IList<string> leftList = new BindingList<string>();
        private IList<string> rightList = new BindingList<string>();

private BackgroundWorker worker = null;

public MainForm()
        {
            InitializeComponent();
            //Databinding here
            listBox1.DataSource = leftList;
            listBox2.DataSource = rightList;
        }

private void addButton_Click(object sender, EventArgs e)
        {
            if (textBox.Text.Length != 0)
            {
                leftList.Add(textBox.Text);
                textBox.Text = "";
                textBox.Focus();
            }
        }

private void moveButton_Click(object sender, EventArgs e)
        {
            //显示进度条
            ProgressForm progressForm = new ProgressForm();
            progressForm.Show();

// Prepare the background worker for asynchronous prime number calculation
            //准备进度条的记数
            worker= new BackgroundWorker();
            // Specify that the background worker provides progress notifications 
            //指定提供进度通知
            worker.WorkerReportsProgress = true;
            // Specify that the background worker supports cancellation
            //提供中断功能
            worker.WorkerSupportsCancellation = true;
            // The DoWork event handler is the main work function of the background thread
            //线程的主要功能是处理事件
            //开启线程执行工作
            worker.DoWork += new DoWorkEventHandler(worker_DoWork);
            // Specify the function to use to handle progress
            //指定使用的功能来处理进度
            worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);
            worker.ProgressChanged += new ProgressChangedEventHandler(progressForm.OnProgressChanged);
            // Specify the function to run when the background worker finishes
            // There are three conditions possible that should be handled in this function:
            // 1. The work completed successfully
            // 2. The work aborted with errors
            // 3. The user cancelled the process
            //进度条结束完成工作
            //1.工作完成
            //2.工作错误异常
            //3.取消工作
            worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
            worker.RunWorkerCompleted+=new RunWorkerCompletedEventHandler(progressForm.OnProcessCompleted);
                
            //If your background operation requires a parameter,
            //call System.ComponentModel.BackgroundWorker.RunWorkerAsync
            //with your parameter. Inside the System.ComponentModel.BackgroundWorker.DoWork
            //event handler, you can extract the parameter from the
            //System.ComponentModel.DoWorkEventArgs.Argument property.
            //如果进度条需要参数
            //调用System.ComponentModel.BackgroundWorker.RunWorkerAsync
            //传入你的参数至System.ComponentModel.BackgroundWorker.DoWork
            //提取参数
            //System.ComponentModel.DoWorkEventArgs.Argument
            worker.RunWorkerAsync(leftList);
        }

//单线程执行工作
        private void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            MoveList((BackgroundWorker)sender,e);
        }

//进行转移工作
        private void MoveList(BackgroundWorker worker,DoWorkEventArgs e)
        {
            IList<string> list = e.Argument as IList<string>;

for (int i = 0; i < list.Count; i++)
            {
                // Check for cancellation
                //检查取消
                if (worker.CancellationPending)
                {
                    e.Cancel = true;
                    break;
                }
                else
                {
                    // This will be handled in the correct thread thanks to the
                    // internals of BackgroundWroker and AsyncOperation
                    worker.ReportProgress((i + 1) * (100 / list.Count), list[i]);
                    // Simulate some time consuming proccess.
                    //线程休眠
                    Thread.Sleep(500);
                }
            }
        }
        //添加数据至右边listBox
        private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            //Add string to the right listBox
            rightList.Add(e.UserState as string);
        }

//工作完成状态
        private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Cancelled)
            {
                label.Text = "Cancelled!取消";
            }
            else if (e.Error != null)
            {
                label.Text = "Error!异常";
            }
            else
            {
                label.Text = "Success!完成";
                leftList.Clear();
            }
        }
        //取消中
        private void cancelButton_Click(object sender, EventArgs e)
        {
            if (worker.IsBusy)
            {
                label.Text = "Cancelling...";
                //挂起进程
                worker.CancelAsync();
            }
        }
        //返回操作
        private void moveBackButton_Click(object sender, EventArgs e)
        {
            foreach (string str in rightList)
            {
                leftList.Add(str);
            }
            rightList.Clear();
        }
    }
}

C# winform进度条 (异步)的更多相关文章

  1. 通过XmlHttpRequest实现带进度条异步下载文件

    本博文源自技术群的讨论,因为网上找不到实现这样效果的的代码,而我说没问题,可以实现,因此有人质疑我是否能做到,呵呵,现将我实现代码贴出如下,希望有兴趣的同学可以继续完善: 本代码仅做技术展现,请勿探讨 ...

  2. WinForm 进度条

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

  3. Android有进度条异步任务下载图片

    首先在AndroidMainifest中添加上网权限 ? 1 <uses-permission android:name="android.permission.INTERNET&qu ...

  4. winform进度条

    参考资料: http://www.cnblogs.com/zzy0471/archive/2010/12/12/1903602.html http://www.cnblogs.com/haogj/ar ...

  5. Unity3D研究院之异步加载游戏场景与异步加载游戏资源进度条

    Unity3D研究院之异步加载游戏场景与异步加载游戏资源进度条 异步任务相信大家应该不会陌生,那么本章内容MOMO将带领大家学习Unity中的一些异步任务.在同步加载游戏场景的时候通常会使用方法 Ap ...

  6. winform异步进度条LongTime

    winform异步进度条LongTime,运用到回调函数 定义事件的参数类: namespace LongTime.Business { // 定义事件的参数类 public class ValueE ...

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

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

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

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

  9. JS -- 异步加载进度条

    今天在博客园问答里面看到博友问道怎么实现Ajax异步加载产生进度条. 很好奇就自己写了一个. 展现效果: 1) 当点击Load的时候,模拟执行异步加载. 浏览器被遮挡. 进度条出现. 实现思路: 1. ...

随机推荐

  1. spring整合mybatis、hibernate、logback配置

    Spring整合配置Mybatis 1.配置数据源(连接数据库最基本的属性配置,如数据库url,账号,密码,和数据库驱动等最基本参数配置) <!-- 导入properties配置文件 --> ...

  2. BCGcontrolBar(四) ListCtrl 操作输出显示

    Ribbon 的效果主要为在可停靠面板上放置各种组件 具体操作为新建一个watchbar 然后在watchbar上加入 ListCtrl组件 效果如下 因为是在可停靠面板上生成的 因此可以随意拖动 具 ...

  3. 02-第一个Java程序

    学习java的第一个程序 记录自己的学习 记录自己的坚持 记录自己的梦想 public class Hello{ public static void main(String[] args) { Sy ...

  4. python学习笔记之一

    1.可以自己运行,也可以被import后调用 if __name__ == '__main__' main() 2.可变参数,关键字参数,命名关键字参数 可变参数和关键字参数 def f1(a, b, ...

  5. 第11章 拾遗5:IPv6和IPv4共存技术(3)_NAT-PT技术【全书完】

    6.4 NAT-PT (1)NAT-PT和NAT的差别 ①NAT-PT(附带协议转换的网络地址转换)技术秉承NAT技术的思想,但在原理方面大有不同. ②NAT-PT和NAT本质的区别在于应用场合的不同 ...

  6. vs code编辑器使用教程指南

    1.安装插件: 这里可以搜索到插件并安装. 2.修改快捷键或查找快捷键: 这里可以进行快捷键的查找和修改 3.进入引用文件: 点击f12,或者右击快捷键可以看到进入引用文件的快捷方法. 4.查看目录:

  7. AFNetWorking 上传功能使用及源码分析

    使用方法比较多,这里列举两种: 第一种: // 1. 使用AFHTTPSessionManager的接口 AFHTTPSessionManager *manager = [AFHTTPSessionM ...

  8. Java中同步的几种实现方式

    1.使用synchronized关键字修饰类或者代码块: 2.使用Volatile关键字修饰变量: 3.在类中加入重入锁. 代码示例: 非同步状态下: public static void main( ...

  9. linux环境下redis数据库的安装|配置|启动

    安装 下载:打开redis官方网站,推荐下载稳定版本(stable) 解压 tar zxvf redis-3.2.5.tar.gz 复制:推荐放到usr/local目录下 sudo mv -r red ...

  10. 安装配置fastDFS文件服务器 - Linux

    一.配置linux环境 1.新建虚拟机 把上次安装的CentOS7的文件复制一份,并改名 打开VM>打开虚拟机,选择刚才复制好的虚拟机,并启动.这样做的目的主要是为了保留一份最基础的母本,为了将 ...