I’m trying to iterate through the selected values on a ListBox to see if I have a match, and then set a bool if I do. I’ve got this code that works when not threading:
for (int i = 0; i < FileTypesExcludedListBox.SelectedItems.Count; i++)
if (currentFiles[currentFileLoc].EndsWith(FileTypesExcludedListBox.SelectedItems[i].ToString()))
doNotCompare = true;
Now I’ve changed the app so this is happening inside a thread that didn’t create the control. I’ve used anonymous delegates to update text labels before, but I need the return value for this and I’m not sure how to do that…
1) You can Invoke calls to control on UI thread. Of course, you can pass array of objects as parameter. But it’s really easy to put some parameter on wrong place. I think, it’s better to use anonymous delegate instead.
Same thing for returning values from control. You just need to specify type of returned value in delegate signature.
Usage from worker thread:
2) You can provide parameters when starting thread.
So, you will not need access to control from worker thread:
3) You can use AOP e.g. PostSharp to implement thread dispatching automatically.