簡體   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