Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6673603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:38:48+00:00 2026-05-26T03:38:48+00:00

Based on the solution found here I am able to iterate through a list

  • 0

Based on the solution found
here I am able to iterate through a list of Application.Current.Windows and close them upon logging out of my system. However, there is a possibility that a Dialog (OpenFileDialog or the like) may be open; unfortunately, this dialog is not in the collection of Current.Windows. Is there any other way to ensure that all such dialogs get closed (without having to store a collection of them somewhere, for instance?)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:38:48+00:00Added an answer on May 26, 2026 at 3:38 am

    To find all the traditional Win32 windows, you will want to enumerate the windows on your thread(s) and close them.

    Declarations of some helpful functions and a constant:

    [DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
    [DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, IntPtr windowTitle);
    [DllImport("user32.dll", SetLastError = true)]
    static extern uint GetWindowThreadProcessId(IntPtr hwnd, IntPtr processId);
    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool EnumThreadWindows(uint dwThreadId, EnumThreadFindWindowDelegate lpfn, IntPtr lParam);
    [DllImport("user32", SetLastError = true, CharSet = CharSet.Auto)]
    private extern static int GetWindowText(IntPtr hWnd, StringBuilder text, int maxCount);
    [DllImport("User32.dll", EntryPoint = "SendMessage")]
    public static extern int SendMessage(int hWnd, int Msg, int wParam, int lParam);
    
    const uint WM_CLOSE = 0x10;
    

    You need a delegate declaration for the callback to enum the children:

    public delegate bool EnumThreadFindWindowDelegate(IntPtr hwnd, IntPtr lParam);
    

    The callback itself, which closes each window found. You will want to add your own criteria here if there are windows you do not want closed, e.g. your top level window. I’ve added some commented out code below to get the window title, for example.

    static bool EnumThreadCallback(IntPtr hWnd, IntPtr lParam)
    {
        // If you want to check for a non-closing window by title...
        // Get the window's title
        //StringBuilder text = new StringBuilder(500);
        //GetWindowText(hWnd, text, 500);
    
        SendMessage(hWnd, WM_CLOSE, 0, 0);
    
        // Continue
        return true;
    }
    

    To do the enumeration:

    uint threadId = Thread.CurrentThread.ManagedThreadId;
    EnumThreadWindows(threadId, EnumThreadCallback, IntPtr.Zero);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Update: This question was an epic failure, but here's the working solution. It's based
I found a good solution for a reusable select view here: select dropdown with
Has anyone successfully implemented a Java based solution that uses Microsoft SQL Server 2005
We are building a web based solution right now and it is almost ready
I have a solution based on ASP, VB6, COM and SQL Server. The web
I'm involved in creating a web based business solution. The idea is that the
I am looking for a good role based authorization solution to use alongside Authlogic.
I work at a college that uses an intranet based student management solution not
I would like to enable/disable some code based on a custom solution configuration I
I'm looking for a good solution for graphically viewing a SOAP-based web service and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.