本篇文章給大家分享的是有關(guān)C#中怎么利用異步實(shí)現(xiàn)一個(gè)進(jìn)度條效果,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
在金塔等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè) 網(wǎng)站設(shè)計(jì)制作定制網(wǎng)站建設(shè),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站建設(shè),成都全網(wǎng)營(yíng)銷推廣,外貿(mào)營(yíng)銷網(wǎng)站建設(shè),金塔網(wǎng)站建設(shè)費(fèi)用合理。
C#進(jìn)度條實(shí)現(xiàn)之異步實(shí)例進(jìn)度條頁面:
//==================================== // 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(); } //工作完成后執(zhí)行的事件 public void OnProcessCompleted(object sender, EventArgs e) { this.Close(); } //工作中執(zhí)行進(jìn)度更新 ,C#進(jìn)度條實(shí)現(xiàn)之異步實(shí)例public void OnProgressChanged(object sender, ProgressChangedEventArgs e) { progressWork.Value = e.ProgressPercentage; } private void btnClose_Click(object sender, EventArgs e) { Close(); } } }
C#進(jìn)度條實(shí)現(xiàn)之異步實(shí)例主頁面:
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 IListleftList = new BindingList (); private IList rightList = new BindingList (); 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) { //顯示進(jìn)度條 ,C#進(jìn)度條實(shí)現(xiàn)之異步實(shí)例ProgressForm progressForm = new ProgressForm(); progressForm.Show(); // Prepare the background worker //for asynchronous prime number calculation //準(zhǔn)備進(jìn)度條的記數(shù) worker= new BackgroundWorker(); // Specify that the background //worker provides progress notifications //指定提供進(jìn)度通知 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 //線程的主要功能是處理事件 //開啟線程執(zhí)行工作 ,C#進(jìn)度條實(shí)現(xiàn)之異步實(shí)例worker.DoWork += new DoWorkEventHandler(worker_DoWork); // Specify the function to use to handle progress //指定使用的功能來處理進(jìn)度 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 //進(jìn)度條結(jié)束完成工作 //1.工作完成 //2.工作錯(cuò)誤異常 //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. //如果進(jìn)度條需要參數(shù) //調(diào)用System.ComponentModel.BackgroundWorker.RunWorkerAsync //傳入你的參數(shù)至System.ComponentModel.BackgroundWorker.DoWork //提取參數(shù) //System.ComponentModel.DoWorkEventArgs.Argument worker.RunWorkerAsync(leftList); } //單線程執(zhí)行工作 private void worker_DoWork(object sender, DoWorkEventArgs e) { MoveList((BackgroundWorker)sender,e); } //進(jìn)行轉(zhuǎn)移工作 private void MoveList(BackgroundWorker worker,DoWorkEventArgs e) { IList list = e.Argument as IList ; 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); } } } //添加數(shù)據(jù)至右邊listBox private void worker_ProgressChanged( object sender, ProgressChangedEventArgs e) { //Add string to the right listBox rightList.Add(e.UserState as string); } //C#進(jìn)度條實(shí)現(xiàn)之異步實(shí)例//工作完成狀態(tài) 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..."; //掛起進(jìn)程 worker.CancelAsync(); } } //返回操作 private void moveBackButton_Click( object sender, EventArgs e) { foreach (string str in rightList) { leftList.Add(str); } rightList.Clear(); } } }
以上就是C#中怎么利用異步實(shí)現(xiàn)一個(gè)進(jìn)度條效果,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。