473,408 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 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 4291
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.