简体   繁体   English

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

[英]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.

相关问题 C#CancelButton关闭对话框? - C# CancelButton closes dialog? 如何等待进程终止后才能继续执行-C# - How to wait until a process terminates before continuing to execute - C# 如何使主窗口等到C#WPF中新打开的窗口关闭? - How to make main window wait until a newly opened window closes in C# WPF? 当应用程序在C#中关闭时,如何自动关闭由应用程序启动的进程? - How to automatically close the process that is started by application, when application closes in c#? 由C#启动的应用程序保持运行状态,直到主线程关闭 - An application initiated by C# stays as running process until main thread closes C# - 制作一个 Process.Start 等到进程启动 - C# - Making a Process.Start wait until the process has start-up C#:如何开启一个进程,等待进程中的程序加载完成,然后使用新的进程执行命令? - C#: How can I open a process, wait until the program in the process loads completely, and then use a new process to execute commands? 如何使用(Delegate方法)等待直到线程处理在C#中完成 - How to wait until thread process finishes in C# using (Delegate Method) C#-告诉Process.Start等待直到上一个实例完成 - C# - Telling Process.Start to wait until previous instance has finished C#等待进程启动 - C# wait for process to start
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM