C # initial test of BackgroundWorker,

Source: Internet
Author: User

C # initial test of BackgroundWorker,

 

/* * Created by SharpDevelop. * User: Administrator * Date: 2017/7/31 * Time: 16:18 *  * To change this template use Tools | Options | Coding | Edit Standard Headers. */using System;using System.Collections.Generic;using System.Drawing;using System.Windows.Forms;using System.Threading;using System.ComponentModel;namespace App{    /// <summary>    /// Description of MainForm.    /// </summary>    public partial class MainForm : Form    {        private BackgroundWorker bw;        public MainForm()        {            //            // The InitializeComponent() call is required for Windows Forms designer support.            //            InitializeComponent();            //            // TODO: Add constructor code after the InitializeComponent() call.            //        }        void process1_Exited(object sender, EventArgs e)        {            // TODO: Implement process1_Exited        }        private void MainForm_Load(object sender, EventArgs e)        {        }        void button1_Click(object sender, EventArgs e)        {            bw = new BackgroundWorker();            bw.WorkerReportsProgress = true;            bw.WorkerSupportsCancellation = true;            bw.DoWork += DoWork;            bw.RunWorkerCompleted += RunWorkerComplated;            bw.ProgressChanged += UpdateButtonText;            bw.RunWorkerAsync();        }        void RunWorkerComplated(object sender, RunWorkerCompletedEventArgs e)        {            MessageBox.Show("ok");        }        void DoWork(object sender, DoWorkEventArgs e)        {            for (int i = 1; i <= 100; i++)            {                (sender as BackgroundWorker).ReportProgress(i);                label1.Text = i.ToString();                Thread.Sleep(100);            }        }        void UpdateButtonText(object sender, ProgressChangedEventArgs e)        {            button2.Text = e.ProgressPercentage.ToString();        }    }}

Detours:

1. The UI control cannot be manipulated in the DoWork method.

2. Call the ReportProgress method in the DoWork event to manipulate the UI control in the ProgressChanged event.

3. The WorkerReportsProgress attribute must be set to true. (the default value is false. Because of this, it takes a lot of time to find the cause)

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.