Main函數
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
webscreenshot wb = new webscreenshot();
Process current = Process.GetCurrentProcess();
bool newinstance = true;
Process[] processes = Process.GetProcessesByName(current.ProcessName);
//遍曆正在有相同名字啟動並執行常式
foreach (Process process in processes)
{
//忽略現有的常式
if (process.Id != current.Id)
{
//確保常式從EXE檔案運行
if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
{
//返回另一個常式執行個體
current = process;
newinstance = false;
break;
}
}
}
if (newinstance)
{
Application.Run(wb);
}
else
{
ShowWindowAsync(current.MainWindowHandle, 1);
//設定真執行個體程為foreground window
SetForegroundWindow(current.MainWindowHandle);
}
}
引入這兩個API函數
[DllImport("User32.dll")]
private static extern bool ShowWindowAsync( IntPtr hWnd, int cmdShow);
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
}