繁体   English   中英

c#等到启动进程对话框关闭

[英]c# wait until dialog of started process closes

如何等待(阻止)我的程序,直到关闭之前启动进程的特定对话框?

我正在启动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;
}

这可能会帮助您,但不能完全控制您。 我对选美不熟悉,所以不确定它是否在后台运行。 但是,如果程序自动关闭,则可以在应用程序中执行此操作。

因此,您可以在循环中检查Pageant应用程序是否打开,一旦打开,便执行一些代码,一旦关闭,便再次启用该程序。

在某些后台工作程序中执行此代码。

    //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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM