Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 确保WPF应用程序的单个实例:难以将已运行的应用程序恢复到前台_C#_Wpf - Fatal编程技术网

C# 确保WPF应用程序的单个实例:难以将已运行的应用程序恢复到前台

C# 确保WPF应用程序的单个实例:难以将已运行的应用程序恢复到前台,c#,wpf,C#,Wpf,意图:运行WPF应用程序的单个实例。启动新实例时,应将已运行的实例设置为前台 虽然我已经实现了大部分,但当已经运行的应用程序位于通知栏中时,我面临一个问题。代码运行时没有错误,但无法还原窗口并将其设置为前台代码片段(c#): [DllImport("user32.dll")] private static extern bool SetForegroundWindow(IntPtr hWnd); [DllImport("user32.dll")] private static extern b

意图:运行WPF应用程序的单个实例。启动新实例时,应将已运行的实例设置为前台

虽然我已经实现了大部分,但当已经运行的应用程序位于通知栏中时,我面临一个问题。代码运行时没有错误,但无法还原窗口并将其设置为前台代码片段(c#):

[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);

[DllImport("user32.dll")]
private static extern bool IsIconic(IntPtr hWnd);

[DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hWnd, int cmdShow);

var currentProcess = Process.GetCurrentProcess();

string processName = currentProcess.ProcessName;
Process[] instances = Process.GetProcessesByName(processName);

if (instances.Length > 1)
{
    foreach(var instance in instances)
    {
        if (!currentProcess.Id.Equals(instance.Id))
        {
            IntPtr hWnd = instance.MainWindowHandle;

            if (IsIconic(hWnd))
                ShowWindow(hWnd, SW_RESTORE);

            SetForegroundWindow(hWnd);
        }
    }
    currentProcess.Kill();
}
谁能指出我做错了什么。再次重申,当已经运行的窗口处于最大化状态时,它会起作用,但事后看来是这样。当已经运行的窗口最小化到通知托盘时,它将失败


谢谢

您是否验证过,当已经运行的进程在systray中时,您的代码是否执行了
ShowWindow
?我这样问是因为我不认为IsIconic是正确的函数:它表示“确定指定的窗口是否最小化”。如果进程在systray中,则它不是最小化的,而是隐藏的

我想你应该改用