写wpf单例启动程序时,在某台电脑上报了这个错,微软的帮助文档是这么说的
复制如下
此单实例应用程序未能连接到原始实例
此单实例应用程序未能连接到原始实例。 一些可能导致此问题的原因包括:
-
原始实例停止了响应。
-
应用程序没有创建内核对象的权限。 有关内核对象的详细信息,请参阅互斥体。
内核对象的基名称是通过串联程序集的 GUID、主版本号和次版本号得到的。 例如,基名称可能是
3639f15d-9547-43da-8145-60da347829915.1
。
在开发应用程序时更正此错误
-
检查应用程序未进入未响应状态。
-
检查应用程序是否具有创建内核对象的足够权限。
-
重启应用程序的原始实例。
-
重启计算机,以清除可能正在使用连接到原始实例应用程序所需资源的任何进程。
-
记录发生错误的环境,并与 Microsoft 产品支持服务联系。
而查了半天也没解决,只能用别的方式启动。最后选用的是PostMessage方式。
方案如下:
主窗口注册响应事件
private void Window_Loaded(object sender, RoutedEventArgs e)
{
if (System.Windows.Application.Current.MainWindow == this)
{
IntPtr hwnd = new WindowInteropHelper(this).Handle;
HwndSource.FromHwnd(hwnd).AddHook(new HwndSourceHook(WndPrc));
}
}
//2049时打开窗口
private IntPtr WndPrc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg == 2048)
{
this.Tag = "True";
this.Close();
}
else if (msg == 2049)
{
System.Windows.Application.Current.MainWindow.ShowActivated = true;
System.Windows.Application.Current.MainWindow.Show();
System.Windows.Application.Current.MainWindow.Activate();
System.Windows.Application.Current.MainWindow.WindowState = WindowState.Normal;
}
return IntPtr.Zero;
}
调用程序时的逻辑:
[DllImport("USER32.DLL")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);
[DllImport("User32.dll", EntryPoint = "FindWindow")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("User32.dll", EntryPoint = "FindWindowEx")]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpClassName, string lpWindowName);
[DllImport("User32.dll", EntryPoint = "SendMessage")]
private static extern IntPtr SendMessage(int hWnd, int msg, IntPtr wParam, IntPtr lParam);
[DllImport("User32.dll", EntryPoint = "PostMessage")]
public static extern IntPtr PostMessage(int hWnd, int msg, IntPtr wParam, IntPtr lParam);
IntPtr hWnd1= FindWindowEx(IntPtr.Zero,IntPtr.Zero,null, "MainWindow");
if (hWnd1 != IntPtr.Zero)
{
while (hWnd1 != null && hWnd1 != IntPtr.Zero)
{
PostMessage(hWnd1.ToInt32(), 2049, new IntPtr(14), IntPtr.Zero);
hWnd1 = FindWindowEx(IntPtr.Zero, hWnd1, null, "MainWindow");
}
}
else
{
try {
//todo 正常的单例启动
}
catch { }
}
补充:
在获取窗口句柄时,最初用的是Process[] processes = Process.GetProcessesByName("TestWindow");然后传参时,用p.MainWindowHandle。正常情况下,该方法可以获得窗口句柄,但是当窗口进入托盘模式时,p.MainWindowHandle一直为0,故放弃该方式。
后来采用IntPtr hWnd = FindWindow(null, "MainWindow"); 这种方式只能获取Z轴最上面的那个窗口(简单理解为最早打开的窗口),如果存在多个相同名称的窗口,只会有一个收到,故最终选择FindWindowEx 方式。
而窗口打开如果使用SetForegroundWindow,这个只会把窗口从下层显示到最上层,窗口处于最小化时是没效果的。
SwitchToThisWindow(hWnd, true);这个在最小化时可以把窗口激活,并且显示出来,但是在托盘图标下是没效果的。
而SendMessage,会等待相应事件完成后返回,考虑到使用场景不需要等待窗口打开,故采用PostMessage
最终选择上面的方式。