I am using a background worker for some reason I am getting an error that saids collection cannot be modified error, I am basically launching the Listview thats on a form, to always give me import status on data on a list view, I am able to close and open the list view at any given time.
Can someone point me how to get pass this error when I am trying to launch the form to look the update of the list view.
public void LoadList(List<ImportLog> ImportList)
{
if (ImportList != null){
// save the ImportStatusList for later
this.ImportStatusList = ImportList;
foreach (ImportLog Status in this.ImportStatusList)//Error occurs
{AddStatus(Status);}
}
else
MessageBox.Show("Message Viewer is Empty"
);
Visible =
false;
I am done with this. I resolved it by I freezing the list on this thread before you iterate it. You can do that by copying it to an array: I had no ideal what mike was talking about earlier. I use the lock function instead,
Before I was using a List, which is not good to modify or to touch during iteration. I think that's mike was trying to point out to me.
Lists are not thread-safe, the important point here is to copy the List to a different object prior to iteration and not just assign a reference to the same object to some other variable. So I used an array instead. Any changes made to the List on a different thread will not be reflected in the array which can therefore now be safely iterated.
ImportLog[] ImportArray;
Hi Keith,
If you require any further assistance regarding this issue, please let me know.
Sincerely,Chris KDeveloper Support EngineerInfragistics, Inc.www.infragistics.com/support
I don't know what you talking about mike, BUt my software is working as expected. So basically I am handling the update on the importlog on another thread by invoking it. so its actually not on the background worker thread when i release the updates status on another form. Don't worry to much about this solution. I can't really explain at the moment. I got to hit a deadline. I can explain later. But everything is working as expected.
KeithDudley said:the background worker is on the main form thread
That statement does not make sense. Threads are separate, that's the whole point. One thread does not exist on another thread.
If you are binding any control to a List or any other data source, then I would strongly advise you not to attempt to access either the control(s) or the data source on a background worker thread. This is very dangerous. You will almost certainly run into problems and exceptions that are nearly impossible to debug because they occur nowhere near where the actual problem occurred.
You cannot mix threading and data binding, because all communication across threads has to be properly marshaled, and with data binding, you are not in control of that communication.
I fix the error I use an editable source, I use BindingSource.