更新时间:2023-11-22 21:05:22
传统的方式做,这是一个互斥体,如:
The traditional way to do this is with a mutex, e.g.
bool bNew = true;
using (Mutex mutex = new Mutex(true, "MYAPP_0D36E4C9-399D-4b05-BDA3-EE059FB77E8D", out bNew))
{
if (bNew)
{
// blah, blah,
Application.Run(new MainForm());
}
}
编辑:
我发现这个code网上调用SetForegroundWindow,所以你可以找到你的应用程序的其他实例,并显示它:
I found this code to invoke SetForegroundWindow online, so you can find the other instance of your app and bring it forward:
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);
Process me = Process.GetCurrentProcess();
foreach (Process process in Process.GetProcessesByName (me.ProcessName))
{
if (process.Id != me.Id)
{
SetForegroundWindow (process.MainWindowHandle);
break;
}
}
请注意,在现代的Windows实现,你只能给前台了。
Note that in modern Windows implementations you can only give the foreground away.