Detect if external process is interactive and has any visible UI

后端 未结 2 1227
不知归路
不知归路 2021-01-17 03:48

I cannot seem to find a way to determine whether a Process has a user interface e.g. a window, which is visible to the user?

  • Environment.UserInte
相关标签:
2条回答
  • 2021-01-17 04:12
    1. Find out the process ID from your Process instance.
    2. Enumerate the top-level windows with EnumWindows.
    3. Call GetWindowThreadProcessId and see if it matches the target PID.
    4. Call IsWindowVisible and/or IsIconic to test if that window is visible to the user.
    0 讨论(0)
  • 2021-01-17 04:15

    The MSDN article about System.Diagnostics.Process.MainWindowHandle states the following

    If you have just started a process and want to use its main window handle, consider using the WaitForInputIdle method to allow the process to finish starting, ensuring that the main window handle has been created. Otherwise, an exception will be thrown.

    What they are implying is that the Window might take several seconds to render after you've made the call for the MainWindowHandle, returning IntPtr.Zero even though you can clearly see a Window is shown.

    See https://msdn.microsoft.com/en-us/library/system.diagnostics.process.mainwindowhandle(v=vs.110).aspx for reference

    0 讨论(0)
提交回复
热议问题