Process -> MainWindowHandle always coming back as 0???

  • Thread starter Thread starter Steven Van Dyke
  • Start date Start date
S

Steven Van Dyke

I'm using a mutex to ensure only 1 instance of my app is created. It works
fine.

If an instance of my app IS already running, I want to use the following
code to bring the window to the foreground. BUT, the MainWindowHandle is
always 0 (I checked all processes and they are all 0). How can I get the
window handle back? Or, is there another way to popup my application?

Thanks,

Steve

if (!grantedInitialOwnership)
{
Process[] ps =
Process.GetProcessesByName("AppName");
IntPtr hWnd = ps[0].MainWindowHandle;

// Show existing instance
if (hWnd.ToInt64() != 0)
{
SetForegroundWindow(hWnd);
ShowWindow(hWnd, 5); // SW_SHOW
}
}
 
Steven,
Process[] ps =
Process.GetProcessesByName("AppName");
IntPtr hWnd = ps[0].MainWindowHandle;

You're only checking the first process found, which might be your
currently executing process that hasn't displayed its main window yet
(thus the null handle). You should loop through all processes returned
by GetProcessesByName().



Mattias
 
Back
Top