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 is started with the class "Process". This works fine.
My ssh key has a passphrase. So my main program/process (this one started the process) has to wait until the user entered the ssh key passphrase.
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. So my main program/process can wait until the passphrase dialog is closed. Is it possible to do this in 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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.