Jump to content

Checking if process is suspended/hanged/not responding


Recommended Posts

Hello everyone,

after searching this forum ower and ower again i couldn't find solution to my problem or i didn't understand hints that were given in some of the threads i found.

Let me explain what am i trying to do.

I have a program that hangs from time to time for many different reasons. Now what am i trying to do is to write an AutoIt script that will be checking if program is running as it should or if its hanged/crashed/not responding (i dont know if those program states are all the same).

After some searching on forum i found a code which is calling a user32.dll function called "IsHungAppWindow" (see code bolow):

If _NotResponding("Calculator", "", 1)  Then
    MsgBox(1,"Test","Process hanged")
Else
    MsgBox(1,"Test","Process not hanged")
EndIf

Func _NotResponding($title, $text, $closeIfHung = 0)
    $hWnd = WinGetHandle($title, $text)

If $hWnd == "0" Then
MsgBox(1,"Tets","Window not found")
EndIf
    $retArr = DllCall("user32.dll", "int", "IsHungAppWindow", "hwnd", $hWnd)
    If @error == 0 Then
        If $retArr[0] == 1 Then
            If $closeIfHung Then
                ProcessClose(WinGetProcess($title, $text))
            EndIf
            Return 1
        EndIf
    Else
        Return 0
    EndIf
EndFunc

I was using windows Calculator to test this code. So what i did was i downloaded some program from CodeProject which is desigend to Suspend process ... and above posted code does not return correct MSGBox.

I am not sure if code doesnt work or the entire logic is wrong?

I hope some of you could help me a bit.

Thank you for your replays.

Best regards.

Link to comment
Share on other sites

I just realized i have another option but i dont know if its doable.

Is there a settings in windows 7 where you can choose to automatically close non responding programs or something similar.

I could do a simple procesexist search every x seconds and wouldnt need to check if application is running or its hanged.

Link to comment
Share on other sites

In this post the function SendMessageTimeoutW is discussed. I was thinking all along that this might be a good method to check if a window is hung. Set the flags correctly (SMTO_ABORTIFHUNG, SMTO_ERRORONEXIT) with an acceptable timeout (5 seconds?), send something like WM_NULL or an empty WM_COPYDATA or something else not interfering with the program itself and wait for a response (0 on timeout)... will probably have time in the evening to try something :blink:...
Link to comment
Share on other sites

In this post the function SendMessageTimeoutW is discussed. I was thinking all along that this might be a good method to check if a window is hung. Set the flags correctly (SMTO_ABORTIFHUNG, SMTO_ERRORONEXIT) with an acceptable timeout (5 seconds?), send something like WM_NULL or an empty WM_COPYDATA or something else not interfering with the program itself and wait for a response (0 on timeout)... will probably have time in the evening to try something ;)...

Ok i will try this even tho i am most certain i will fail :P.

I am wondering if there is a difference when program is :

a) hanged

:blink: not responding

c) something else.

In the mean while i have found some "solutio" on this website. Creating short cut on desktop which closes any non responging programs in one click so i can simply call it from my script.

Any more feedback is welcomed.

thanks for now.

Edit: I have found an even better solution ... what you need to do is a bit of regedit. Link

Edited by amakrkr
Link to comment
Share on other sites

'IsHungAppWindow' will work in certain scenarios. However, applications with threads that are Suspended will not usually be reported as 'Hung'. Calling a function with 'SendMessageTimeout' is a good secondary detection mechanism though, and if that fails (when called with flag 'SMTO_ABORTIFHUNG') then you can call 'GetLastError' and see if it returns error code 1460 (ERROR_TIMEOUT). I use both of these methods in my 'Full Screen Crash Recovery' program, and it works well.

Link to comment
Share on other sites

'IsHungAppWindow' will work in certain scenarios. However, applications with threads that are Suspended will not usually be reported as 'Hung'. Calling a function with 'SendMessageTimeout' is a good secondary detection mechanism though, and if that fails (when called with flag 'SMTO_ABORTIFHUNG') then you can call 'GetLastError' and see if it returns error code 1460 (ERROR_TIMEOUT).

Thanks for the infos m8, will give it a try :P (vacation next week ;))...

I use both of these methods in my 'Full Screen Crash Recovery' program, and it works well.

Just checked it out but saw no source :blink: ...
Link to comment
Share on other sites

Just checked it out but saw no source :blink: ...

haha, I know. The only project I had that I closed the source on - though who woulda thunk it, its out there (an older version) on some website of course.

Anyway, I was just referring to my app because the method discussed has worked very well for it in detecting pesky crashed/hung applications. I just realized now though that there's a note on the MSDN website about Windows 2000 not returning the right error code when using 'GetLastError'. Argh, guess that should be fixed now..

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...