I have an instance of ObservableCollection in the ViewModel of my .NET Framework 4.0 C# MVVM application. The name of this ObservableCollection is Workspaces. This collection is the data source for TabControl in View (the ItemSource property of the TabControl is bound to the collection). This collection is used with two threads: thread "A" and thread "B". Thread "A" adds or removes elements in collection and thread "B" reads elements from collection via foreach loop.
foreach (WorkspaceViewModel workspace in this.Workspaces)
{
. . . .
}
Sometimes when thread "A" adds element in collection the InvalideOperationException has place in thread "B" in foreach loop. This exception reports: "Collection was modified; enumeration operation may not execute". If I replace foreach loop to for loop, can it solve my problem?