zoukankan      html  css  js  c++  java
  • C# BackgroundWorker

    示范:

    using System.Windows;
    using System.Threading;
    using System.ComponentModel;
    
    namespace WpfApp1
    {
        /// <summary>
        /// MainWindow.xaml 的交互逻辑
        /// </summary>
        public partial class MainWindow : Window
        {
    
            private BackgroundWorker bw = new BackgroundWorker(); //声明BackgroundWorker对象
    
            public MainWindow()
            {
                InitializeComponent();
    
                InitBW();
    
            }
    
            /// <summary>
            /// 初始化
            /// </summary>
            private void InitBW()
            {
                //属性
                bw.WorkerReportsProgress = true;
                bw.WorkerSupportsCancellation = true;
    
                //事件
                bw.DoWork += Bw_DoWork;
                bw.ProgressChanged += Bw_ProgressChanged;
                bw.RunWorkerCompleted += Bw_RunWorkerCompleted;
            }
    
            private void Bw_DoWork(object sender, DoWorkEventArgs e)
            {
                //string a = e.Argument as string;
    
                for (int i = 0; i < 100; i++)
                {
                    //当接收到取消指令时
                    if (bw.CancellationPending)
                    {
                        e.Cancel = true;
                        return;
                    }
                    else
                    {
                        bw.ReportProgress(i, "Working"); //调用ReportProgress方法
                        Thread.Sleep(10);
                    }
                }
            }
    
            private void Bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
            {
                pro1.Value = e.ProgressPercentage;
                txt1.Text = e.ProgressPercentage.ToString() + "%";
                txt2.Text = e.UserState.ToString();
            }
    
            private void Bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
            {
                //是否出现错误
                if (e.Error != null)
                {
                    MessageBox.Show(e.Error.ToString());
                    return;
                }
    
                //是否为用户取消
                if (e.Cancelled)
                {
                    txt1.Text = "终止";
                    txt2.Text = "Worked";
                }
                else
                {
                    txt1.Text = "完毕";
                    txt2.Text = "Worked";
                }
            }
    
    
            //启动
            private void btnStart_Click(object sender, RoutedEventArgs e)
            {
                //是否正在执行一个异步操作
                if (bw.IsBusy)
                    return;
    
                pro1.Maximum = 100;
                btnStart.IsEnabled = false;
                btnStop.IsEnabled = true;
                bw.RunWorkerAsync("hello world");
    
            }
    
            //停止
            private void btnStop_Click(object sender, RoutedEventArgs e)
            {
                bw.CancelAsync();
                btnStart.IsEnabled = true;
                btnStop.IsEnabled = false;
            }
    
        }
    }

     

     

     

    C#backgroundWorker用法

     

    1、在 WinForms 中,有时要执行耗时的操作,在该操作未完成之前操作用户界面,会导致用户界面停止响应。解决的方法就是新开一个线程,把耗时的操作放到线程中执行,这样就可以在用户界面上进行其它操作。新建线程可以用 Thread 类,可以实现多线程同时操作。简单的方法可以通过 BackgroundWorker 类实现。

    BackgroundWorker 可以用来更新UI界面,但是通常用来Progressbar(进度条)控件

    例如更新UI

    复制代码
    private void Form1_Load(object sender, EventArgs e)
    {
          this.backgroundWorker1.RunWorkerAsync();
    }
    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
           this.textBox1.Text = "UI";
    }
    复制代码

    用 BackgroundWorker 类执行耗时的操作
    BackgroundWorker 类在 System.ComponentModel 命名空间下。
    VS 的工具箱时有一个 BackgroundWorker 组件,就是这个类。

    backgroundwork主要的事件及参数:
            1.DoWork——当执行BackgroundWorker.RunWorkerAsync方法时会触发该事件,并且传递DoWorkEventArgs参数;

            2.RunWorkerCompleted——异步操作完成或中途终止会触发该事件。

            3.ProgressChanged——操作处理中获得的处理状态变化,通过BackgroundWorker.ReportProgress(int)方法触发该事件,并且传递ProgressChangedEventArgs,其中包含了处理的百分比,这个参数在UI界面上设置progressbar控件。   

    backgroundwork主要的方法:
            1. BackgroundWorker.RunWorkerAsync——“起动”异步调用的方法有两次重载RunWorkerAsync(),RunWorkerAsync(object argument),第二个重载提供了一个参数,可以供异步调用使用。(如果有多个参数要传递怎么办,使用一个类来传递他们吧)。调用该方法后会触发DoWork事件。并且为处理DoWork事件的函数传递DoWorkEventArg参数,其中包含了RunWorkerAsync传递的参数。在相应DoWork的处理函数中就可以做具体的复杂操作。

           2. BackgroundWorker.ReportProgress——需要在一个冗长的操作中向用户不断反馈进度,这样的话就可以调用的ReportProgress(int percent),在调用 ReportProgress 方法时,触发ProgressChanged事件。提供一个在 0 到 100 之间的整数,它表示后台活动已完成的百分比。你也可以提供任何对象作为第二个参数,允许你 给事件处理程序传递状态信息。作为传递到此过程的 ProgressChangedEventArgs 参数属性,百分比和你自己的对象(如果提供的话)均要被传递到 ProgressChanged 事件处理程序。这些属性被分别命名为 ProgressPercentage 和 UserState,并且你的事件处理程序可以以任何需要的方式使用它们。(注意:只有在BackgroundWorker.WorkerReportsProgress属性被设置为true该方法才可用)。

          3. BackgroundWorker.CancelAsync——但需要退出异步调用的时候,就调用的这个方法。但是样还不够,因为它仅仅是将BackgroudWorker.CancellationPending属性设置为true。你需要在具体的异步调用处理的时候,不断检查BackgroudWorker.CancellationPending是否为true,如果是真的话就退出。(注意:只有在BackgroundWorker.WorkerSupportsCancellation属性被设置为true该方法才可用)。

    BackgroundWorker组件
    重要属性:
        1、CancellationPending         获取一个值,指示应用程序是否已请求取消后台操作。通过在DoWork事件中判断CancellationPending属性可以认定是否需要取消后台操作(也就是结束线程);
        2、 WorkerReportsProgress :获取或设置一个值,该值指示BackgroundWorker能否报告进度更新

    重要方法:
        1、CancelAsync         请求取消挂起的后台操作
        2、RunWorkerAsync      开始执行后台操作
        3、ReportProgress      引发ProgressChanged事件  

    重要事件:  
        1、DoWork              调用 RunWorkerAsync 时发生
        2、ProgressChanged     调用 ReportProgress 时发生
        3、RunWorkerCompleted  当后台操作已完成、被取消或引发异常时发生
        另外还有三个重要的参数是RunWorkerCompletedEventArgs以及DoWorkEventArgs、ProgressChangedEventArgs。
    例子:

    复制代码
    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.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace Delegate3
    {
        public partial class Form1 : Form
        {
    
            public Form1()
            {
                InitializeComponent();
            }
            
            private void button1_Click(object sender, EventArgs e)
            {
                //启动异步调用方法
                //调用RunWorkerAsync()方法,会触发DoWork事件
                this.backgroundWorker2.RunWorkerAsync();
            }
    
            private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
            {
                backgroundWorker2.WorkerReportsProgress = true;
                for (int i = 1; i <= 100; i++)
                {
                    //这里判断一下是否用户要求取消后台进行,并可以尽早退出。
                    //可以通过调用CancelAsync方法设置CancellationPending的值为false
                    if (backgroundWorker2.CancellationPending)
                    {
                        backgroundWorker2.ReportProgress(i, String.Format("{0}%,操作被用户申请中断", i));
                    }
    
                    //调用 ReportProgress 方法,会触发ProgressChanged事件
                    backgroundWorker2.ReportProgress(i, String.Format("{0}%", i));
                    System.Threading.Thread.Sleep(10);
                }
            }
    
            private void backgroundWorker2_ProgressChanged(object sender, ProgressChangedEventArgs e)
            {
                this.progressBar1.Value = e.ProgressPercentage;
                this.label1.Text = e.UserState.ToString();
                this.label1.Update();
            }
    
            private void backgroundWorker2_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
            {
                //这是结束后做的事情
                MessageBox.Show("完成");
            }
        }
    }
    复制代码

    using System.Windows;using System.Threading;using System.ComponentModel;
    namespace WpfApp1{/// <summary>/// MainWindow.xaml 的交互逻辑/// </summary>public partial class MainWindow : Window{
    private BackgroundWorker bw = new BackgroundWorker(); //声明BackgroundWorker对象
    public MainWindow(){InitializeComponent();
    InitBW();
    }
    /// <summary>/// 初始化/// </summary>private void InitBW(){//属性bw.WorkerReportsProgress = true;bw.WorkerSupportsCancellation = true;
    //事件bw.DoWork += Bw_DoWork;bw.ProgressChanged += Bw_ProgressChanged;bw.RunWorkerCompleted += Bw_RunWorkerCompleted;}
    private void Bw_DoWork(object sender, DoWorkEventArgs e){//string a = e.Argument as string;
    for (int i = 0; i < 100; i++){//当接收到取消指令时if (bw.CancellationPending){e.Cancel = true;return;}else{bw.ReportProgress(i, "Working"); //调用ReportProgress方法Thread.Sleep(10);}}}
    private void Bw_ProgressChanged(object sender, ProgressChangedEventArgs e){pro1.Value = e.ProgressPercentage;txt1.Text = e.ProgressPercentage.ToString() + "%";txt2.Text = e.UserState.ToString();}
    private void Bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e){//是否出现错误if (e.Error != null){MessageBox.Show(e.Error.ToString());return;}
    //是否为用户取消if (e.Cancelled){txt1.Text = "终止";txt2.Text = "Worked";}else{txt1.Text = "完毕";txt2.Text = "Worked";}}

    //启动private void btnStart_Click(object sender, RoutedEventArgs e){//是否正在执行一个异步操作if (bw.IsBusy)return;
    pro1.Maximum = 100;btnStart.IsEnabled = false;btnStop.IsEnabled = true;bw.RunWorkerAsync("hello world");
    }
    //停止private void btnStop_Click(object sender, RoutedEventArgs e){bw.CancelAsync();btnStart.IsEnabled = true;btnStop.IsEnabled = false;}
    }}

  • 相关阅读:
    CCCC 2020 酱油记
    CCPC 2020 威海 滚粗记
    IEEExtreme 2020 酱油记
    CCSP 2020 酱油记
    ICPC 陕西省赛 2020 游记
    CCPC 网络赛 2020 自闭记
    CSP 第20次认证 酱油记
    CSP-S 2019 酱油记
    NOI2019 退役记
    树链剖分入门
  • 原文地址:https://www.cnblogs.com/micro-chen/p/13848036.html
Copyright © 2011-2022 走看看