Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
WatchDog Thread
#1
Im running an application and pulling data from its screen. But sometimes the application freezes. It has nothing to do with the QM code, but the application im controling has a bug or a network induced timeout.


What I would like to do is to create a seporate thread, that will be set to cound down from, say 10 to 0. One step every second. The main program will be responsible to reset the counter value to 10 very frequently. So, if the value reaches 0, it will indicate that the main program is hung.

I belive this technique is called a watchDog timer.

I tryed to create a seporate macro using

mac "watchDogThread"

But it does not create a async thread. The watchDogThread only runs when the main thread exits.

I know its a simple mistake but Im not shure what Im doing wrong.

Anyone have any suggestions?


Attached Files
.doc   Select Record QM.doc (Size: 134.5 KB / Downloads: 254)
#2
Open watchDogThread, open Properties, and select Function.
#3
Everytime i waste your time Gintaras, i feal i should payback by contributing to the knowledge base. So Ill post this code in case anyone is intrested

WatchDog Timer

Everytime I start the application to scrape from, I also start the watchDog Thread

;; a few global variables
int+ watchDogCountDown
int+ watchDogLimit
int+ watchExec_handle

;; setup and run the thread
watchDogLimit = 60*5
watchDog_reset()
watchExec_handle = run("long_running_application_name.exe")
mac "watchDogThread"

--------------

function watchDog_reset

watchDogCountDown = watchDogLimit

--------------

function watchDogThread

rep
,watchDogCountDown - 1
,if(watchDogCountDown < 10) out "watchDog=%d" watchDogCountDown
,wait 1.0
,if(watchDogCountDown = 0)
,,clo watchExec_handle
,,err+
,,,out "watchDogStopped"
,,,ret
,,out "watchDogStopped"
,,ret


--------------------------------

In main loop of the scraping program put this

watchDog_reset()

This sets count down value to a high number and prevents the system from killing the program.

But if the program ever hangs the watchDog will wait for 5 min and close the program with

clo watchExec_handle

Gintras, QM is great stuff!!! I was a bit taken aback by the entire change in paradyme but now Im a believer.... It remindes me of an environemnt i worked with years ago called "forth". The environment yes, the language no. Once again, great contributuion. Thanks Gintras...
#4
watchExec_handle = run("long_running_application_name.exe")
...
clo watchExec_handle

I have to say that run returns not window handle, and it cannot be used with clo. To get window handle, use code like this:

Code:
Copy      Help
int window_handle
run "notepad.exe" "" "" "" 0x800 "- Notepad" window_handle
5
clo window_handle

Or get it later with win(...).

run returns process handle. It can be used with wait, and with some Windows API functions (dll functions), including TerminateProcess.

Code:
Copy      Help
int process_handle
process_handle=run("notepad.exe")
5
TerminateProcess process_handle 1
CloseHandle process_handle

Or:

Code:
Copy      Help
run "notepad.exe"
5
ShutDownProcess win("- Notepad")

TerminateProcess should be used only to terminate hung apps. It does not allow the app to execute any exit code.
#5
Try function IsHungAppWindow. Win 2000, XP and later. Read more in MSDN Library.

Code:
Copy      Help
dll user32 #IsHungAppWindow hWnd

int window_handle
run "notepad.exe" "" "" "" 0x800 "- Notepad" window_handle
rep
,1
,if(!IsWindow(window_handle)) break ;;closed
,if(IsHungAppWindow(window_handle))
,,ShutDownProcess window_handle
#6
Would this also work?

Code:
Copy      Help
dll user32 #IsHungAppWindow hWnd
act "My Dialog"
int xWindow=win
rep
    if(IsHungAppWindow(xWindow))
        ShutDownProcess xWindow
        break
    ..

If I am using dll user32 #IsHungAppWindow hWnd in main macro and also in a function, should I also add the same line in the Function? Or having it on the main macro is good enough? The function is separate from the main macro, its a trigger.
#7
Identical declarations in multiple places is not error, but better put it in a single place. Somewhere so that the function would be declared before it is called. For example, in function init2 (if init2 does not exist - create), which runs at startup.

The code should have a wait command, or it will consume much CPU time. Also should check whether the window still exists, or IsHungAppWindow will endlessly return 0 checking dead window handle.
#8
There are many intances I have ran into though, where the window is still there, but cannot close it, where I have to go to Task Manager to close it. So this function will only work if the window does not exist?
#9
IsHungAppWindow returns 1 if the window exists but is not responding >5 seconds.

ShutDownProcess2 kills the program like Task Manager but without any dialogs. the program can be hung or not. It can have windows or not. ShutDownProcess too, but does not accept program name, only window handle.
#10
Gintaras Wrote:ShutDownProcess2 kills the program like Task Manager but without any dialogs. the program can be hung or not. It can have windows or not. ShutDownProcess too, but does not accept program name, only window handle.


ShutDownProcess does not?
#11
In QM 2.1.8.5, ShutDownProcess is same as ShutDownProcess2.
#12
ShutdownProcess is available on 2.1.6, is this not the same as the one on 2.1.8?
#13
New QM versions add more features to some functions. In QM 2.1.8.5, with ShutDownProcess you can use either window handle or program name. Before QM 2.1.8.5, program name cannot be used. Version 2.1.8.6 will add more features, eg terminate all matching processes.


Forum Jump:


Users browsing this thread: 1 Guest(s)