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