• BackgroundWorker 类-如何:在后台下载文件


    BackgroundWorker 类

    1.事件类型

    在这里插入图片描述

    2.说明

    若要尝试此代码,请创建Windows 窗体应用程序。 添加一个名为resultLabel的控件,并添加两Button个名为LabelstartAsyncButton和 cancelAsyncButton. 为这两个按钮创建 Click 事件处理程序。 在工具箱的 “组件 ”选项卡中,添加 BackgroundWorker 名为 “ backgroundWorker1. Create DoWork, ProgressChanged, and RunWorkerCompleted event handlers for the BackgroundWorker. 在表单的代码中,将现有代码替换为以下代码。

    3.案例源代码

    在这里插入图片描述

    C#

    using System;
    using System.ComponentModel;
    using System.Windows.Forms;
    
    namespace BackgroundWorkerSimple
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                backgroundWorker1.WorkerReportsProgress = true;
                backgroundWorker1.WorkerSupportsCancellation = true;
            }
    
            private void startAsyncButton_Click(object sender, EventArgs e)
            {
                if (backgroundWorker1.IsBusy != true)
                {
                    // Start the asynchronous operation.
                    backgroundWorker1.RunWorkerAsync();
                }
            }
    
            private void cancelAsyncButton_Click(object sender, EventArgs e)
            {
                if (backgroundWorker1.WorkerSupportsCancellation == true)
                {
                    // Cancel the asynchronous operation.
                    backgroundWorker1.CancelAsync();
                }
            }
    
            // This event handler is where the time-consuming work is done.
            private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
            {
                BackgroundWorker worker = sender as BackgroundWorker;
    
                for (int i = 1; i <= 10; i++)
                {
                    if (worker.CancellationPending == true)
                    {
                        e.Cancel = true;
                        break;
                    }
                    else
                    {
                        // Perform a time consuming operation and report progress.
                        System.Threading.Thread.Sleep(500);
                        worker.ReportProgress(i * 10);
                    }
                }
            }
    
            // This event handler updates the progress.
            private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
            {
                resultLabel.Text = (e.ProgressPercentage.ToString() + "%");
            }
    
            // This event handler deals with the results of the background operation.
            private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
            {
                if (e.Cancelled == true)
                {
                    resultLabel.Text = "Canceled!";
                }
                else if (e.Error != null)
                {
                    resultLabel.Text = "Error: " + e.Error.Message;
                }
                else
                {
                    resultLabel.Text = "Done!";
                }
            }
        }
    }
    
    • 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

    4. 该操作计算所选的 Fibonacci 数,在计算继续进行时报告进度更新,并允许取消挂起的计算。

    在这里插入图片描述

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Drawing;
    using System.Threading;
    using System.Windows.Forms;
    
    namespace BackgroundWorkerExample
    {	
        public class FibonacciForm : System.Windows.Forms.Form
        {	
            private int numberToCompute = 0;
            private int highestPercentageReached = 0;
    
            private System.Windows.Forms.NumericUpDown numericUpDown1;
            private System.Windows.Forms.Button startAsyncButton;
            private System.Windows.Forms.Button cancelAsyncButton;
            private System.Windows.Forms.ProgressBar progressBar1;
            private System.Windows.Forms.Label resultLabel;
            private System.ComponentModel.BackgroundWorker backgroundWorker1;
    
            public FibonacciForm()
            {	
                InitializeComponent();
    
                InitializeBackgroundWorker();
            }
    
            // Set up the BackgroundWorker object by 
            // attaching event handlers. 
            private void InitializeBackgroundWorker()
            {
                backgroundWorker1.DoWork += 
                    new DoWorkEventHandler(backgroundWorker1_DoWork);
                backgroundWorker1.RunWorkerCompleted += 
                    new RunWorkerCompletedEventHandler(
                backgroundWorker1_RunWorkerCompleted);
                backgroundWorker1.ProgressChanged += 
                    new ProgressChangedEventHandler(
                backgroundWorker1_ProgressChanged);
            }
        
            private void startAsyncButton_Click(System.Object sender, 
                System.EventArgs e)
            {
                // Reset the text in the result label.
                resultLabel.Text = String.Empty;
    
                // Disable the UpDown control until 
                // the asynchronous operation is done.
                this.numericUpDown1.Enabled = false;
    
                // Disable the Start button until 
                // the asynchronous operation is done.
                this.startAsyncButton.Enabled = false;
    
                // Enable the Cancel button while 
                // the asynchronous operation runs.
                this.cancelAsyncButton.Enabled = true;
    
                // Get the value from the UpDown control.
                numberToCompute = (int)numericUpDown1.Value;
    
                // Reset the variable for percentage tracking.
                highestPercentageReached = 0;
    
                // Start the asynchronous operation.
                backgroundWorker1.RunWorkerAsync(numberToCompute);
            }
    
            private void cancelAsyncButton_Click(System.Object sender, 
                System.EventArgs e)
            {   
                // Cancel the asynchronous operation.
                this.backgroundWorker1.CancelAsync();
    
                // Disable the Cancel button.
                cancelAsyncButton.Enabled = false;
            }
    
            // This event handler is where the actual,
            // potentially time-consuming work is done.
            private void backgroundWorker1_DoWork(object sender, 
                DoWorkEventArgs e)
            {   
                // Get the BackgroundWorker that raised this event.
                BackgroundWorker worker = sender as BackgroundWorker;
    
                // Assign the result of the computation
                // to the Result property of the DoWorkEventArgs
                // object. This is will be available to the 
                // RunWorkerCompleted eventhandler.
                e.Result = ComputeFibonacci((int)e.Argument, worker, e);
            }
    
            // This event handler deals with the results of the
            // background operation.
            private void backgroundWorker1_RunWorkerCompleted(
                object sender, RunWorkerCompletedEventArgs e)
            {
                // First, handle the case where an exception was thrown.
                if (e.Error != null)
                {
                    MessageBox.Show(e.Error.Message);
                }
                else if (e.Cancelled)
                {
                    // Next, handle the case where the user canceled 
                    // the operation.
                    // Note that due to a race condition in 
                    // the DoWork event handler, the Cancelled
                    // flag may not have been set, even though
                    // CancelAsync was called.
                    resultLabel.Text = "Canceled";
                }
                else
                {
                    // Finally, handle the case where the operation 
                    // succeeded.
                    resultLabel.Text = e.Result.ToString();
                }
    
                // Enable the UpDown control.
                this.numericUpDown1.Enabled = true;
    
                // Enable the Start button.
                startAsyncButton.Enabled = true;
    
                // Disable the Cancel button.
                cancelAsyncButton.Enabled = false;
            }
    
            // This event handler updates the progress bar.
            private void backgroundWorker1_ProgressChanged(object sender,
                ProgressChangedEventArgs e)
            {
                this.progressBar1.Value = e.ProgressPercentage;
            }
    
            // This is the method that does the actual work. For this
            // example, it computes a Fibonacci number and
            // reports progress as it does its work.
            long ComputeFibonacci(int n, BackgroundWorker worker, DoWorkEventArgs e)
            {
                // The parameter n must be >= 0 and <= 91.
                // Fib(n), with n > 91, overflows a long.
                if ((n < 0) || (n > 91))
                {
                    throw new ArgumentException(
                        "value must be >= 0 and <= 91", "n");
                }
    
                long result = 0;
    
                // Abort the operation if the user has canceled.
                // Note that a call to CancelAsync may have set 
                // CancellationPending to true just after the
                // last invocation of this method exits, so this 
                // code will not have the opportunity to set the 
                // DoWorkEventArgs.Cancel flag to true. This means
                // that RunWorkerCompletedEventArgs.Cancelled will
                // not be set to true in your RunWorkerCompleted
                // event handler. This is a race condition.
    
                if (worker.CancellationPending)
                {   
                    e.Cancel = true;
                }
                else
                {   
                    if (n < 2)
                    {   
                        result = 1;
                    }
                    else
                    {   
                        result = ComputeFibonacci(n - 1, worker, e) + 
                                 ComputeFibonacci(n - 2, worker, e);
                    }
    
                    // Report progress as a percentage of the total task.
                    int percentComplete = 
                        (int)((float)n / (float)numberToCompute * 100);
                    if (percentComplete > highestPercentageReached)
                    {
                        highestPercentageReached = percentComplete;
                        worker.ReportProgress(percentComplete);
                    }
                }
    
                return result;
            }
    
            #region Windows Form Designer generated code
            
            private void InitializeComponent()
            {
                this.numericUpDown1 = new System.Windows.Forms.NumericUpDown();
                this.startAsyncButton = new System.Windows.Forms.Button();
                this.cancelAsyncButton = new System.Windows.Forms.Button();
                this.resultLabel = new System.Windows.Forms.Label();
                this.progressBar1 = new System.Windows.Forms.ProgressBar();
                this.backgroundWorker1 = new System.ComponentModel.BackgroundWorker();
                ((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).BeginInit();
                this.SuspendLayout();
                // 
                // numericUpDown1
                // 
                this.numericUpDown1.Location = new System.Drawing.Point(16, 16);
                this.numericUpDown1.Maximum = new System.Decimal(new int[] {
                91,
                0,
                0,
                0});
                this.numericUpDown1.Minimum = new System.Decimal(new int[] {
                1,
                0,
                0,
                0});
                this.numericUpDown1.Name = "numericUpDown1";
                this.numericUpDown1.Size = new System.Drawing.Size(80, 20);
                this.numericUpDown1.TabIndex = 0;
                this.numericUpDown1.Value = new System.Decimal(new int[] {
                1,
                0,
                0,
                0});
                // 
                // startAsyncButton
                // 
                this.startAsyncButton.Location = new System.Drawing.Point(16, 72);
                this.startAsyncButton.Name = "startAsyncButton";
                this.startAsyncButton.Size = new System.Drawing.Size(120, 23);
                this.startAsyncButton.TabIndex = 1;
                this.startAsyncButton.Text = "Start Async";
                this.startAsyncButton.Click += new System.EventHandler(this.startAsyncButton_Click);
                // 
                // cancelAsyncButton
                // 
                this.cancelAsyncButton.Enabled = false;
                this.cancelAsyncButton.Location = new System.Drawing.Point(153, 72);
                this.cancelAsyncButton.Name = "cancelAsyncButton";
                this.cancelAsyncButton.Size = new System.Drawing.Size(119, 23);
                this.cancelAsyncButton.TabIndex = 2;
                this.cancelAsyncButton.Text = "Cancel Async";
                this.cancelAsyncButton.Click += new System.EventHandler(this.cancelAsyncButton_Click);
                // 
                // resultLabel
                // 
                this.resultLabel.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
                this.resultLabel.Location = new System.Drawing.Point(112, 16);
                this.resultLabel.Name = "resultLabel";
                this.resultLabel.Size = new System.Drawing.Size(160, 23);
                this.resultLabel.TabIndex = 3;
                this.resultLabel.Text = "(no result)";
                this.resultLabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
                // 
                // progressBar1
                // 
                this.progressBar1.Location = new System.Drawing.Point(18, 48);
                this.progressBar1.Name = "progressBar1";
                this.progressBar1.Size = new System.Drawing.Size(256, 8);
                this.progressBar1.Step = 2;
                this.progressBar1.TabIndex = 4;
                // 
                // backgroundWorker1
                // 
                this.backgroundWorker1.WorkerReportsProgress = true;
                this.backgroundWorker1.WorkerSupportsCancellation = true;
                // 
                // FibonacciForm
                // 
                this.ClientSize = new System.Drawing.Size(292, 118);
                this.Controls.Add(this.progressBar1);
                this.Controls.Add(this.resultLabel);
                this.Controls.Add(this.cancelAsyncButton);
                this.Controls.Add(this.startAsyncButton);
                this.Controls.Add(this.numericUpDown1);
                this.Name = "FibonacciForm";
                this.Text = "Fibonacci Calculator";
                ((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).EndInit();
                this.ResumeLayout(false);
            }
            #endregion
    
            [STAThread]
            static void Main()
            {
                Application.Run(new FibonacciForm());
            }
        }
    }
    
    • 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
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292

    5. RunWorkerAsync()

    下面的代码示例演示如何使用 RunWorkerAsync 该方法启动异步操作。 它是 如何:在后台下载文件的大型示例的一部分。

    private void downloadButton_Click(object sender, EventArgs e)
    {
        // Start the download operation in the background.
        this.backgroundWorker1.RunWorkerAsync();
    
        // Disable the button for the duration of the download.
        this.downloadButton.Enabled = false;
    
        // Once you have started the background thread you 
        // can exit the handler and the application will 
        // wait until the RunWorkerCompleted event is raised.
    
        // Or if you want to do something else in the main thread,
        // such as update a progress bar, you can do so in a loop 
        // while checking IsBusy to see if the background task is
        // still running.
    
    
    
    //启动后台线程后
    //可以退出处理程序,应用程序将
    //等待,直到引发RunWorkerCompleted事件。
    //或者如果你想在主线程中做其他事情,
    //例如更新进度条,可以在循环中执行
    //检查IsBusy以查看后台任务是否为
    //仍在运行
        while (this.backgroundWorker1.IsBusy)
        {
            progressBar1.Increment(1);
            // Keep UI messages moving, so the form remains 
            // responsive during the asynchronous operation.
            Application.DoEvents();
        }
    }
    
    • 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

    6. 如何:在后台下载文件

    下载文件是一项常见任务,在单独线程上运行这个可能很耗时的操作通常很有用。 使用 BackgroundWorker 组件来完成此任务,几乎不使用任何代码。

    示例

    以下代码示例演示如何使用 BackgroundWorker 组件从 URL 加载 XML 文件。 当用户单击“下载”按钮时,Click
    事件处理程序将调用 BackgroundWorker 组件的 RunWorkerAsync 方法以开始下载操作。
    按钮在下载期间处于禁用状态,下载完毕后处于启用状态。 MessageBox 显示文件的内容。

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Threading;
    using System.Windows.Forms;
    using System.Xml;
    
    public class Form1 : Form
    {
        private BackgroundWorker backgroundWorker1;
        private Button downloadButton;
        private ProgressBar progressBar1;
        private XmlDocument document = null;
    
        public Form1()
        {
            InitializeComponent();
    
            // Instantiate BackgroundWorker and attach handlers to its
            // DoWork and RunWorkerCompleted events.
            backgroundWorker1 = new System.ComponentModel.BackgroundWorker();
            backgroundWorker1.DoWork += new System.ComponentModel.DoWorkEventHandler(this.backgroundWorker1_DoWork);
            backgroundWorker1.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.backgroundWorker1_RunWorkerCompleted);
        }
    
        private void downloadButton_Click(object sender, EventArgs e)
        {
            // Start the download operation in the background.
            this.backgroundWorker1.RunWorkerAsync();
    
            // Disable the button for the duration of the download.
            this.downloadButton.Enabled = false;
    
            // Once you have started the background thread you
            // can exit the handler and the application will
            // wait until the RunWorkerCompleted event is raised.
    
            // Or if you want to do something else in the main thread,
            // such as update a progress bar, you can do so in a loop
            // while checking IsBusy to see if the background task is
            // still running.
    
            while (this.backgroundWorker1.IsBusy)
            {
                progressBar1.Increment(1);
                // Keep UI messages moving, so the form remains
                // responsive during the asynchronous operation.
                Application.DoEvents();
            }
        }
    
        private void backgroundWorker1_DoWork(
            object sender,
            DoWorkEventArgs e)
        {
            document = new XmlDocument();
    
            // Uncomment the following line to
            // simulate a noticeable latency.
            //Thread.Sleep(5000);
    
            // Replace this file name with a valid file name.
            document.Load(@"http://www.tailspintoys.com/sample.xml");
        }
    
        private void backgroundWorker1_RunWorkerCompleted(
            object sender,
            RunWorkerCompletedEventArgs e)
        {
            // Set progress bar to 100% in case it's not already there.
            progressBar1.Value = 100;
    
            if (e.Error == null)
            {
                MessageBox.Show(document.InnerXml, "Download Complete");
            }
            else
            {
                MessageBox.Show(
                    "Failed to download file",
                    "Download failed",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
            }
    
            // Enable the download button and reset the progress bar.
            this.downloadButton.Enabled = true;
            progressBar1.Value = 0;
        }
    
        #region Windows Form Designer generated code
    
        /// 
        /// Required designer variable.
        /// 
        private System.ComponentModel.IContainer components = null;
    
        /// 
        /// Clean up any resources being used.
        /// 
        /// true if managed resources should be disposed; otherwise, false.
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
    
        /// 
        /// Required method for Designer support
        /// 
        private void InitializeComponent()
        {
            this.downloadButton = new System.Windows.Forms.Button();
            this.progressBar1 = new System.Windows.Forms.ProgressBar();
            this.SuspendLayout();
            //
            // downloadButton
            //
            this.downloadButton.Location = new System.Drawing.Point(12, 12);
            this.downloadButton.Name = "downloadButton";
            this.downloadButton.Size = new System.Drawing.Size(100, 23);
            this.downloadButton.TabIndex = 0;
            this.downloadButton.Text = "Download file";
            this.downloadButton.UseVisualStyleBackColor = true;
            this.downloadButton.Click += new System.EventHandler(this.downloadButton_Click);
            //
            // progressBar1
            //
            this.progressBar1.Location = new System.Drawing.Point(12, 50);
            this.progressBar1.Name = "progressBar1";
            this.progressBar1.Size = new System.Drawing.Size(100, 26);
            this.progressBar1.TabIndex = 1;
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(133, 104);
            this.Controls.Add(this.progressBar1);
            this.Controls.Add(this.downloadButton);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
        }
    
        #endregion
    }
    
    static class Program
    {
        /// 
        /// The main entry point for the application.
        /// 
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.Run(new Form1());
        }
    }
    
    • 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
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164

    7.下载文件

    文件被下载到 BackgroundWorker 组件的工作线程上,该线程运行 DoWork 事件处理程序。 当代码调用 RunWorkerAsync 方法时,将启动此线程。

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        document = new XmlDocument();
    
        // Uncomment the following line to simulate a noticeable latency.
        // 取消注释下面的行以模拟明显的延迟。
        //Thread.Sleep(5000);
    
        // Replace this file name with a valid file name.
        document.Load(@"http://www.tailspintoys.com/sample.xml");
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    等待 BackgroundWorker 完成

    downloadButton_Click 事件处理程序演示如何等待 BackgroundWorker 组件完成其异步任务。

    在等待后台线程完成时,如果你只希望应用程序响应事件,而不希望在主线程中执行任何操作,直接退出处理程序即可。

    如果想要继续在主线程中执行操作,可使用 IsBusy 属性来确定 BackgroundWorker 线程是否仍在运行。 在示例中,进度条随下载进行而更新。 请确保调用 Application.DoEvents 方法以保持 UI 响应能力。

    private void downloadButton_Click(object sender, EventArgs e)
    {
        // Start the download operation in the background.
        this.backgroundWorker1.RunWorkerAsync();
    
        // Disable the button for the duration of the download.
        this.downloadButton.Enabled = false;
    
        // Once you have started the background thread you
        // can exit the handler and the application will
        // wait until the RunWorkerCompleted event is raised.
    
        // Or if you want to do something else in the main thread,
        // such as update a progress bar, you can do so in a loop
        // while checking IsBusy to see if the background task is
        // still running.
    
        while (this.backgroundWorker1.IsBusy)
        {
            progressBar1.Increment(1);
            // Keep UI messages moving, so the form remains
            // responsive during the asynchronous operation.
            Application.DoEvents();
        }
    }
    
    • 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

    显示结果

    backgroundWorker1_RunWorkerCompleted 方法处理 RunWorkerCompleted 事件,并在后台操作完成时被调用。 此方法首先检查 AsyncCompletedEventArgs.Error 属性。 如果 AsyncCompletedEventArgs.Error 为 null,则此方法显示文件的内容。 然后它启用在下载开始后被禁用的下载按钮,并重置进度栏。

    private void backgroundWorker1_RunWorkerCompleted(  object sender, RunWorkerCompletedEventArgs e)
    {
        // Set progress bar to 100% in case it's not already there.
        progressBar1.Value = 100;
    
        if (e.Error == null)
        {
            MessageBox.Show(document.InnerXml, "Download Complete");
        }
        else
        {
            MessageBox.Show(
                "Failed to download file",
                "Download failed",
                MessageBoxButtons.OK,
                MessageBoxIcon.Error);
        }
    
        // Enable the download button and reset the progress bar.
        this.downloadButton.Enabled = true;
        progressBar1.Value = 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
  • 相关阅读:
    MATLAB算法实战应用案例精讲-【智能优化算法】黏菌算法(SMA)
    latex三线表使用辅助线,超出表格长度
    C# 第二章『基础语法』◆第4节:foreach循环语句
    What is SVM algorithm
    深入理解与应用工厂方法模式
    手撕代码(Simple)- Java后端高频面试算法题集锦 1
    EL 与 JSTL(1)( EL 表达式基础知识)
    【LeetCode高频SQL50题-基础版】打卡第1天:第1~10题
    USB2.0 UTMI接口
    Qt开发-QT Quick
  • 原文地址:https://blog.csdn.net/kalvin_y_liu/article/details/127631127