C#进度条实现之异步实例浅析

开发 后端
C#进度条实现之异步实例是如何实现的呢?C#进度条实现之异步实例需要注意的是什么呢?那么本文就向你介绍C#进度条实现之异步实例的具体事宜。

C#进度条实现之异步实例是如何展示C#进度条实现的呢?让我们来看看:

C#进度条实现之异步实例进度条页面:

  1. //====================================  
  2. // Microsoft patterns & practices  
  3. // CompositeUI Application Block  
  4. //====================================  
  5. // Copyright ?Microsoft Corporation.    
  6. //All rights reserved.  
  7. // THIS CODE AND INFORMATION IS   
  8. //PROVIDED "AS IS" WITHOUT WARRANTY  
  9. // OF ANY KIND, EITHER EXPRESSED OR   
  10. //IMPLIED, INCLUDING BUT NOT  
  11. // LIMITED TO THE IMPLIED WARRANTIES  
  12. // OF MERCHANTABILITY AND  
  13. // FITNESS FOR A PARTICULAR PURPOSE.  
  14. //=====================================  
  15.  
  16. using System;  
  17. using System.Collections.Generic;  
  18. using System.ComponentModel;  
  19. using System.Data;  
  20. using System.Drawing;  
  21. using System.Text;  
  22. using System.Windows.Forms;  
  23.  
  24.  
  25. namespace BackgroudWokerUI  
  26. {  
  27. public partial class ProgressForm : Form  
  28. {  
  29. public ProgressForm()  
  30. {  
  31. InitializeComponent();  
  32. }  
  33.  
  34. //工作完成后执行的事件  
  35. public void OnProcessCompleted(object sender, EventArgs e)  
  36. {  
  37. this.Close();  
  38. }  
  39.  
  40. //工作中执行进度更新  ,C#进度条实现之异步实例
  41. public void OnProgressChanged(
  42. object sender, ProgressChangedEventArgs e)  
  43. {  
  44. progressWork.Value = e.ProgressPercentage;  
  45. }  
  46.  
  47. private void btnClose_Click(object sender, EventArgs e)  
  48. {  
  49. Close();  
  50. }  
  51. }  

C#进度条实现之异步实例主页面:

  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.  
  10. //Note You must be careful not to manipulate any user-interface objects   
  11. //in your System.ComponentModel.BackgroundWorker.DoWork event handler.   
  12. //Instead, communicate to the user interface through the   
  13. //System.ComponentModel.BackgroundWorker.ProgressChanged and   
  14. //System.ComponentModel.BackgroundWorker.RunWorkerCompleted events.  
  15.  
  16. namespace BackgroudWokerUI  
  17. {  
  18. public partial class MainForm : Form  
  19. {  
  20. //BindingList is useful list for UI   
  21. private IList<string> leftList = new BindingList<string>();  
  22. private IList<string> rightList = new BindingList<string>();  
  23.  
  24. private BackgroundWorker worker = null;  
  25.  
  26. public MainForm()  
  27. {  
  28. InitializeComponent();  
  29. //Databinding here  
  30. listBox1.DataSource = leftList;  
  31. listBox2.DataSource = rightList;  
  32. }  
  33.  
  34. private void addButton_Click(object sender, EventArgs e)  
  35. {  
  36. if (textBox.Text.Length != 0)  
  37. {  
  38. leftList.Add(textBox.Text);  
  39. textBox.Text = "";  
  40. textBox.Focus();  
  41. }  
  42. }  
  43.  
  44. private void moveButton_Click(object sender, EventArgs e)  
  45. {  
  46. //显示进度条  ,C#进度条实现之异步实例
  47. ProgressForm progressForm = new ProgressForm();  
  48. progressForm.Show();  
  49.  
  50. // Prepare the background worker   
  51. //for asynchronous prime number calculation  
  52. //准备进度条的记数  
  53. worker= new BackgroundWorker();  
  54. // Specify that the background   
  55. //worker provides progress notifications    
  56. //指定提供进度通知  
  57. worker.WorkerReportsProgress = true;  
  58. // Specify that the background worker supports cancellation  
  59. //提供中断功能  
  60. worker.WorkerSupportsCancellation = true;  
  61. // The DoWork event handler is the main   
  62. //work function of the background thread  
  63. //线程的主要功能是处理事件  
  64. //开启线程执行工作  ,C#进度条实现之异步实例
  65. worker.DoWork += new DoWorkEventHandler(worker_DoWork);  
  66. // Specify the function to use to handle progress  
  67. //指定使用的功能来处理进度  
  68. worker.ProgressChanged +=   
  69. new ProgressChangedEventHandler(worker_ProgressChanged);  
  70. worker.ProgressChanged +=   
  71. new ProgressChangedEventHandler(progressForm.OnProgressChanged);  
  72. // Specify the function to run when the   
  73. //background worker finishes  
  74. // There are three conditions possible   
  75. //that should be handled in this function:  
  76. // 1. The work completed successfully  
  77. // 2. The work aborted with errors  
  78. // 3. The user cancelled the process  
  79. //进度条结束完成工作  
  80. //1.工作完成  
  81. //2.工作错误异常  
  82. //3.取消工作  
  83. worker.RunWorkerCompleted +=   
  84. new RunWorkerCompletedEventHandler(  
  85. worker_RunWorkerCompleted);  
  86. worker.RunWorkerCompleted+=  
  87. new RunWorkerCompletedEventHandler(  
  88. progressForm.OnProcessCompleted);  
  89.    
  90. //If your background operation requires a parameter,   
  91. //call System.ComponentModel.BackgroundWorker.RunWorkerAsync   
  92. //with your parameter. Inside   
  93. //the System.ComponentModel.BackgroundWorker.DoWork   
  94. //event handler, you can extract the parameter from the   
  95. //System.ComponentModel.DoWorkEventArgs.Argument property.  
  96. //如果进度条需要参数  
  97. //调用System.ComponentModel.BackgroundWorker.RunWorkerAsync  
  98. //传入你的参数至System.ComponentModel.BackgroundWorker.DoWork   
  99. //提取参数  
  100. //System.ComponentModel.DoWorkEventArgs.Argument   
  101. worker.RunWorkerAsync(leftList);  
  102. }  
  103.  
  104. //单线程执行工作  
  105. private void worker_DoWork(
  106. object sender, DoWorkEventArgs e)  
  107. {  
  108. MoveList((BackgroundWorker)sender,e);  
  109. }  
  110.  
  111. //进行转移工作  
  112. private void MoveList(
  113. BackgroundWorker worker,DoWorkEventArgs e)  
  114. {  
  115. IList<string> list = e.Argument as IList<string>;  
  116.  
  117. for (int i = 0; i < list.Count; i++)  
  118. {  
  119. // Check for cancellation  
  120. //检查取消  
  121. if (worker.CancellationPending)  
  122. {  
  123. e.Cancel = true;  
  124. break;  
  125. }  
  126. else 
  127. {  
  128. // This will be handled in the correct thread thanks to the   
  129. // internals of BackgroundWroker and AsyncOperation  
  130. worker.ReportProgress((i + 1) * (100 / list.Count), list[i]);  
  131. // Simulate some time consuming proccess.  
  132. //线程休眠  
  133. Thread.Sleep(500);  
  134. }  
  135. }  
  136. }  
  137. //添加数据至右边listBox  
  138. private void worker_ProgressChanged(  
  139. object sender, ProgressChangedEventArgs e)  
  140. {  
  141. //Add string to the right listBox  
  142. rightList.Add(e.UserState as string);  
  143. }  
  144.  //C#进度条实现之异步实例
  145. //工作完成状态  
  146. private void worker_RunWorkerCompleted(  
  147. object sender, RunWorkerCompletedEventArgs e)  
  148. {  
  149. if (e.Cancelled)  
  150. {  
  151. label.Text = "Cancelled!取消";  
  152. }  
  153. else if (e.Error != null)  
  154. {  
  155. label.Text = "Error!异常";  
  156. }  
  157. else 
  158. {  
  159. label.Text = "Success!完成";  
  160. leftList.Clear();  
  161. }  
  162. }  
  163. //取消中  
  164. private void cancelButton_Click(  
  165. object sender, EventArgs e)  
  166. {  
  167. if (worker.IsBusy)  
  168. {  
  169. label.Text = "Cancelling...";  
  170. //挂起进程  
  171. worker.CancelAsync();  
  172. }  
  173. }  
  174. //返回操作  
  175. private void moveBackButton_Click(  
  176. object sender, EventArgs e)  
  177. {  
  178. foreach (string str in rightList)  
  179. {  
  180. leftList.Add(str);  
  181. }  
  182. rightList.Clear();  
  183. }  
  184. }  

C#进度条实现之异步实例的相关内容就向你介绍到这里,希望对你了解和学习C#进度条实现有所帮助。

【编辑推荐】

  1. C#调用Windows API之调用格式浅析
  2. C#调用Windows API之参数类型浅析
  3. C#中调用Windows API之托管对象
  4. C#进度条的使用及开发浅析
  5. C#进度条使用之多线程应用浅析
责任编辑:仲衡 来源: 博客园
相关推荐

2009-08-17 15:48:47

C# WinForm进

2009-08-17 14:41:47

C#进度条实现

2009-08-18 09:49:00

C# listview

2009-08-17 17:15:48

C# 进度条效果

2009-08-17 15:05:41

C#进度条

2009-08-17 13:56:29

C#进度条的使用

2009-08-17 16:49:46

C#多线程控制

2009-08-17 14:08:33

C#进度条使用

2009-08-17 16:41:03

C#多线程控制

2011-07-05 15:16:00

QT 进度条

2009-08-27 14:01:41

C#进度条

2009-06-06 18:54:02

JSP编程进度条

2009-08-21 09:20:44

C#异步套接字

2009-08-17 13:34:02

C#异步操作

2009-08-21 10:13:02

C#异步初步

2009-08-21 11:24:16

C#异步调用

2023-12-11 17:15:05

应用开发波纹进度条ArkUI

2015-07-31 11:19:43

数字进度条源码

2009-08-17 16:29:56

C#多线程控制

2009-08-17 16:56:51

C#多线程控制进度条
点赞
收藏

51CTO技术栈公众号