G
Guest
On May 3, I posted a question asking how to do this. A colleague and I
figured out how to do it. Thought I would pass it along.
// The following code prevents multiple instances of a program
// from being launched and if the currently executing one
// is minimized or covered by another window, it is restored.
[DllImport("User32.dll")]
static extern IntPtr SetForegroundWindow(IntPtr hWnd);
[DllImport("User32.dll")]
static extern IntPtr ShowWindow(IntPtr hWnd, int nCmdShow);
// At start-up - Get the number of instances of this app
Process[] procs = Process.GetProcessesByName(Application.ProductName);
if (procs.Length > 1)
{
// the previously running instance will be at either index 0 or 1
int index;
if ((int)procs[0].MainWindowHandle != 0) index = 0;
else index = 1;
SetForegroundWindow(procs[index].MainWindowHandle);
// 9 = SW_RESTORE (winuser.h)
ShowWindow(procs[index].MainWindowHandle, 9);
return; // exit, terminate this instance
}
figured out how to do it. Thought I would pass it along.
// The following code prevents multiple instances of a program
// from being launched and if the currently executing one
// is minimized or covered by another window, it is restored.
[DllImport("User32.dll")]
static extern IntPtr SetForegroundWindow(IntPtr hWnd);
[DllImport("User32.dll")]
static extern IntPtr ShowWindow(IntPtr hWnd, int nCmdShow);
// At start-up - Get the number of instances of this app
Process[] procs = Process.GetProcessesByName(Application.ProductName);
if (procs.Length > 1)
{
// the previously running instance will be at either index 0 or 1
int index;
if ((int)procs[0].MainWindowHandle != 0) index = 0;
else index = 1;
SetForegroundWindow(procs[index].MainWindowHandle);
// 9 = SW_RESTORE (winuser.h)
ShowWindow(procs[index].MainWindowHandle, 9);
return; // exit, terminate this instance
}