views:

218

answers:

4

Hi, i was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance.

Eg - Application X is running but is in the background. I cant see it so i try to run another instance and because the mutex returns false the best thing to do is to bring the existing instance to the foreground.

Anyone know the best way to interact with your own application but from a separate process?

+1  A: 

You will need to get a hWnd to the other application's main window. The only way I remember how to do that is to loop over all windows until you find yours. There might be a more direct way

[DllImport("user32.dll")] private static extern 
              int EnumWindows(EnumWindowsProc ewp, int lParam); 

public delegate bool EnumWindowsProc(int hWnd, int lParam);

public void EnumerateAllWindows()
{
   EnumWindowsProc ewp = new EnumWindowsProc(EvalWindow);
   EnumWindows(ewp, 0);
 }

private bool EvalWindow(int hWnd, int lParam)
{
    //this will be called for each window..         
    // use GetWindowThreadProcessId to get the PID for each window
    // and use Process.GetProcessById to get the PID you are looking for
    // then bring it to foregroun using of these calls below:

}

    [DllImport("user32.dll")]
    public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

    [DllImport("user32.dll")]
    public static extern bool BringWindowToTop(IntPtr hWnd);

    DllImport("user32.dll")] public static extern bool SetForegroundWindow(IntPtr hWnd
Radu094
thanks Radu, how can i get the main window hWnd?
Grant
+2  A: 

one way of performing your goal is to create a named shared memory.

the first application will create the named shared memory and store inside its window handle, process identifier, or any other information useful for this purpose.

when the second application tries to create a shared memory with the same name, it will retrieve a handle for the same shared memory, while the return value will tell you that this object was already existing. the second application can then read the window handle from the shared memory and any other information. it can then use BringWindowToTop(), SetForegroundWindow() or such.

you will find an example of a named shared memory in the Windows SDK. while this example uses straight Win32 API, i am pretty sure you can perform something equivalent in C#.

Adrien Plisson
+1  A: 

There is a feature built into .Net to let a second instance of your application communicate with the first instance by raising an event in the first instance. Although it's part of VB.Net, it can be used with C# too.

However it's very important to be aware of the bugs. With some firewalls, it's impossible to open even one instance - your application crashes at startup! See this excellent article by Bill McCarthy for more details, and an alternative technique. He communicates from a second instance back to the first instance with pipes in .NET 3.5. I recommend you use that. His code is VB.Net, but you could probably convert to C# with Telerik.

MarkJ
A: 

google for forceForegroundWindow (if you're on windows), anyway

rogerdpack