日期:2014-05-20 浏览次数:20869 次
1 /// Summary description for ProcessUtils.
2 public static class ProcessUtils
3 {
4 private static Mutex mutex = null;
5
6 /// Determine if the current process is already running
7 public static bool ThisProcessIsAlreadyRunning()
8 {
9 // Only want to call this method once, at startup.
Debug.Assert(mutex == null);
// createdNew needs to be false in .Net 2.0, otherwise, if another instance of
// this program is running, the Mutex constructor will block, and then throw
// an exception if the other instance is shut down.
bool createdNew = false;
mutex = new Mutex(false, Application.ProductName, out createdNew);
Debug.Assert(mutex != null);
return !createdNew;
}
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
static extern bool IsIconic(IntPtr hWnd);
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
const int SW_RESTORE = 9;