472,951 Members | 1,950 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,951 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 4269
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Ron L | last post by:
I have an MDI application with a number of child windows. In each child window I am catching the Closing event and having the child window decide if it should set cancel to true. The intent here...
2
by: Ron L | last post by:
I have an MDI application which opens a number of child windows, each of which could have data in a state that needs to be saved. Each child window catches its Closing event and cancels it if the...
22
by: alecjames1 | last post by:
I have a form which the user must complete before closing. I have disabled the window x button and use my own exit button. When selected it checks to see if the user has completed the entries...
0
by: Andrew | last post by:
I get a Null Reference Exception if I close a modeless form (that is, a form displayed using Show()) when a selection is made from a ComboxBox. If the form is modal (displayed using ShowDialog())...
3
by: Andrew | last post by:
I get a Null Reference Exception if I close a non-modal dialog (that is, a form opened with Show()) when a selection is made from a ComboBox. The error message refers to Unsafe Native Methods, but...
2
by: chris in grimsby | last post by:
MDIChild Window Closing event not raised when MDI Parent is in a class library! Intructions to recreate problem: 1. Create a ClassLibrary project 2. Add an MDIParent form and a form that will...
10
by: Charles Law | last post by:
For some reason, when I click the X to close my MDI parent form, the action appears to be re-directed to one of the MDI child forms, and the parent remains open. I am then unable to close the...
3
by: sewid | last post by:
Hi! I have a very simple windows form (Visual Basic, .NET-Framework 2.0). This is my whole code: Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As...
2
by: =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= | last post by:
I am (still) relatively new to Windows applications, most of my experience has been Web based, and I am confused about what exactly happens when the Main() method is called and how to manipulate...
4
Fr33dan
by: Fr33dan | last post by:
Hi, I'm having trouble with a multi-threaded program crashing on a specific machine when the worker thread is not initialized at when the _FormClosing method of my main form called. Here is my...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

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.