We help IT Professionals succeed at work.
Get Started

is backgroundworker.IsBusy property thread safe?

CodingCat
CodingCat asked
on
3,155 Views
Last Modified: 2012-05-12
hi, experts,

I have a backgroundworker declared in the main thread:

            bkworker = new BackgroundWorker { WorkerSupportsCancellation = true };
            bkworker.DoWork += new DoWorkEventHandler(bkworker_DoWork);
            bkworker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bkworker_RunWorkerCompleted);

I have a camera which sends in images periodically, once an image arrives, I would like to do some image processing with the backgroundworker:

        void camera_ImageArrived(object sender, ImageEventsArgs e)
        {
            if (!bkworker.IsBusy)
            {
                augmentedImg = e.Image;
                bkworker.RunWorkerAsync();
            }
        }

The idea is that when a new image arrives, if the backgroundworker has finished processing the previous image, then start processing the new image, if not, ignore the new image.

after running the program for a while, I got this error message:

A first chance exception of type 'System.InvalidOperationException' occurred in System.dll

Additional information: This BackgroundWorker is currently busy and cannot run multiple tasks concurrently.

This indicates that the bkworker is busy, but bkworker.RunWorkerAsync() should only be executed when bkworker is not busy.  What's the problem?
Comment
Watch Question
Full Stack .NET Developer
Commented:
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE