Please excuse me if I'm asking this in the wrong place. I'm trying to figure out the best way to pass a collection to a background thread for processing.
I've setup a WinForms BackGroundWorker component (using the basic plumbing from MSDN.
Reading from the collection is not a problem, but setting a property (i.e. the ModifiedDate property below) on an entity contained in the collection gives me a cross thread execution error when run from a background thread.
I've read about using InvokeRequired and Invoke, but don't know where this would apply here.
The entitycollection is set as the datasource of a BindingSource on the form, if I disconnect it durng processing, would that fix it?
Open to any ideas or pointers.
VS2005, LLBLGen 2.0, Adapter
// designer code
this.bgParse.DoWork += new
System.ComponentModel.DoWorkEventHandler(this.bgParse_DoWork);
// form code
EntityCollection<MyEntity> ec =
new EntityCollection<MyEntity>(new MyEntityFactory());
private void bgParse_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
EntityCollection<MyEntity> ec = (EntityCollection<MyEntity>) e.Argument;
LongRunningTask(ec, worker, e);
}
private void buttonParse_Click(object sender, EventArgs e)
{
this.bgParse.RunWorkerAsync(this.ec);
}
private void LongRunningTask(
EntityCollection<MyEntity> ec,
BackgroundWorker worker,
DoWorkEventArgs e)
{
while (!worker.CancellationPending)
{
for (int i = 0; i < ec.Count; i++)
{
MyEntity entity = (MyEntity) ec.Items[i];
entity.ModifiedDate = DateTime.Now();
}
break;
}
}