六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 560|回复: 0

C# winform进度条 (异步)

[复制链接]
 楼主| 发表于 2013-2-22 18:27:52 | 显示全部楼层 |阅读模式
进度条页面:
  1. //===============================================================================
  2. // Microsoft patterns & practices
  3. // CompositeUI Application Block
  4. //===============================================================================
  5. // Copyright ?Microsoft Corporation.  All rights reserved.
  6. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
  7. // OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
  8. // LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  9. // FITNESS FOR A PARTICULAR PURPOSE.
  10. //===============================================================================

  11. using System;
  12. using System.Collections.Generic;
  13. using System.ComponentModel;
  14. using System.Data;
  15. using System.Drawing;
  16. using System.Text;
  17. using System.Windows.Forms;


  18. namespace BackgroudWokerUI
  19. {
  20.     public partial class ProgressForm : Form
  21.     {
  22.         public ProgressForm()
  23.         {
  24.             InitializeComponent();
  25.         }

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

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

  36.         private void btnClose_Click(object sender, EventArgs e)
  37.         {
  38.             Close();
  39.         }
  40.     }
  41. }
复制代码
主页面:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Threading;

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

  14. namespace BackgroudWokerUI
  15. {
  16.     public partial class MainForm : Form
  17.     {
  18.         //BindingList is useful list for UI
  19.         private IList<string> leftList = new BindingList<string>();
  20.         private IList<string> rightList = new BindingList<string>();

  21.         private BackgroundWorker worker = null;

  22.         public MainForm()
  23.         {
  24.             InitializeComponent();
  25.             //Databinding here
  26.             listBox1.DataSource = leftList;
  27.             listBox2.DataSource = rightList;
  28.         }

  29.         private void addButton_Click(object sender, EventArgs e)
  30.         {
  31.             if (textBox.Text.Length != 0)
  32.             {
  33.                 leftList.Add(textBox.Text);
  34.                 textBox.Text = "";
  35.                 textBox.Focus();
  36.             }
  37.         }

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

  43.             // Prepare the background worker for asynchronous prime number calculation
  44.             //准备进度条的记数
  45.             worker= new BackgroundWorker();
  46.             // Specify that the background worker provides progress notifications
  47.             //指定提供进度通知
  48.             worker.WorkerReportsProgress = true;
  49.             // Specify that the background worker supports cancellation
  50.             //提供中断功能
  51.             worker.WorkerSupportsCancellation = true;
  52.             // The DoWork event handler is the main work function of the background thread
  53.             //线程的主要功能是处理事件
  54.             //开启线程执行工作
  55.             worker.DoWork += new DoWorkEventHandler(worker_DoWork);
  56.             // Specify the function to use to handle progress
  57.             //指定使用的功能来处理进度
  58.             worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);
  59.             worker.ProgressChanged += new ProgressChangedEventHandler(progressForm.OnProgressChanged);
  60.             // Specify the function to run when the background worker finishes
  61.             // There are three conditions possible that should be handled in this function:
  62.             // 1. The work completed successfully
  63.             // 2. The work aborted with errors
  64.             // 3. The user cancelled the process
  65.             //进度条结束完成工作
  66.             //1.工作完成
  67.             //2.工作错误异常
  68.             //3.取消工作
  69.             worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
  70.             worker.RunWorkerCompleted+=new RunWorkerCompletedEventHandler(progressForm.OnProcessCompleted);
  71.                
  72.             //If your background operation requires a parameter,
  73.             //call System.ComponentModel.BackgroundWorker.RunWorkerAsync
  74.             //with your parameter. Inside the System.ComponentModel.BackgroundWorker.DoWork
  75.             //event handler, you can extract the parameter from the
  76.             //System.ComponentModel.DoWorkEventArgs.Argument property.
  77.             //如果进度条需要参数
  78.             //调用System.ComponentModel.BackgroundWorker.RunWorkerAsync
  79.             //传入你的参数至System.ComponentModel.BackgroundWorker.DoWork
  80.             //提取参数
  81.             //System.ComponentModel.DoWorkEventArgs.Argument
  82.             worker.RunWorkerAsync(leftList);
  83.         }

  84.         //单线程执行工作
  85.         private void worker_DoWork(object sender, DoWorkEventArgs e)
  86.         {
  87.             MoveList((BackgroundWorker)sender,e);
  88.         }

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

  93.             for (int i = 0; i < list.Count; i++)
  94.             {
  95.                 // Check for cancellation
  96.                 //检查取消
  97.                 if (worker.CancellationPending)
  98.                 {
  99.                     e.Cancel = true;
  100.                     break;
  101.                 }
  102.                 else
  103.                 {
  104.                     // This will be handled in the correct thread thanks to the
  105.                     // internals of BackgroundWroker and AsyncOperation
  106.                     worker.ReportProgress((i + 1) * (100 / list.Count), list[i]);
  107.                     // Simulate some time consuming proccess.
  108.                     //线程休眠
  109.                     Thread.Sleep(500);
  110.                 }
  111.             }
  112.         }
  113.         //添加数据至右边listBox
  114.         private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
  115.         {
  116.             //Add string to the right listBox
  117.             rightList.Add(e.UserState as string);
  118.         }

  119.         //工作完成状态
  120.         private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  121.         {
  122.             if (e.Cancelled)
  123.             {
  124.                 label.Text = "Cancelled!取消";
  125.             }
  126.             else if (e.Error != null)
  127.             {
  128.                 label.Text = "Error!异常";
  129.             }
  130.             else
  131.             {
  132.                 label.Text = "Success!完成";
  133.                 leftList.Clear();
  134.             }
  135.         }
  136.         //取消中
  137.         private void cancelButton_Click(object sender, EventArgs e)
  138.         {
  139.             if (worker.IsBusy)
  140.             {
  141.                 label.Text = "Cancelling...";
  142.                 //挂起进程
  143.                 worker.CancelAsync();
  144.             }
  145.         }
  146.         //返回操作
  147.         private void moveBackButton_Click(object sender, EventArgs e)
  148.         {
  149.             foreach (string str in rightList)
  150.             {
  151.                 leftList.Add(str);
  152.             }
  153.             rightList.Clear();
  154.         }
  155.     }
  156. }
复制代码
该会员没有填写今日想说内容.
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表