1

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;
}
Pas
  • 11
  • 3
  • 1
    We're generally not here to convert Perl script to c# for you. [ask]. Also checkout the fun facts at [mcve] –  Sep 23 '16 at 07:58
  • This may help http://stackoverflow.com/a/3147920/6248956 – YuvShap Sep 23 '16 at 08:11

1 Answers1

0

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
usselite
  • 846
  • 7
  • 24
  • All that does is check if the process is running or not, not if a dialog is open. Plus it maxes out the CPU –  Sep 24 '16 at 01:21