创建Wpf单实例
程序员文章站
2022-07-04 16:22:45
...
创建互斥体Mutex(命名空间:using System.Threading)
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
bool isNewInstance;
Mutex mutex = new Mutex(true,"新实例",out isNewInstance);
if(!isNewInstance)
{
IntPtr intptr= FindWindowW(null, "Window1");
if(intptr!=IntPtr.Zero)
{
SetForegroundWindow(intptr);
}
Shutdown();
}
}
[DllImport("User32", CharSet = CharSet.Unicode)]
static extern IntPtr FindWindowW(string lpClassName, string lpWindowName);//导入找到windwo窗口方法
[DllImport("User32", CharSet = CharSet.Unicode)]
static extern bool SetForegroundWindow(IntPtr hWnd);//导入设置窗口到前端方法
上一篇: Android spinner控件的实现
下一篇: kotlin实现简单的spinner