Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Recommend a better way to detect state of app? 1

Status
Not open for further replies.

benlinkknilneb

Programmer
May 16, 2002
590
US
(This is cross-posted in the Win API forum)

Hi all,

I've been working on a program to reboot my computer when an application crashes. I thought I had it figured out; I was using some code I found to detect whether the program was hung or not. However, I'm getting the "This program has performed an illegal operation..." message in the app that I'm checking on. Apparently, this isn't sufficient for the current code to say that the program is "hung", and it won't reboot until someone clicks the button to cancel the window. Below is the current code that I'm using to detect the state of the app:
Code:
Private Sub CheckStatus(hWnd As Long)
    Dim lPid As Long
    Dim lProcess As Long
    
    If WindowIsHung(hWnd) Then
        lProcess = GetWindowThreadProcessId(hWnd, lPid)
        lProcess = OpenProcess(PROCESS_ALL_ACCESS, 0&, lPid)
        TerminateProcess lProcess, 0&
        ShutDownWindows True, True
        End
    End If
End Sub

Private Function WindowIsHung(hWnd As Long) As Boolean
    Dim lResult As Long
    WindowIsHung = (SendMessageTimeout(hWnd, WM_NULL, 0&, 0&, SMTO_ABORTIFHUNG Or SMTO_BLOCK, 1000, lResult) = False)
End Function
Can anyone suggest a better way to do this, that will ignore the "illegal operation" message and reboot?

Ben
 
Hmm maybe use WMI?
Loop the whole thing with a 10 sec delay and put in a reboot section if the app is not running.

//Peter


strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcesses = objWMIService.ExecQuery _
("SELECT * FROM Win32_Process WHERE Name = 'Database.exe'")
If colProcesses.Count = 0 Then
Wscript.Echo "yourapp.exe is not running."
Else
Wscript.Echo "Yourapp.exe is running."
End If
 
Hi, Peter...

That might do the trick... but I was under the impression that WMI did not work with Win98. Is that the case?

Ben
 
Ah, thats true. Sorry for that..;) I live in the w2k and XP world. I dont have a solution for win98 systems though...


upgrade...? ;)
 
Yeah, I wish, Peter... but in most regards, this cpu is just fine for what it has to do... basically, it just collects data and stores it on the network. Nobody ever really uses it for anything... so they're in no hurry to upgrade it.
 
A completely crashed program is not the same as a hung window. The code given above is specifically for detecting hung windows.

One approach you might like to consider is determine the ProcessID of your application, and then just check to see if that process still exists on regular basis.
 
So the process is already terminated when the "Illegal operation" box appears?
 
IMHO, you should concentrate to figure out the cause of the application crash and rectify it rather than writing a program to reboot the machine when the application halts.

Obviously, this is not a good sign that the windows crashes so frequently so that it requires a restart now and then as a solution in normal routine.

A clean re-installation of Windows may fix the problem...
 
I have that script too... but only with WMI, If youre interested, post a reply.

/Peter
 
I agree, Hypetia... but ours is a small plant, IT dept is only 2 people... and I'm not even one of them. I've been developing as an intern in the QA department. If I could just reinstall Windows, I would... and IT has this on their list of things to fix... but in the interim, we still need the data that this machine collects, so I need a good temporary solution.
 
However... I would like to note that Windows is not crashing... just one program is. The computer is connected via DDE to a couple of automated measuring devices; data is collected every 2 minutes. My problem is that the program used to control the DDE connections keeps going south.
 
Hey all... take a look at this code and tell me if you think it looks ok.
Code:
Dim PID As Long
GetWindowThreadProcessId FindWindow(vbNullString, "RSLinx OEM"), PID
If Not PID Then
    ShutDownWindows True, True
    End
End If
I'm most concerned with the second line, and the condition in the if statement... I know that the "ShutDownWindows" function works. Is that how to properly get/use the process id, and will that still reboot automatically if the machine is waiting on the "OK" click from the Illegal Op box?
 
The condition you are using with if is incorrect. It will always be true.
The condition
[tt]If Not PID Then[/tt]
should be changed to
[tt]If PID = 0 Then[/tt]
 
In general :
If Not PID Then

and
If PID = 0 Then

are equivalent

PID was dimmed as long. A Boolean test on a Long will recognise 0 as False and any other value as True


________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
Well, I don't think so.
Numerically [tt]Not PID[/tt] is equivalent to [tt]-(PID + 1)[/tt].

You may verify the results.
[tt]
Dim PID As Long
PID = 12345 'or any other value
If Not PID Then MsgBox Not PID
If PID = 0 Then MsgBox PID = 0
[/tt]
In the above code, [tt]Not PID[/tt] is not evaluated as False (boolean), rather, it is evaluated as [tt]-12346[/tt].
The two conditions only return same result if PID is 0 (False) or -1 (True), otherwise the result is always different.
 
Thanks Hypetia,
My bad! My usual use specifically evaluates the Boolean first, unlike this case (as you correctly point out)



________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top