What does Process.Responding really mean?
Asked Answered
F

3

10

I am shelling out to do some work and one of the requirements is to kill the process if it is hung.
My first thought was Process.Responding, however, I am not sure what it really means.

Is it the same thing as when Win7 adds a (Not Responding) to the window title of an application? On my machine, this happens even when MS Word tries to open a file from a really slow remote share.

What are the conditions for having a Process.Responding be false?

Fremd answered 21/7, 2011 at 19:47 Comment(1)
It is exactly what it says: This has always been one of those un-userfriendly messages that really comes from technical implementation details: the message loop is not responding.Cobbler
F
17

Under the hood, when you check the Process.Responding property, the Windows function SendMessageTimeout is called.

This function basically sends a message to the main window of another process and checks whether the window is accepting the message within a timeout of 5000 ms (Therefore checking this property on a console application has no effect).

If you want to use a custom timeout you can just as well call the SendMessageTimeout function yourself:

[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern IntPtr SendMessageTimeout(
    HandleRef hWnd, 
    int msg, 
    IntPtr wParam, 
    IntPtr lParam, 
    int flags, 
    int timeout, 
    out IntPtr pdwResult);

const int SMTO_ABORTIFHUNG = 2;

public bool RespondingWithinMs(Process process, int timeoutMs)
{
    IntPtr ptr2;
    return SendMessageTimeout(
        new HandleRef(process, process.MainWindowHandle), 
        0, 
        IntPtr.Zero, 
        IntPtr.Zero, 
        SMTO_ABORTIFHUNG, 
        timeoutMs,
        out ptr2) != IntPtr.Zero;
}
Fenella answered 21/7, 2011 at 19:54 Comment(2)
Process.Responding source: Note they pass in 5000ms timeout explicitly. An MSDN page for SendMessageTimeout here has more info than your MSDN link, this one clarifies that "This function considers that a thread is not responding if it has not called GetMessage or a similar function within five seconds"--I assume this only affects the SMTO_ABORTIFHUNG flag, that this is a different, hard-coded 5 second check.Sluff
But if this has some performance issues for checked process app or the app which check the process?Ghost
F
4

Responding means the application window is responding to the user. the process should have a MainWindowHandle from msdn:

true if the user interface of the associated process is responding to the system; otherwise, false.

If the process does not have a MainWindowHandle, this property returns true.

You can modify the timeout used by application.Responding check this.

Fayum answered 21/7, 2011 at 19:52 Comment(0)
M
3

From http://msdn.microsoft.com/en-us/library/system.diagnostics.process.responding.aspx

If a process has a user interface, the Responding property contacts the user interface to determine whether the process is responding to user input. If the interface does not respond immediately, the Responding property returns false. Use this property to determine whether the interface of the associated process has stopped responding.

Mcleroy answered 21/7, 2011 at 19:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.