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
Ask a Question
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

troubleshooting Question

BackgroundWorker is throwing Cross-thread exception

Avatar of tiwengr
tiwengr asked on
C#
14 Comments1 Solution2501 ViewsLast Modified:
I am attempting to create a BackgroundWorker for a new application.  The purpose is to update the contents of a ListBox on Form1.  The updating is triggered by a Form Timer, timer1.  The timer interval is 5000.  So every 5 seconds, I would like the Background Worker to update the ListBox, listPending, by calling a public static method SRLib.loadPending() that probes a MySQL database looking for items needing approval.

I dragged a BackgroundWorker control onto Form1, called backgroundWorker1 and here's some code:

public Form1()
        {
            InitializeComponent();

            backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);

            timer1.Enabled = true;
         }


private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            SRLib.loadPending(listPending, "A", SRLib.userName); //loads all orders needing approval -
        }
       

private void timer1_Tick(object sender, EventArgs e)
        {
            backgroundWorker1.RunWorkerAsync();
        }


When this is run, I get the dialog saying "Problem encountered: System.InvalidOperationException:
Cross-thread operation not valid.  Control 'listPending' accessed from a thread other than the thread it was created on.

Then after that I get:
InvalidOperationException was unhandled:  This BackgroundWorker is currently busy and cannot run multiple tasks concurrently.

I have never created a BackgroundWorker before so I am probably making a bunch of rookie mistakes, but I would really appreciate some expert guidance here.

Thanks,
-Bob
ASKER CERTIFIED SOLUTION
Avatar of p_davis
p_davis

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Commented:
This problem has been solved!
Unlock 1 Answer and 14 Comments.
See Answers