日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
C#進度條實現(xiàn)之異步實例淺析

C#進度條實現(xiàn)之異步實例是如何展示C#進度條實現(xiàn)的呢?讓我們來看看:

10余年的醴陵網(wǎng)站建設經(jīng)驗,針對設計、前端、開發(fā)、售后、文案、推廣等六對一服務,響應快,48小時及時工作處理。全網(wǎng)營銷推廣的優(yōu)勢是能夠根據(jù)用戶設備顯示端的尺寸不同,自動調整醴陵建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設計,從而大程度地提升瀏覽體驗。創(chuàng)新互聯(lián)從事“醴陵網(wǎng)站設計”,“醴陵網(wǎng)站推廣”以來,每個客戶項目都認真落實執(zhí)行。

C#進度條實現(xiàn)之異步實例進度條頁面:

 
 
 
  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. //工作完成后執(zhí)行的事件
  31. public void OnProcessCompleted(object sender, EventArgs e)
  32. {
  33. this.Close();
  34. }
  35. //工作中執(zhí)行進度更新  ,C#進度條實現(xiàn)之異步實例
  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#進度條實現(xiàn)之異步實例主頁面:

 
 
 
  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#進度條實現(xiàn)之異步實例
  41. ProgressForm progressForm = new ProgressForm();
  42. progressForm.Show();
  43. // Prepare the background worker 
  44. //for asynchronous prime number calculation
  45. //準備進度條的記數(shù)
  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. //開啟線程執(zhí)行工作  ,C#進度條實現(xiàn)之異步實例
  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. //如果進度條需要參數(shù)
  90. //調用System.ComponentModel.BackgroundWorker.RunWorkerAsync
  91. //傳入你的參數(shù)至System.ComponentModel.BackgroundWorker.DoWork 
  92. //提取參數(shù)
  93. //System.ComponentModel.DoWorkEventArgs.Argument 
  94. worker.RunWorkerAsync(leftList);
  95. }
  96. //單線程執(zhí)行工作
  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. //添加數(shù)據(jù)至右邊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#進度條實現(xiàn)之異步實例
  135. //工作完成狀態(tài)
  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#進度條實現(xiàn)之異步實例的相關內容就向你介紹到這里,希望對你了解和學習C#進度條實現(xiàn)有所幫助。


網(wǎng)頁標題:C#進度條實現(xiàn)之異步實例淺析
鏈接地址:http://m.5511xx.com/article/cogcesh.html