Bringing a program to the front

  • Thread starter Thread starter Thore Berntsen
  • Start date Start date
T

Thore Berntsen

Does anyone know how I can check if a program is running, if the answer is
yes then bring it to the front, if no start it?

Thore Berntsen
 
to find out what is running you can pinvoke CreateToolhelp32Snapshot and
Process32First and Process32Next
 
There are different approaches for this. One of this method is use
FindWindow, SetForegroundWindow:

// in the main function
IntPtr nWnd = FindWindow(null, "Main Window Title");

if(nWnd != IntPtr.Zero)
{
SetForegroundWindow(nWnd);
Application.Exit();
}

....

[DllImport("coredll",EntryPoint="FindWindow")]
private static extern IntPtr FindWindow(string className, string
windowName);

[DllImport("coredll",EntryPoint="SetForegroundWindow")]
private static extern bool SetForegroundWindow(IntPtr hWnd);




Best regards,
Sergey Bogdanov
 
Back
Top