Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Process
Hi
 
In a C# application, how do I bring the instance of an application that was created using Process.Start() to the foreground?
 
Thanks
K
Posted 7-Nov-11 1:08am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Try:
        [DllImport("User32.dll")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);
        private IntPtr handle;
        private Process process;
        private void button1_Click(object sender, EventArgs e)
            {
            process = Process.Start("cmd");
            }
 
        private void button2_Click(object sender, EventArgs e)
            {
            handle = process.MainWindowHandle;
            SetForegroundWindow(handle);
            }
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Solution 1 will work just fine for your need. But I just wanted to add that if the windows happens to be minimized you will need to use something like this:
 
[System.Runtime.InteropServices.DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr handle);
[System.Runtime.InteropServices.DllImport("User32.dll")]
private static extern bool ShowWindow(IntPtr handle, int nCmdShow);
[System.Runtime.InteropServices.DllImport("User32.dll")]
private static extern bool IsIconic(IntPtr handle);
 
const int SW_RESTORE = 9;
 
Process proc = Process.Start("cmd.exe");
 
private void button1_Click(object sender, EventArgs e)
{
    IntPtr handle = proc.MainWindowHandle;
    if (IsIconic(handle))
    {
        ShowWindow(handle, SW_RESTORE);
    }
 
    SetForegroundWindow(handle);
}
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Thank you for your responses. I tried the solution mentioned above, with my application. Still I am not able to bring the newly created process to the foreground, it is still in the background. When I spawn the new process with cmd.exe using the solution provided, it does bring the command prompt to the forgeround.
 
But when I do the same with my own application, it is still in the background. The gist of what I am trying to do is to create a second instance of the application spawned when the user selects File->Open option in the application, when a file is already opened. This way, the second file will be opened in another instance of the application (similar to MS Word). Below is my code, where MainForm is the main form of my MDI application. The true flag indicates a second instance of the application had to be created.
 
IntPtr handle;
Process process = 
    Process.Start(typeof(MainForm).Assembly.Location, "true");
    handle = process.MainWindowHandle;
SetForegroundWindow(handle);
 

Any idea/suggestion, why I am not able to bring the second instance of my application to the foreground is greatly appreciated!
 
Thanks in advance
K
  Permalink  
v2
Comments
LanFanNinja at 7-Nov-11 12:21pm
   
I don't know? This code totally works for me
 
private void button1_Click(object sender, EventArgs e)
{
IntPtr handle;
Process proc =
Process.Start(typeof(MainForm).Assembly.Location, "true");
handle = proc.MainWindowHandle;
 
if (IsIconic(handle))
{
ShowWindow(handle, SW_RESTORE);
}
 
SetForegroundWindow(handle);
}
 
actually it work correctly without the call to SetForegroundWindow(handle);
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Hi, had the same problem, hope this will help:
 
The trick is to make windows ‘think’ that our process and the target window (hwnd) are related by attaching the threads (using AttachThreadInput API) and using an alternative API: BringWindowToTop.
 

Forcing Window/Internet Explorer to the foreground:
private static void ForceForegroundWindow(IntPtr hWnd)
{
    uint foreThread = GetWindowThreadProcessId(GetForegroundWindow(), IntPtr.Zero);
    uint appThread = GetCurrentThreadId();
    const uint SW_SHOW = 5;
 
    if (foreThread != appThread)
    {
        AttachThreadInput(foreThread, appThread, true);
        BringWindowToTop(hWnd);
        ShowWindow(hWnd, SW_SHOW);
        AttachThreadInput(foreThread, appThread, false);
    }
    else
    {
        BringWindowToTop(hWnd);
        ShowWindow(hWnd, SW_SHOW);
    }
}
 
A more advanced implementation of AttachedThreadInputAction pattern
 
The idea is to attach to the target process thread and preform an action.
 
Code Snippet
public static void AttachedThreadInputAction(Action action)
{
    var foreThread = GetWindowThreadProcessId(GetForegroundWindow(), IntPtr.Zero);
    var appThread = GetCurrentThreadId();
    bool threadsAttached = false;
    try
    {
        threadsAttached =
            foreThread == appThread ||
            AttachThreadInput(foreThread, appThread, true);
        if (threadsAttached) action();
        else throw new ThreadStateException(“AttachThreadInput failed.”);
    }
    finally
    {
        if (threadsAttached)
            AttachThreadInput(foreThread, appThread, false);
    }
}
Usage
 
Code Snippet
public const uint SW_SHOW = 5;
 
///<summary>
/// Forces the window to foreground.
///</summary>
///hwnd”>The HWND.</param>
public static void ForceWindowToForeground(IntPtr hwnd)
{
    AttachedThreadInputAction(
        () =>
        {
            BringWindowToTop(hwnd);
            ShowWindow(hwnd, SW_SHOW);
        });
}
 
public static IntPtr SetFocusAttached(IntPtr hWnd)
{
    var result = new IntPtr();
    AttachedThreadInputAction(
        () =>
        {
            result = SetFocus(hWnd);
        });
    return result;
}
 

The full article can be found here:
http://www.shloemi.com/2012/09/solved-setforegroundwindow-win32-api-not-always-works/[^]
  Permalink  
v3
Comments
Besic Denis at 3-Apr-13 8:07am
   
What about BIG NOTE colored in red on top of article. Did you have problems with stability of application.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 240
1 Kamal Rocks 184
2 BillWoodruff 173
3 PIEBALDconsult 160
4 CPallini 155
0 OriginalGriff 5,695
1 DamithSL 4,506
2 Maciej Los 4,007
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 9 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100