I am writing a WinForms C# Program.
I am trying to close all forms except my main form, FrmMain
.
I must hide my main form.
I produced this by having two forms open. One with my main form, and one with another form shown with the ShowDialog()
method.
When this code is executed on my machine, it appears that it should close all of the forms properly. For some reason, when I am not setting breakpoints and running this particular piece of code, I get an ArgumentOutOfRangeException
because the variable i
gets to the point where it is -1
. When I do set breakpoints, and slowly step through each piece of code it works fine.
It doesn't make sense for the for loop to continue all the way for i
to get to -1
because I have the i >= 0
condition.
Can someone explain to me why the index of i
gets to -1
when I am not using breakpoints but gets to 0
when I am using breakpoints and stepping through each line of the loop individually?
What can be done to fix this?
Thanks in advance.
for (int i = Application.OpenForms.Count - 1; i >= 0; i--)
{
if (Application.OpenForms[i] is FrmMain)
{
Application.OpenForms[i]?.BeginInvoke((MethodInvoker)delegate
{
Application.OpenForms[i]?.Hide();
});
}
else
{
Application.OpenForms[i]?.BeginInvoke((MethodInvoker)delegate
{
Application.OpenForms[i]?.Dispose();
});
}
}
EDIT:
The way I prevented getting the ArgumentOutOfRangeException is by adding another variable inside the for loop. This is the code that I changed.
for (int i = Application.OpenForms.Count - 1; i >= 0; i--)
{
int i1 = i;
if (Application.OpenForms[i] is FrmMain)
{
Application.OpenForms[i]?.BeginInvoke((MethodInvoker)delegate
{
Application.OpenForms[i1]?.Hide();
});
}
else
{
Application.OpenForms[i]?.BeginInvoke((MethodInvoker)delegate
{
Application.OpenForms[i1]?.Dispose();
});
}
}