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 9006893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:31:58+00:00 2026-06-16T01:31:58+00:00

In my GUI application, I’m using the C# Process class to spawn external processes

  • 0

In my GUI application, I’m using the C# Process class to spawn external processes which may launch windows. The subprocess windows may be displayed via third-party API calls, so it’s not always possible to get the window handle. Is there any way to ensure that the subprocess’s windows are displayed in front of the main application window?

  • 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-06-16T01:32:00+00:00Added an answer on June 16, 2026 at 1:32 am

    The usual method is:

    1 . Get Process class instance returned by Process.Start()
    2 . Query Process.MainWindowHandle
    3 . Call unmanaged Win32 API function “ShowWindow” or “SwitchToThisWindow”

    The trick to your question is that “sub-process windows may be displayed via third-party API calls”. In that case you will need Get the Window Handle of the spawned exe and Enum Child Windows. Once you have the Handles for the forms that are shown after API calls you can use the BringWindowToTop API.

    I put together a small test using How To Enumerate Windows Using the WIN32 API as inspiration. Create a Windows application with 1 form and 2 buttons:

    public partial class Form1 : Form
    {
    [DllImport("user32.dll", SetLastError = true)]
    static extern bool BringWindowToTop(IntPtr hWnd);
    
    [DllImport("user32")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr i);
    
    public Form1()
    {
        InitializeComponent();
    }
    
    private System.IntPtr hWnd;
    private void button1_Click(object sender, EventArgs e)
    {
        Process p = Process.Start(@"C:\TFS\Sandbox\3rdPartyAppExample.exe");         
        try
        {
            do
            {
                p.Refresh();
            }
            while (p.MainWindowHandle.ToInt32() == 0);
    
            hWnd = new IntPtr(p.MainWindowHandle.ToInt32());
        }
        catch (Exception ex)
        {
            //Do some stuff...
            throw;
        }
    }
    
    private void button2_Click(object sender, EventArgs e)
    {
        3rdPartyAppExample.Form1 f = new 3rdPartyAppExample.Form1();
        f.ShowForm2();
        //Bring main external exe window to front
        BringWindowToTop(hWnd);
        //Bring child external exe windows to front
        BringExternalExeChildWindowsToFront(hWnd);
    }
    
    private void BringExternalExeChildWindowsToFront(IntPtr parent)
    {
        List<IntPtr> childWindows = GetChildWindows(hWnd);
        foreach (IntPtr childWindow in childWindows)
        {
            BringWindowToTop(childWindow);
        }
    }
    
    // <summary>
    /// Returns a list of child windows
    /// </summary>
    /// <param name="parent">Parent of the windows to return</param>
    /// <returns>List of child windows</returns>
    public static List<IntPtr> GetChildWindows(IntPtr parent)
    {
        List<IntPtr> result = new List<IntPtr>();
        GCHandle listHandle = GCHandle.Alloc(result);
        try
        {
            EnumWindowProc childProc = new EnumWindowProc(EnumWindow);
            EnumChildWindows(parent, childProc, GCHandle.ToIntPtr(listHandle));
        }
        finally
        {
            if (listHandle.IsAllocated)
                listHandle.Free();
        }
        return result;
    }
    
    /// <summary>
    /// Callback method to be used when enumerating windows.
    /// </summary>
    /// <param name="handle">Handle of the next window</param>
    /// <param name="pointer">Pointer to a GCHandle that holds a reference to the list to fill</param>
    /// <returns>True to continue the enumeration, false to bail</returns>
    private static bool EnumWindow(IntPtr handle, IntPtr pointer)
    {
        GCHandle gch = GCHandle.FromIntPtr(pointer);
        List<IntPtr> list = gch.Target as List<IntPtr>;
        if (list == null)
        {
            throw new InvalidCastException("GCHandle Target could not be cast as List<IntPtr>");
        }
        list.Add(handle);
        //  You can modify this to check to see if you want to cancel the operation, then return a null here
        return true;
    }
    
    /// <summary>
    /// Delegate for the EnumChildWindows method
    /// </summary>
    /// <param name="hWnd">Window handle</param>
    /// <param name="parameter">Caller-defined variable; we use it for a pointer to our list</param>
    /// <returns>True to continue enumerating, false to bail.</returns>
    public delegate bool EnumWindowProc(IntPtr hWnd, IntPtr parameter);
    
    }
    

    The 3rdPartyAppExample is a Winform App with 2 forms. I reference this application and call Form1’s Public method to show Form2:

    public partial class Form1 : Form
    {        
        public Form1()
        {
            InitializeComponent();
        }
        public void ShowForm2()
        {
            var f = new Form2();
            f.Show();
        }
    

    Optionally you may wish to check the Windows Caption:

      hInst = ProcessStart("calc.exe")
    
      // Begin search for handle
      hWndApp = GetWinHandle(hInst)
    
      If hWndApp <> 0 Then
       // Init buffer
       buffer = Space$(128)
    
       // Get caption of window
       numChars = GetWindowText(hWndApp, buffer, Len(buffer))
    

    The other solution (that isn’t very stable) is discussed here:
    http://www.shloemi.com/2012/09/solved-setforegroundwindow-win32-api-not-always-works/

    The trick is to make windows ‘think’ that our process and the target window (hwnd) are related by attaching the threads (using AttachThreadInput API).

    Regarding the myTopForm.TopMost = true answer, that will not work for external applications.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a GUI application within which i'm spawning a console application using Process
I have a GUI application which can create many similar windows on desktop. All
I'm writing a GUI application using Qt 4.7 on Windows 7 Ultimate 32 bit.
I want to invoke an external GUI application from a python script which will
Which standalone Windows GUI application do you recommend for use for accessing a Subversion
I have a Windows GUI application that's using the Qt framework (currently version 3.3.5,
I am starting a new GUI application project using Qt and OpenGL for Linux/Windows
I plan to write a stand-alone GUI application using which I can generate APK
In my standalone GUI application, I will install a Windows Closed handle on the
I am writing a GUI application using Django 1.1.1. This is the views.py: from

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.