Closing all Windows in Windows Application

  • Thread starter Thread starter aagarwal8
  • Start date Start date
A

aagarwal8

Hi,

I have a chat application which i have created using WinForms 2.0 (C#
lang).

The situation i am faced with is that i need to close all the opened
(chat, public chat, chat invite, chat history, user profile) windows
at the time of logout, except for the main window.

In my application, all the chat windows are opened on a separate
thread, and the user can then open the rest of the windows (chat
invite, chat history, user profile) from either the conversation
window thread or the main thread.

At the time of signout, i am using the following code to close all the
windows. But some how at times not all the windows get closed.

FormCollection formCollection = Application.OpenForms;
if (formCollection != null && formCollection.Count > 0)
{
//skip i=0, as 0th form is the main form
for (int i = 1; i < formCollection.Count; i++)
{
Form form = formCollection;
if (form != null && form.IsDisposed != true)
{
if (form.InvokeRequired == false)
{
form.Close();
}
else
{
form.Invoke(new CloseFormDelegate(form.Close));
}
}
}
}


Any help in this regard will be highly appreciated.

Regards,
Ankit!!
 
Hi,

I have a chat application which i have created using WinForms 2.0 (C#
lang).

The situation i am faced with is that i need to close all the opened
(chat, public chat, chat invite, chat history, user profile) windows
at the time of logout, except for the main window.

In my application, all the chat windows are opened on a separate
thread, and the user can then open the rest of the windows (chat
invite, chat history, user profile) from either the conversation
window thread or the main thread.

At the time of signout, i am using the following code to close all the
windows. But some how at times not all the windows get closed.

FormCollection formCollection = Application.OpenForms;
if (formCollection != null && formCollection.Count > 0)
{
              //skip i=0, as 0th form is the main form
        for (int i = 1; i < formCollection.Count; i++)
        {
                Form form = formCollection;
                if (form != null && form.IsDisposed !=true)
                {
                        if (form.InvokeRequired == false)
                        {
                                form.Close();
                        }
                        else
                        {
                                     form.Invoke(new CloseFormDelegate(form.Close));
                        }
                }
        }

}

Any help in this regard will be highly appreciated.

Regards,
Ankit!!


Hi,

Keep a list of the windows you have open in a central location (could
be a singleton list) then you will ahve access to all of them and you
can close them from a central location
 
But what is the problem with the existing logic which uses the
collection given by .Net?

Regards,
Ankit!
 
Where is the assumption that the element formCollection [0] is the main form
documented? I think that may be the issue.
 
Since this is the first form which is created when the application is
launched, i am assuming that this is the form at the 0th index. Can
this be a problem?

Regards,
Ankit!
 
Since this is the first form which is created when the application is
launched, i am assuming that this is the form at the 0th index.  Can
this be a problem?

Regards,
Ankit!

Well, you can check it for yourself :-)

You can always use the form's name to check.

Another problem could be the fact that each form is in a different
thread.
 
You are altering the collection while looping through it. You seem to be
right about the main form being in the first element. I don't think it is
documented though.

Try the following:

FormCollection formCollection = Application.OpenForms;
if (formCollection != null && formCollection.Count > 0)
{
while (formCollection.Count > 1)
{
if (formCollection[1].InvokeRequired)
formCollection[1].Invoke(new
FormCloseDelegate(formCollection[1].Close));
else formCollection[1].Close();
formCollection = Application.OpenForms;
}
}
 
Sorry, change

if (formCollection != null && formCollection.Count > 0)

to:

if (formCollection != null && formCollection.Count > 1)


Family Tree Mike said:
You are altering the collection while looping through it. You seem to be
right about the main form being in the first element. I don't think it is
documented though.

Try the following:

FormCollection formCollection = Application.OpenForms;
if (formCollection != null && formCollection.Count > 0)
{
while (formCollection.Count > 1)
{
if (formCollection[1].InvokeRequired)
formCollection[1].Invoke(new
FormCloseDelegate(formCollection[1].Close));
else formCollection[1].Close();
formCollection = Application.OpenForms;
}
}


Hi,

I have a chat application which i have created using WinForms 2.0 (C#
lang).

The situation i am faced with is that i need to close all the opened
(chat, public chat, chat invite, chat history, user profile) windows
at the time of logout, except for the main window.

In my application, all the chat windows are opened on a separate
thread, and the user can then open the rest of the windows (chat
invite, chat history, user profile) from either the conversation
window thread or the main thread.

At the time of signout, i am using the following code to close all the
windows. But some how at times not all the windows get closed.

FormCollection formCollection = Application.OpenForms;
if (formCollection != null && formCollection.Count > 0)
{
//skip i=0, as 0th form is the main form
for (int i = 1; i < formCollection.Count; i++)
{
Form form = formCollection;
if (form != null && form.IsDisposed != true)
{
if (form.InvokeRequired == false)
{
form.Close();
}
else
{
form.Invoke(new CloseFormDelegate(form.Close));
}
}
}
}


Any help in this regard will be highly appreciated.

Regards,
Ankit!!
 
You are altering the collection while looping through it.  You seem to be
right about the main form being in the first element.  I don't think it is
documented though.

If it is not documented I would not presume it will always be the
first.

You are correct, he is potentially modifying the collection.
You can keep a reference to the main window (or to the handle) when
the application starts.

I would use this code
List<Form> forms = new List<Form>();
foreach(Form f in App.OpenForms)
if ( f.Handle != mainHandle)
forms.Add(f);
foreach(Form f in forms)
f.Close();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top