In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces winform multithreaded component BackgroundWorker how to use the relevant knowledge, detailed and easy to understand, simple and fast operation, with certain reference value, I believe that everyone will have a harvest after reading this winform multithreaded component BackgroundWorker how to use the article, let's take a look at it.
BackgroundWorker is a control used in. net to perform multithreaded tasks, allowing programmers to perform operations on a single thread.
BackgroundWorker can be created programmatically or dragged onto a form from the Components tab of the Toolbox. If you create a BackgroundWorker in the Windows Forms Designer, it appears in the Component Tray and its properties appear in the Properties window.
common methods
RunWorkerAsync starts performing background operations. Raises a DoWork event.
public void RunWorkerAsync(); //Start thread, trigger DoWork event
public void RunWorkerAsync(object argument);
CancelAsync requests cancellation of pending background operations.
Note: This method sets the CancellationPending property to true and does not terminate background operations. The CancellationPending property is checked in the background to determine whether to proceed with the time-consuming operation.
ReportProgress raises the ProgressChanged event.
public void ReportProgress(int percentProgress); //Report progress, trigger ProgressChanged event
public void ReportProgress(int percentProgress, object userState);
commonly used attributes
IsBusy://read-only property, used to determine whether the current thread is working.
CancellationPending: Indicates whether the application has requested cancellation of background operations. Read-only property, false by default, true when CancelAsync method is executed.
WorkerSupportsCancellation: Indicates whether asynchronous cancellations are supported. To execute the CancelAsync method, you first need to set this property to true.
WorkerReportsProgress: Indicates whether progress can be reported. To execute the ReportProgress method, you first need to set this property to true.
Common Events
DoWork: occurs when the RunWorkerAsync method is called.
ProgressChanged: Optional, occurs when the ReportProgress method is called.
RunWorkerCompleted: Optional, occurs when a background operation has completed, is canceled, or throws an exception.
Note: No user interface objects are manipulated in the DoWork event handler. Instead, you should communicate with the user interface via the ProgressChanged and RunWorkerCompleted events.
If you want to communicate with a control of the user interface in a DoWork event handler, you can use the ReportProgress method. ReportProgress (int percentProgress, object userState), can pass an object. The ProgressChanged event gets this information object from the UserState property of the ProgressChangedEventArgs class. This event can also implement the progress bar function, presenting the progress of the task to the user in real time.
Simple programs use BackgroundWorker more convenient than Thread, Thread and user interface control communication is more troublesome, you need to use delegates to call the Invoke or BeginInvoke method of the control, not BackgroundWorker convenience.
Using backgroundWorker steps
Create a new BackgroundWorder object;
Set whether to cancel (Worker SupportsCancellation) and report progress (Worker Reports Progress) according to requirements;
Set relevant events according to requirements: DoWorker, ProgressChanged, ProgressChanged;
Call the RunWorkerAsyns() method to start the thread;
Determine the CancellationPending value at the position where cancellation is required, and perform relevant processing;//Optional
Call the ReportProgress(int percentProgress) method at the appropriate location to report progress.
BackgroundWorker instance public partial class Form1 : Form { public Form1() { InitializeComponent(); backgroundWorker1.WorkerReportsProgress = true;//Report completion progress backgroundWorker1.WorkerSupportsCancellation = true;//Allow user to terminate background threads //bind event backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork); backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged); backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted); } //Start button private void button1_Click(object sender, EventArgs e) { if (! backgroundWorker 1.IsBusy)//Determine if backgroundWorker1 is running asynchronous operations { backgroundWorker1.RunWorkerAsync(1000);//Start background asynchronous operations, invoke DoWork event } while (backgroundWorker1.IsBusy)//wait for the background to finish running { Application.DoEvents(); } MessageBox.Show("Operation completed"); } //Cancel button private void button2_Click(object sender, EventArgs e) { if (backgroundWorker1.WorkerSupportsCancellation == true) { backgroundWorker1.CancelAsync();//Cancel background operation backgroundWorker1.Dispose();//Free resources } } //DoWork events declare time-consuming actions to perform private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker backgroundWorker = sender as BackgroundWorker; e.Result = ListNumber(backgroundWorker, e);//The result of the operation is stored in e.Result (which may be used in the RunWorkerCompleted event) } bool ListNumber(object sender, DoWorkEventArgs e) { int num = (int)e.Argument;//Accepts the passed parameter, i.e. RunWorkerAsync(object argument) for (int i = 1; i
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.