C#异步编程-简单示例

一个Freamwork GUI程序,有一个进度条,两个按钮。程序主要展示了BackgroundWorker类的使用。

异步编程简单示例

BackgroundWorker对象的常用事件

  • bgWorker.DoWork += DoWork_Handler;
  • bgWorker.ProgressChanged += ProgressChanged_Handler;
  • bgWorker.RunWorkerCompleted += RunWorkerCompleted_Handler;

BackgroundWorker对象的常用属性

  • bgWorker.WorkerReportsProgress = true;
  • bgWorker.WorkerSupportsCancellation = true;
  • bgWorker.IsBusy
  • worker.CancellationPending

BackgroundWorker对象的常用方法(异步方法名命名约定以Async结尾)

  • bgWorker.RunWorkerAsync();
  • worker.ReportProgress(i * 10);
  • bgWorker.CancelAsync();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84

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

namespace book_study
{
    public partial class form1 : Form
    {
        BackgroundWorker bgWorker = new BackgroundWorker();

        public form1()
        {
            InitializeComponent();

            // set 属性
            bgWorker.WorkerReportsProgress = true;
            bgWorker.WorkerSupportsCancellation = true;

            // 连接方法,绑定事件
            bgWorker.DoWork += DoWork_Handler;
            bgWorker.ProgressChanged += ProgressChanged_Handler;
            bgWorker.RunWorkerCompleted += RunWorkerCompleted_Handler;
        }

        private void process_Click(object sender, EventArgs e)
        {
            if (!bgWorker.IsBusy)
            {
                bgWorker.RunWorkerAsync(); // method
            }
        }

        private void ProgressChanged_Handler(object sender,
            ProgressChangedEventArgs args)
        {
            progressBar1.Value = args.ProgressPercentage;
        }

        private void DoWork_Handler(object sender,DoWorkEventArgs args)
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            for(int i = 1; i <= 10; i++)
            {
                if (worker.CancellationPending)
                {
                    args.Cancel = true;
                    break;
                }
                else
                {
                    worker.ReportProgress(i * 10);// method
                    Thread.Sleep(500);
                }
            }
        }

        private void RunWorkerCompleted_Handler(object sender, RunWorkerCompletedEventArgs args)
        {
            progressBar1.Value = 0;
            if (args.Cancelled)
            {
                MessageBox.Show("process was cancelled.", "process cancelled");
            }
            else
            {
                MessageBox.Show("process completed normally.", "process completed");
            }
        }

        private void cancel_Click(object sender, EventArgs e)
        {
            bgWorker.CancelAsync();// method
        }
    }
}