如何等待(阻止)我的程序,直到我先前启动的进程的特定对话框关闭?c#等待,直到启动进程的对话框关闭
我正在启动pageant.exe来加载ssh密钥。 Pageant从类“Process”开始。这工作正常。
我的ssh密钥有一个密码。所以我的主程序/进程(这个启动进程)必须等到用户输入ssh密钥密码。
我有一个想法如何等待,但不知道如何在c#中执行此操作: 如果选美会询问密码,将出现一个对话框。所以我的主程序/进程可以等到密码对话框关闭。是否有可能在C#中做到这一点?
我从here得到了想法。
编辑:找到一个解决办法
// 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;
}
我们通常不会在这里将Perl脚本转换为C#。 [问]。还可以在[mcve] – MickyD
处查看有趣的事实。这可能有助于http://stackoverflow.com/a/3147920/6248956 – YuvShap