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

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

10余年的醴陵网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。全网营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整醴陵建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联从事“醴陵网站设计”,“醴陵网站推广”以来,每个客户项目都认真落实执行。

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. using System;
  16. using System.Collections.Generic;
  17. using System.ComponentModel;
  18. using System.Data;
  19. using System.Drawing;
  20. using System.Text;
  21. using System.Windows.Forms;
  22. namespace BackgroudWokerUI
  23. {
  24. public partial class ProgressForm : Form
  25. {
  26. public ProgressForm()
  27. {
  28. InitializeComponent();
  29. }
  30. //工作完成后执行的事件
  31. public void OnProcessCompleted(object sender, EventArgs e)
  32. {
  33. this.Close();
  34. }
  35. //工作中执行进度更新  ,C#进度条实现之异步实例
  36. public void OnProgressChanged(
  37. object sender, ProgressChangedEventArgs e)
  38. {
  39. progressWork.Value = e.ProgressPercentage;
  40. }
  41. private void btnClose_Click(object sender, EventArgs e)
  42. {
  43. Close();
  44. }
  45. }
  46. }

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

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

网站标题:C#进度条实现之异步实例浅析
网页链接:http://www.shufengxianlan.com/qtweb/news8/65408.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联