[英]c# wait until dialog of started process closes
How do I wait (block) my program until a specific dialog of my previous started process closes? 如何等待(阻止)我的程序,直到关闭之前启动进程的特定对话框?
I'm starting pageant.exe to load a ssh key. 我正在启动pageant.exe以加载ssh密钥。 Pageant is started with the class "Process". 选美开始于“过程”类。 This works fine. 这很好。
My ssh key has a passphrase. 我的ssh密钥有一个密码短语。 So my main program/process (this one started the process) has to wait until the user entered the ssh key passphrase. 因此,我的主程序/过程(此过程开始了该过程)必须等待,直到用户输入了ssh密钥密码。
I got an idea how to wait, but don't know how to do this in c#: If pageant ask for the passphrase a dialog appears. 我知道如何等待,但是不知道如何在c#中执行此操作:如果选美要求输入密码,则会出现一个对话框。 So my main program/process can wait until the passphrase dialog is closed. 因此,我的主程序/进程可以等待,直到关闭密码对话框。 Is it possible to do this in c#? 是否可以在C#中执行此操作?
I got the idea from here . 我从这里得到了这个主意。
EDIT: found a solution 编辑:找到了解决方案
// 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;
}
This might help you out, but doesn't give you entire control. 这可能会帮助您,但不能完全控制您。 I am not familiar with pageant, so I am not sure if it keeps running in the background or not. 我对选美不熟悉,所以不确定它是否在后台运行。 But in case the program automatically closes you could do this in your application. 但是,如果程序自动关闭,则可以在应用程序中执行此操作。
So you could check in a loop if the Pageant application is open or not, once it is open you execute some code and once it is closed you enable the program again. 因此,您可以在循环中检查Pageant应用程序是否打开,一旦打开,便执行一些代码,一旦关闭,便再次启用该程序。
Execute this code in some background worker. 在某些后台工作程序中执行此代码。
//Lets look from here if pageant is open or not.
while(true)
{
if (Process.GetProcessesByName("pageant").Length >= 1)
{
//block your controls or whatsoever.
break;
}
}
//pageant is open
while(true)
{
if (!Process.GetProcessesByName("pageant").Length >= 1)
{
//enable controls again
break;
}
}
//close thread
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.