Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why BackgroundWorker always is busy?

I realized something strange in my background worker in my WPF application.

What I'm trying to accomplish right now is to wait until the BW finishes to start another thread.

Check the following code:

if (bw.IsBusy)
{
    bw.CancelAsync();

    System.Threading.ThreadStart WaitThread = 
        new System.Threading.ThreadStart(delegate() {
            while (bw.IsBusy)
            {
                System.Threading.Thread.Sleep(100);
            }

            bw.RunWorkerAsync();
        });

    System.Windows.Application.Current.Dispatcher.Invoke(
        System.Windows.Threading.DispatcherPriority.Normal,
        WaitThread);  // if I remove this line, bw fires RunWorkerAsyncEvent
}
else
{
    bw.RunWorkerAsync();
}

Please note that I added a Dispatcher.Invoke to wait until the bw is not busy, but ALL THE TIME IS BUSY if I Invoke it and never fires the RunWorkerAsyncCompleted event. Although,, if I remove that line, FIRES the event and it's really strange that.

How can I wait until the bw finishes?

like image 702
Darf Zon Avatar asked Jan 21 '13 00:01

Darf Zon


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 ...

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 does DoWork pass parameters to BackgroundWorker?

You can send a parameter to the background operation using the RunWorkerAsync() method. You can receive this parameter by using the Argument property of the instance of DoWorkEventArgs in the DoWork event handler then you cast it to use it in the background operation.

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.


1 Answers

The deadlock occurs due to the "main" thread of the application can not run (and handle events), thus the Backgroundworker is never able to fire it's finished-function. What you can do to allow the application to fire this is add an Application.DoEvents();

This is what I currently use in a similiar scenario, and it appears to work like a charm!

    while (bw.IsBusy)
    {
        Application.DoEvents();
        System.Threading.Thread.Sleep(100);
    }
like image 53
Gnutt Avatar answered Sep 17 '22 13:09

Gnutt