Find if process is responding without using System.Diagnostics.Process.Responding

后端 未结 1 630
星月不相逢
星月不相逢 2020-12-22 04:45

Good day everyone.

This problem was part of another one which it as been solved, i realized that what i thought it was the problem after all, it was

相关标签:
1条回答
  • 2020-12-22 05:04

    IE is special because each tab has its own process plus there is an additional parent IE process. In fact, only the parent process will have a valid MainWindowHandle.

    Did you check whether MainWindowHandle is null for all these processes? If it isn't I think your code should work as expected on XP as well.

    Update

    Since checking all IE instances didn't help, the next thing I would try is to modify the timeout that is used by Process.Responding. The property internally calls the SendMessageTimeout api function and then checks the return value whether a timeout occurred. If so, the process is assumed to be hanging. The timeout is a hard-coded value of 5 seconds.

    You can call SendMessageTimeout yourself using P/Invoke and vary the timeout. Possibly a shorter value would give better results on Windows XP:

    [DllImport("user32.dll", CharSet=CharSet.Auto)]
    static extern IntPtr SendMessageTimeout(
        HandleRef hWnd, 
        int msg, 
        IntPtr wParam, 
        IntPtr lParam, 
        int flags, 
        int timeout, 
        out IntPtr pdwResult);
    
    const int SMTO_ABORTIFHUNG = 2;
    
    bool IsResponding(Process process)
    {
        HandeRef handleRef = new HandleRef(process, process.MainWindowHandle);
    
        int timeout = 2000;
        IntPtr lpdwResult;
    
        IntPtr lResult = SendMessageTimeout(
            handleRef, 
            0, 
            IntPtr.Zero, 
            IntPtr.Zero, 
            SMTO_ABORTIFHUNG, 
            timeout, 
            out lpdwResult);
    
        return lResult != IntPtr.Zero;
    }
    
    0 讨论(0)
提交回复
热议问题