Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a BackgroundWorker?

I know it has 3 methods. In my program I have a method to send a message. It is often late and the program sometimes doesn't send the message at all in response to a button press. At times it is as late as 5 seconds from what I would expect and the program freezes. I want to use a BackgroundWorker to send the message as expected and allow the program to run normally at all times. I had the code for sending the message in a button handler. Now where do I put this equivalent code? I would like all of this to still be handled by a button press.

Is this the appropriate handler?

backgroundWorker1.RunWorkerAsync(); 

and in:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) {} 

I'm going to put my code in the button handler? And this before:

carga.progressBar1.Minimum = 0; carga.progressBar1.Maximum = 100; 

Carga is my other form where the ProgressBar is. How do I use a BackgroundWorker in this scenario?

like image 718
angel Avatar asked Jun 26 '11 00:06

angel


People also ask

How do I use BackgroundWorker?

The steps are extremely simple: Create a BackgroundWorker object. Tell the BackgroundWorker object what task to run on the background thread (the DoWork function). Tell it what function to run on the UI thread when the work is complete (the RunWorkerCompleted function).

How do I start BackgroundWorker?

You can create the BackgroundWorker programmatically or you can drag it onto your form from the Components tab of the Toolbox. If you create the BackgroundWorker in the Windows Forms Designer, it will appear in the Component Tray, and its properties will be displayed in the Properties window.

What is the use of BackgroundWorker in VB net?

BackgroundWorker class executes the time consuming operation on a separate background thread thereby keeping the GUI responsive to the end user. It can also report the progress of the operation periodically, cancel the operation while it is running and indicate when the operation is completed.

What is the difference between BackgroundWorker and thread?

BackgroundWorker has already implemented functionality of reporting progress, completion and cancellation - so you don't need to implement it by yourself. Usage of Thread gives you more control over the async process execution (e.g. thread priority or choosing beetween foreground/background thread type).


1 Answers

You can update progress bar only from ProgressChanged or RunWorkerCompleted event handlers as these are synchronized with the UI thread.

The basic idea is. Thread.Sleep just simulates some work here. Replace it with your real routing call.

public Form1() {     InitializeComponent();      backgroundWorker1.DoWork += backgroundWorker1_DoWork;     backgroundWorker1.ProgressChanged += backgroundWorker1_ProgressChanged;     backgroundWorker1.WorkerReportsProgress = true; }  private void button1_Click(object sender, EventArgs e) {     backgroundWorker1.RunWorkerAsync(); }  private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) {     for (int i = 0; i < 100; i++)     {         Thread.Sleep(1000);         backgroundWorker1.ReportProgress(i);     } }  private void backgroundWorker1_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e) {     progressBar1.Value = e.ProgressPercentage; } 
like image 184
Alex Aza Avatar answered Oct 08 '22 11:10

Alex Aza