如何等待(阻止)我的程序,直到我之前启动的进程的特定对话框关闭?
我正在启动 pageant.exe 来加载 ssh 密钥。选美从“进程”类开始。这工作正常。
我的 ssh 密钥有一个密码。所以我的主程序/进程(这个启动进程)必须等到用户输入 ssh 密钥密码。
我知道如何等待,但不知道如何在 c# 中执行此操作:如果选美要求输入密码,则会出现一个对话框。所以我的主程序/进程可以等到密码对话框关闭。是否可以在 c# 中执行此操作?
我从这里得到了这个想法。
编辑:找到解决方案
// wait till passphrase dialog closes
if(WaitForProcessWindow(cPageantWindowName))
{ // if dialog / process existed check if passphrase was correct
do
{ // if passphrase is wrong, the passphrase dialog is reopened
Thread.Sleep(1000); // wait till correct passphrase is entered
} while (WaitForProcessWindow(cPageantWindowName));
}
}
private static bool WaitForProcessWindow(string pProcessWindowName)
{
Process ProcessWindow = null;
Process[] ProcessList;
bool ProcessExists = false; // false is returned if process is never found
do
{
ProcessList = Process.GetProcesses();
ProcessWindow = null;
foreach (Process Process in ProcessList)
{ // check all running processes with a main window title
if (!String.IsNullOrEmpty(Process.MainWindowTitle))
{
if (Process.MainWindowTitle.Contains(pProcessWindowName))
{
ProcessWindow = Process;
ProcessExists = true;
}
}
}
Thread.Sleep(100); // save cpu
} while (ProcessWindow != null); // loop as long as this window is found
return ProcessExists;
}