模态的MessageBox上的另一过程处理可能锁止目标过程
-
12-09-2019 - |
题
如果我告诉一个MessageBox作为另一个进程中的窗口模式,它工作得很好,只要我的程序仍响应。如果它被关闭或同时在MessageBox是示出了接收在MessageBox将被锁定(但仍响应)的窗口和它将具有通过任务管理器来完成。终止
下面是一个示例代码以证明:
using System;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;
namespace TestMessageBox
{
class Program
{
private WindowWrapper notepad;
Program(IntPtr handle)
{
notepad = new WindowWrapper(handle);
}
static void Main(string[] args)
{
Process[] procs = Process.GetProcessesByName("notepad");
if (procs.Length > 0)
{
Console.WriteLine("Notepad detected...");
Program program = new Program(procs[0].MainWindowHandle);
Thread thread = new Thread(new ThreadStart(program.ShowMessage));
thread.IsBackground = true;
thread.Start();
Console.Write("Press any key to end the program and lock notepad...");
Console.ReadKey();
}
}
void ShowMessage()
{
MessageBox.Show(notepad, "If this is open when the program ends\nit will lock up notepad...");
}
}
/// <summary>
/// Wrapper class so that we can return an IWin32Window given a hwnd
/// </summary>
public class WindowWrapper : System.Windows.Forms.IWin32Window
{
public WindowWrapper(IntPtr handle)
{
_hwnd = handle;
}
public IntPtr Handle
{
get { return _hwnd; }
}
private IntPtr _hwnd;
}
}
如何避免?
解决方案
表示一个模态对话框的行为禁用对话框的父窗口(记事本的在您的示例窗口)。当关闭所述模态对话框时,获取重新启用父窗口。
如果你的程序死掉之前重新启用的窗口,该窗口将永远不会重新启用 - 它是由一下显示的对话框中重新启用父线程。 (在您的例子,它发生MessageBox.Show()
内,用户点击OK或任何后。)
,使这项工作的唯一方法是将有第二过程,其职责是把东西回来,因为他们应该是,如果此过程中创造的模式对话框去世过早,但是这太可怕了。它仍然不是刀枪不入 - ?如果什么观察者进程死掉太
不隶属于 StackOverflow