I have set a property across threads before and I found this post Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on about getting a property.
I think my issue with the code below is setting the variable to the collection is an object therefore on the heap and therefore is just creating a pointer to the same object
So my question is besides creating a deep copy, or copying the collection into a different List object is there a better way to do the following to aviod the error during the for loop.
Cross-thread operation not valid: Control 'lstProcessFiles' accessed
from a thread other than the thread it was created on.
Code:
private void btnRunProcess_Click(object sender, EventArgs e)
{
richTextBox1.Clear();
BackgroundWorker bg = new BackgroundWorker();
bg.DoWork += new DoWorkEventHandler(bg_DoWork);
bg.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bg_RunWorkerCompleted);
bg.RunWorkerAsync();
}
void bg_DoWork(object sender, DoWorkEventArgs e)
{
WorkflowEngine engine = new WorkflowEngine();
ListBox.SelectedObjectCollection selectedCollection=null;
if (lstProcessFiles.InvokeRequired)
{
// Try #1
selectedCollection = (ListBox.SelectedObjectCollection)
this.Invoke(new GetSelectedItemsDelegate(GetSelectedItems),
new object[] { lstProcessFiles });
// Try #2
//lstProcessFiles.Invoke(
// new MethodInvoker(delegate {
// selectedCollection = lstProcessFiles.SelectedItems; }));
}
else
{
selectedCollection = lstProcessFiles.SelectedItems;
}
// *********Same Error on this line********************
// Cross-thread operation not valid: Control 'lstProcessFiles' accessed
// from a thread other than the thread it was created on.
foreach (string l in selectedCollection)
{
if (engine.LoadProcessDocument(String.Format(@"C:\TestDirectory\{0}", l)))
{
try
{
engine.Run();
WriteStep(String.Format("Ran {0} Succussfully", l));
}
catch
{
WriteStep(String.Format("{0} Failed", l));
}
engine.PrintProcess();
WriteStep(String.Format("Rrinted {0} to debug", l));
}
}
}
private delegate void WriteDelegate(string p);
private delegate ListBox.SelectedObjectCollection GetSelectedItemsDelegate(ListBox list);
private ListBox.SelectedObjectCollection GetSelectedItems(ListBox list)
{
return list.SelectedItems;
}
Take a look at this SO question – it addresses a similar topic.
In many UI technologies (Winforms, WPF, Silverlight) – UI elements can only be safely interacted with on the UI thread. This means that when writing multithreaded code, you need to use the mechanisms in your UI library of choice to correctly interact with UI controls. In WPF/Silverlight that would be the Dispatcher, in WinForms it requires using InvokeRequired method and BeginInvoke() to dispatch the work to the UI thread.
In your case, it seems like you are already trying to use
BeginInvoketo dispatch to the appropriate thread. I suspect the problem is that iterating over the ListBox’s collection is a cross-thread operation itself. You don’t know how theSelectedItemscollection implementsGetEnumerator()– most likley though it doesn’t copy the collection. So I suspect your code will have to make a copy before iterating over it – or perform the entire iteration on the UI thread.Making a copy of the collection isn’t to bad, but again, the copy has to be made on the UI thread – and it may need to be a deep copy since it contains other UI-owned objects (ListBoxItem). If you are on .NET 3.5, you can use LINQ to project an anonymous object in your code rather than trying to make deep copies of UI elements.