You are currently offline, waiting for your internet to reconnect

How To Detect If an Application Has Stopped Responding

This article was previously published under Q231844
For a Microsoft Microsoft Visual Basic .NET version of this article, see 304990.
SUMMARY
In many situations you may want to know if an application is blocked. For example when you automate Microsoft Internet Explorer, you want to know whether Internet Explorer stops responding.

This article describes how to detect whether an automated instance of Internet Explorer stops responding. Although the code is written for Internet Explorer and Visual Basic, you can also use the following instructions for other applications.

The code uses the Win32 API SendMessageTimeout function to determine whether the target application responds and then the API call TerminateProcess to kill the hung instance of Internet Explorer.
MORE INFORMATION
There is no clear definition of an application hanging. Typically the application is "busy" with some processing. However from a user's perspective, the application has stopped responding.

The idea is to periodically detect if the application is still responding in a timer and depending on application logic, the target application can be killed or other necessary action can be taken.
The following steps demonstrate this. The code assumes that Internet Explorer is installed on the target machine.
  1. Start a new Standard EXE project in Visual Basic.
  2. Form1 is created by default. Place three command buttons on the form and name them cmdCheck, cmdLaunch, and cmdKill. Change their Caption property to Check, Launch, and Kill, respectively.
  3. Add the following code to Form1:
    Option Explicit'API ConstantsConst SMTO_BLOCK = &H1Const SMTO_ABORTIFHUNG = &H2Const WM_NULL = &H0Const WM_CLOSE = &H10Const PROCESS_ALL_ACCESS = &H1F0FFF'API functionsPrivate Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, _lpdwProcessId As Long) As LongPrivate Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, _ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As LongPrivate Declare Function SendMessageTimeout Lib "user32" Alias "SendMessageTimeoutA" _(ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As _Long, ByVal fuFlags As Long, ByVal uTimeout As Long, lpdwResult As Long) As LongPrivate Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, _ByVal uExitCode As Long) As LongPrivate modObjIE As ObjectPrivate modlngWndIE As LongPrivate Sub cmdCheck_Click()    Dim lngResult As Long    Dim lngReturnValue As Long    lngReturnValue = SendMessageTimeout(modlngWndIE, WM_NULL, 0&,       0&, SMTO_ABORTIFHUNG And SMTO_BLOCK, 1000, lngResult)    If lngReturnValue Then        MsgBox "Responding"    Else        MsgBox "Not Responding","Block tester"    End IfEnd SubPrivate Sub cmdLaunch_Click()    Set modObjIE = Nothing    Set modObjIE = CreateObject("InternetExplorer.Application")    modObjIE.Visible = True    modObjIE.Navigate2 "http://www.microsoft.com"    modlngWndIE = modObjIE.hwndEnd SubPrivate Sub cmdKill_Click()    Dim lngProcessID As Long    Dim lngReturnValue As Long    Dim lngProcess As Long    lngReturnValue = GetWindowThreadProcessId(modlngWndIE, lngProcessID)    lngProcess = OpenProcess(PROCESS_ALL_ACCESS, 0&, lngProcessID)    lngReturnValue = TerminateProcess(lngProcess, 0&)End Sub					
  4. Run this program and click the Launch button.
  5. Click the Check button. You should see Responding appear.
Enter your own URL in the navigate2 command if you have one that causes blocking.

NOTE: None of the automation properties of Internet Explorer will be accessible once Internet Explorer stops responding, which is the reason for storing hwnd in a form level variable.
REFERENCES
For additional information, click the article numbers below to view the articles in the Microsoft Knowledge Base:
129797 How To Launch a Win32 Application from Visual Basic
178893 How To Terminate an Application Cleanly in Win32
176391 How To Programmatically Close a Single Instance of a Windows-Based Program
SendMessageTimeout GetWindowThreadProcessId TerminateProcess hangs hung kill terminate IE "Internet Explorer" freezes blocked blocks "stops responding" "not
Properties

Article ID: 231844 - Last Review: 07/01/2004 15:28:48 - Revision: 3.2

  • Microsoft Visual Basic 5.0 Learning Edition
  • Microsoft Visual Basic 6.0 Learning Edition
  • Microsoft Visual Basic 5.0 Professional Edition
  • Microsoft Visual Basic 6.0 Professional Edition
  • Microsoft Visual Basic 5.0 Enterprise Edition
  • Microsoft Visual Basic Enterprise Edition for Windows 6.0
  • Microsoft Internet Explorer 4.01 Service Pack 2
  • Microsoft Internet Explorer 4.01 Service Pack 1
  • Microsoft Internet Explorer 4.01 Service Pack 2
  • Microsoft Internet Explorer 5.0
  • Microsoft Internet Explorer 5.5
  • kbhowto KB231844
Feedback