欢迎来到代码驿站!

.NET代码

当前位置:首页 > 软件编程 > .NET代码

c#异步操作后台运行(backgroundworker类)示例

时间:2022-01-11 10:30:12|栏目:.NET代码|点击:

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();
        }
    }
}

上一篇:asp.net 无刷新翻页就是这么简单

栏    目:.NET代码

下一篇:基于NVelocity的几种内容生成方式汇总

本文标题:c#异步操作后台运行(backgroundworker类)示例

本文地址:http://www.codeinn.net/misctech/189807.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有