Closing all Windows in Windows Application

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • aagarwal8@gmail.com

    #1

    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.Ope nForms;
    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.InvokeReq uired == false)
    {
    form.Close();
    }
    else
    {
    form.Invoke(new CloseFormDelega te(form.Close)) ;
    }
    }
    }
    }


    Any help in this regard will be highly appreciated.

    Regards,
    Ankit!!
  • Ignacio Machin ( .NET/ C# MVP )

    #2
    Re: Closing all Windows in Windows Application

    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.Ope nForms;
    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.InvokeReq uired == false)
                            {
                                    form.Close();
                            }
                            else
                            {
                                         form.Invoke(new CloseFormDelega te(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

    Comment

    • aagarwal8@gmail.com

      #3
      Re: Closing all Windows in Windows Application

      But what is the problem with the existing logic which uses the
      collection given by .Net?

      Regards,
      Ankit!

      Comment

      Working...