欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

c#異步操作后臺(tái)運(yùn)行(backgroundworker類)示例

 更新時(shí)間:2014年04月09日 10:06:19   作者:  
這篇文章主要介紹了c#異步操作后臺(tái)運(yùn)行(backgroundworker類)示例,需要的朋友可以參考下

c#異步操作,BackgroundWorker類的使用,可以在后臺(tái)運(yùn)行需要的代碼邏輯。

復(fù)制代碼 代碼如下:

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的回調(diào)函數(shù)**********
        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的回調(diào)函數(shù)**********


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

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

相關(guān)文章

  • C#高效反射調(diào)用方法類實(shí)例詳解

    C#高效反射調(diào)用方法類實(shí)例詳解

    在本篇文章中小編給大家分享的是關(guān)于C#高效反射調(diào)用方法類的相關(guān)實(shí)例內(nèi)容,有興趣的朋友們學(xué)習(xí)下。
    2019-07-07
  • C#實(shí)現(xiàn)六大設(shè)計(jì)原則之里氏替換原則

    C#實(shí)現(xiàn)六大設(shè)計(jì)原則之里氏替換原則

    這篇文章介紹了C#實(shí)現(xiàn)六大設(shè)計(jì)原則之里氏替換原則的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-02-02
  • C# ManualResetEvent使用方法詳解

    C# ManualResetEvent使用方法詳解

    這篇文章主要為大家詳細(xì)介紹了ManualResetEvent使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • C# 中閉包(Closure)詳解

    C# 中閉包(Closure)詳解

    這篇文章主要介紹了C# 中閉包(Closure)詳解的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • C#實(shí)現(xiàn)計(jì)算年齡的簡(jiǎn)單方法匯總

    C#實(shí)現(xiàn)計(jì)算年齡的簡(jiǎn)單方法匯總

    本文給大家分享的是C#代碼實(shí)現(xiàn)的簡(jiǎn)單實(shí)用的給出用戶的出生日期,計(jì)算出用戶的年齡的代碼,另外附上其他網(wǎng)友的方法,算是對(duì)計(jì)算年齡的一次小結(jié),希望大家能夠喜歡。
    2015-05-05
  • C#實(shí)現(xiàn)數(shù)獨(dú)解法

    C#實(shí)現(xiàn)數(shù)獨(dú)解法

    這篇文章介紹了C#實(shí)現(xiàn)數(shù)獨(dú)解法的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06
  • C# winform程序讀取文本中的值實(shí)例講解

    C# winform程序讀取文本中的值實(shí)例講解

    在本篇文章中小編給大家分享了關(guān)于C# winform程序讀取文本中的值的相關(guān)知識(shí)點(diǎn),有興趣的朋友們學(xué)習(xí)下。
    2019-06-06
  • C#類的成員之Field字段的使用

    C#類的成員之Field字段的使用

    本文主要介紹了C#類的成員之Field字段的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-05-05
  • C# memcache 使用介紹

    C# memcache 使用介紹

    這篇文章主要介紹了C# memcache 使用介紹,需要的朋友可以參考下
    2015-05-05
  • C#中ExecuteNonQuery()返回值注意點(diǎn)分析

    C#中ExecuteNonQuery()返回值注意點(diǎn)分析

    這篇文章主要介紹了C#中ExecuteNonQuery()返回值注意點(diǎn)分析,對(duì)于C#數(shù)據(jù)庫(kù)程序設(shè)計(jì)有很大的借鑒價(jià)值,需要的朋友可以參考下
    2014-08-08

最新評(píng)論