Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a BackgroundWorker continuously

I need to be able to continuously run my BackgroundWorker. The DoWork event contains a pool threaded process and the OnComplete updates my UI.

I have not been able to find a way to infinitely loop the BackgroundWorker.RunWorkerAsync() method without the whole program freezing. Any help would be greatly appreciated.

like image 997
user2952817 Avatar asked Nov 04 '13 14:11

user2952817


People also ask

What is the difference between BackgroundWorker and thread?

A BackgroundWorker is a ready to use class in WinForms allowing you to execute tasks on background threads which avoids freezing the UI and in addition to this allows you to easily marshal the execution of the success callback on the main thread which gives you the possibility to update the user interface with the ...

Is BackgroundWorker threaded?

BackgroundWorker, is a component in . NET Framework, that allows executing code as a separate thread and then report progress and completion back to the UI.

What is use of BackgroundWorker in C#?

BackgroundWorker is the class in System. ComponentModel which is used when you need to do some task on the back-end or in different thread while keeping the UI available to users (not freezing the user) and at the same time, reporting the progress of the same.

How do I start BackgroundWorker?

To set up for a background operation, add an event handler for the DoWork event. Call your time-consuming operation in this event handler. To start the operation, call RunWorkerAsync.


2 Answers

You have to make a loop in your DoWork-Method. To update your UI you shoud use the ProgressChanged-Method. Here is a small example how this can look like

 public Test()
    {
        this.InitializeComponent();
        BackgroundWorker backgroundWorker = new BackgroundWorker
            {
                 WorkerReportsProgress = true,
                WorkerSupportsCancellation = true
            };
        backgroundWorker.DoWork += BackgroundWorkerOnDoWork;
        backgroundWorker.ProgressChanged += BackgroundWorkerOnProgressChanged;
    }

    private void BackgroundWorkerOnProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        object userObject = e.UserState;
        int percentage = e.ProgressPercentage;
    }

    private void BackgroundWorkerOnDoWork(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker worker = (BackgroundWorker) sender;
        while (!worker.CancellationPending)
        {
            //Do your stuff here
            worker.ReportProgress(0, "AN OBJECT TO PASS TO THE UI-THREAD");
        }        
    }
like image 131
Tomtom Avatar answered Oct 12 '22 23:10

Tomtom


I have done this in the past when needing something to run in the background. If you try to run the backgroundworker while it is running, you will get an excpetion! That is why i make the BackGroundWorker start itself when it is done in the completed event.

And then it will loop forever.

private void Main_Load(object sender, EventArgs e)
{
   // Start Background Worker on load
   bgWorker.RunWorkerAsync();
}

private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
{
   Thread.Sleep(1000);   // If you need to make a pause between runs
   // Do work here
}

private void bgCheck_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
// Update UI

// Run again
bgWorker.RunWorkerAsync();   // This will make the BgWorker run again, and never runs before it is completed.
}
like image 41
Niels Schmidt Avatar answered Oct 13 '22 00:10

Niels Schmidt