How can I tell if a process has a graphical interface?

前端 未结 4 980
夕颜
夕颜 2021-01-11 16:16

I\'m using automation to test an application, but sometimes I want to start the application via a batch file. When I run \"process.WaitForInputIdle(100)\" I get an error:

4条回答
  •  独厮守ぢ
    2021-01-11 16:56

    I was think along the lines of this, Still ugly but trys to avoid exceptions.

    Process process = ...
    
    bool hasUI = false;
    
    if (!process.HasExited)
    {
        try
        {
            hasUI = process.MainWindowHandle != IntPtr.Zero;
        }
        catch (InvalidOperationException)
        {
            if (!process.HasExited)
                throw;
        }
    }
    
    if (!process.HasExited && hasUI)
    {
    
        try
        {
            process.WaitForInputIdle(100);
        }
        catch (InvalidOperationException)
        {
            if (!process.HasExited)
                throw;
        }
    }
    

提交回复
热议问题