Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does Process.Responding really mean?

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?

like image 309
AngryHacker Avatar asked Jul 21 '11 19:07

AngryHacker


3 Answers

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;
}
like image 161
Dirk Vollmar Avatar answered Sep 28 '22 01:09

Dirk Vollmar


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.

like image 34
Bob G Avatar answered Sep 28 '22 02:09

Bob G


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.

like image 39
Jalal Said Avatar answered Sep 28 '22 02:09

Jalal Said