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;
Where does the Background worker thread come in?
What is ImportStatusList in this code? Is that the UltraListView?
Are you saying that you are referring to the UltraListView control (which is on the UI Thread) from another thread?
You cannot do that.
Ok so the background worker is on the main form thread, so stepping through code today the i get the error whether its UltraListView or UltraGrid Control, the problem is the "ImportStatusList " this is a list. The underlying datasource cannot be modified for some reason, First time ever catching this error in history, so I am learning about it now.
Now the ultralistview control is on another form, I simply pass the "ImportStatusList " to the form to show the status of the import class. I am trying to set either a grid or list view . Keep in mind i am using the background worker to do all the processing of import log object below. So how to fix this error that i am getting, can you give me some advice
List<ImportLog> ImportStatusList = new List<ImportLog>();
public DateTime StoptDateTime;
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 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.
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 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;