471,348 Members | 1,150 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,348 software developers and data experts.

Closing all Windows in Windows Application

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[i];
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!!
Apr 7 '08 #1
2 4163
On Apr 7, 11:38*am, aagarw...@gmail.com wrote:
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[i];
* * * * * * * * 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
Apr 7 '08 #2
But what is the problem with the existing logic which uses the
collection given by .Net?

Regards,
Ankit!
Apr 7 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Ron L | last post: by
22 posts views Thread by alecjames1 | last post: by
reply views Thread by Andrew | last post: by
3 posts views Thread by Andrew | last post: by
2 posts views Thread by chris in grimsby | last post: by
2 posts views Thread by =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= | last post: by
1 post views Thread by Ronak mishra | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.