程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> c#異步操作後台運轉(backgroundworker類)示例

c#異步操作後台運轉(backgroundworker類)示例

編輯:C#入門知識

c#異步操作後台運轉(backgroundworker類)示例。本站提示廣大學習愛好者:(c#異步操作後台運轉(backgroundworker類)示例)文章只能為提供參考,不一定能成為您想要的結果。以下是c#異步操作後台運轉(backgroundworker類)示例正文


c#異步操作,BackgroundWorker類的應用,可以在後台運轉須要的代碼邏輯。


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace TestBackgroundWork
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
            InitializeBackgoundWorker();
        }


        private BackgroundWorker backgroundWorker1;

        private void InitializeBackgoundWorker()
        {
            this.backgroundWorker1 = new System.ComponentModel.BackgroundWorker();
            this.backgroundWorker1.WorkerReportsProgress = true;
            this.backgroundWorker1.WorkerSupportsCancellation = true;
            this.backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
            this.backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
            this.backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
        }

        //**********backgroundWorker1的回調函數**********
        void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;
            for (int i = 1; i <= 10; i++)
            {
                if (worker.CancellationPending)
                {
                   e.Cancel = true;
                }
                else
                {
                    Thread.Sleep(500);
                    int percentComplete = (int)((float)i / (float)10 * 100);
                    worker.ReportProgress(percentComplete);
                }
            }
        }

        void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            this.progressBar1.Value = e.ProgressPercentage;
        }

        void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                MessageBox.Show(e.Error.Message);
            }
            else if (e.Cancelled)
            {
                resultLabel.Text = "Canceled";
            }
            else
            {
                resultLabel.Text = "Completed";
            }
            startBtn.Enabled = true;
            stopBtn.Enabled = false;
        }

        //**********backgroundWorker1的回調函數**********


        private void startBtn_Click(object sender, EventArgs e)
        {
            resultLabel.Text = String.Empty;
            this.startBtn.Enabled = false;
            this.stopBtn.Enabled = true;
            //啟動異步操作.
            backgroundWorker1.RunWorkerAsync();
        }

        private void stopBtn_Click(object sender, EventArgs e)
        {
            backgroundWorker1.CancelAsync();
        }
    }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved