I don't think there is a build-in solution to lock this property. I suggest not to use a foreach
-loop in order to prevent the exception caused by the iterator finding a modified collection. Just use a while
-loop (not a for
-loop because the breaking condition is only evaluated once).
There is still the risk that a form is removed from the list after you checked that there is another form to be processed and you will get an ArgumentOutOfRangeException
. you will have to handle this situation gracefully.
Int32 index = 0;
while (index < Application.OpenForms.Count)
{
try
{
// Try to copy the form because the index may be or may
// become invalid.
Form form = Application.OpenForms[index];
// Do stuff with the form.
}
catch (ArgumentOutOfRangeException exception)
{
// Handle no longer valid index.
}
index++;
}
This is far from perfect but I cannot think of a better solution at the moment. For example you could be at form five, get suspended, and the application closes some of the first five forms. In consequence unprocessed forms are moved towards the front and in consequence are ignored by the code when the thread continues at form six.
If you have to process all forms open at some point (like a snapshot of the application state), it might actually be the only solution to use a foreach
-loop and retry until you manage to iterate over the complete collection without an exception. There is of course the obvious risk that this never happens if forms are opened and closed at very high rates or depending on your iterating code.