Article ID: 304990
Article Last Modified on 5/11/2007
APPLIES TO
- Microsoft Visual Basic 2005
- Microsoft Visual Basic .NET 2003 Standard Edition
- Microsoft Visual Basic .NET 2002 Standard Edition
This article was previously published under Q304990
For a Microsoft Visual Basic 5.0 and Microsoft Visual Basic 6.0 version of this article, see 231844.
SUMMARY
In some situations, you may want to detect if an application is blocked. For example, when you are automating Microsoft Internet Explorer, you may want to know if Internet Explorer has stopped responding.
This article describes how to detect whether an automated instance of Internet Explorer has stopped responding (hung) and how to close Internet Explorer. Although the code is written for Internet Explorer and Visual Basic .NET or Visual Basic 2005, you can use this approach for other applications as well.
The first three sections in this article outline the three important coding concepts that are necessary to accomplish this task. The fourth section demonstrates how to build the sample application.
back to the top
Start the Application
The sample for this article uses Internet Explorer as the test application. The following code starts Internet Explorer and uses the GetProcessByName method to get a handle to the process.
Browser = New InternetExplorer() Browser.Visible = True Browser.GoHome() procs = Process.GetProcessesByName("IEXPLORE")
Determine If the Application Is Responding
The following code checks the first element in the array procs, which the GetProcessByName method returns, to determine if the process is responding. This article assumes that only one instance of Internet Explorer is running. You must use the try-catch block to handle the exception that is thrown if the process does not exist.
Try If procs(0).Responding = True Then MessageBox.Show("IEXPLORE is responding") Else MessageBox.Show("IEXPLORE is not responding") End If Catch MessageBox.Show("IEXPLORE is not running") End Try
Close the Application
The following code demonstrates how to close the application. If the application is still responsive, you can use the CloseMainWindows method of the Process class to close it. If the application is not responsive, you must call the Kill method.
Try If procs(0).Responding Then procs(0).CloseMainWindow() Else 'Force closure. procs(0).Kill() End If Catch notRunning As Exception When Err.Number = 91 MessageBox.Show("Could Not Find the IEXPLORE Process") End Try
Build the Sample Project
About the Sample
The sample project in this article consists of a form with the following three buttons:
- Start Internet Explorer, which uses Automation to start an instance of Internet Explorer.
- Check Internet Explorer, which tests to see if the browser is responding.
- Close Internet Explorer, which closes the browser.
If you want to give this code a thorough test and know of a Web page that will cause the browser to stop responding, you can browse to that page after you open the browser. Then, try to click Check Internet Explorer and Close Internet Explorer. Allow a few moments after you click the buttons; the response is not immediate when the browser is unresponsive. back to the top
Steps to Build the Sample
- Start a new Visual Basic Windows Application in Visual Basic .NET or in Visual Basic 2005.
- In the Solution Explorer window, right-click References, and then click Add Reference.
- On the COM tab in the Add Reference dialog box, click Microsoft Internet Controls, and then click Select.
Note In Visual Studio 2005, you do not have to click Select. - Click OK to close the Add Reference dialog box.
- In the Solution Explorer window, right-click Form1.vb, and then click View Code.
- Delete all of the code from the Form1.vb code window.
Paste the following code in the Form1.vb code window:
Option Explicit On Option Strict On Imports SHDocVw Public Class Form1 Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call. End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub Friend WithEvents btnStart As System.Windows.Forms.Button Friend WithEvents btnCheck As System.Windows.Forms.Button Friend WithEvents btnClose As System.Windows.Forms.Button 'Required by the Windows Form Designer Private components As System.ComponentModel.Container 'NOTE: The following procedure is required by the Windows Form Designer. 'You can use the Windows Form Designer to modify it; however, do not 'use the code editor to modify it. <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.btnClose = New System.Windows.Forms.Button() Me.btnStart = New System.Windows.Forms.Button() Me.btnCheck = New System.Windows.Forms.Button() Me.SuspendLayout() ' 'btnClose ' Me.btnClose.Location = New System.Drawing.Point(66, 176) Me.btnClose.Name = "btnClose" Me.btnClose.Size = New System.Drawing.Size(160, 23) Me.btnClose.TabIndex = 2 Me.btnClose.Text = "Close Internet Explorer" ' 'btnStart ' Me.btnStart.Location = New System.Drawing.Point(66, 72) Me.btnStart.Name = "btnStart" Me.btnStart.Size = New System.Drawing.Size(160, 23) Me.btnStart.TabIndex = 0 Me.btnStart.Text = "Start Internet Explorer" ' 'btnCheck ' Me.btnCheck.Location = New System.Drawing.Point(66, 124) Me.btnCheck.Name = "btnCheck" Me.btnCheck.Size = New System.Drawing.Size(160, 23) Me.btnCheck.TabIndex = 1 Me.btnCheck.Text = "Check Internet Explorer" ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 266) Me.Controls.AddRange(New System.Windows.Forms.Control() _ {Me.btnClose, Me.btnCheck, Me.btnStart}) Me.Name = "Form1" Me.Text = "Form1" Me.ResumeLayout(False) End Sub #End Region Private WithEvents Browser As InternetExplorer Private procs() As Process Private Sub btnStart_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnStart.Click Me.Cursor.Current = Cursors.WaitCursor Browser = New InternetExplorer() Browser.Visible = True Browser.GoHome() procs = Process.GetProcessesByName("IEXPLORE") End Sub Private Sub btnCheck_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnCheck.Click 'This code assumes that only one instance of Internet Explorer is running. Me.Cursor.Current = Cursors.WaitCursor Try If procs(0).Responding = True Then MessageBox.Show("IEXPLORE is responding") Else MessageBox.Show("IEXPLORE is not responding") End If Catch MessageBox.Show("IEXPLORE is not running") End Try End Sub Private Sub btnClose_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnClose.Click Me.Cursor.Current = Cursors.WaitCursor Try If procs(0).Responding Then procs(0).CloseMainWindow() Else procs(0).Kill() cleanUp() End If Catch notRunning As Exception When Err.Number = 91 MessageBox.Show("Could Not Find the IEXPLORE Process") Catch lockedUp As Exception When Err.Number = -2147023170 'Do nothing. End Try End Sub Private Sub Browser_OnQuit() Handles Browser.OnQuit 'Clean up if the browser is closed manually. cleanUp() End Sub Private Sub cleanUp() ReDim procs(0) Browser = Nothing End Sub End Class
Note You must change the code in Visual Basic 2005. By default, Visual Basic creates two files for the project when you create a Windows Forms project. If the form is named Form1, the two files that represent the form are named Form1.vb and Form1.Designer.vb. You write the code in the Form1.vb file. The Windows Forms Designer writes the code in the Form1.Designer.vb file. The Windows Forms Designer uses the partial keyword to divide the implementation of Form1 into two separate files. This behavior prevents the designer-generated code from being interspersed with your code.
For more information about the new Visual Basic 2005 language enhancements, visit the following Microsoft Developer Network (MSDN) Web site:For more information about partial classes and the Windows Forms Designer, visit the following MSDN Web site:
NOTE: The preceding code draws, positions, and names the three buttons on the form automatically, so you do not need to add them manually.
- After you paste the code in the Form1.vb code window, you may want to collapse the region that is labeled Windows Form Designer generated code.
- Press F5 to build and run the project.
- After Internet Explorer is running, click the buttons on the form to test the code.
REFERENCES
For additional information, click the article number below to view the article in the Microsoft Knowledge Base:
304991 Detect if an Application Has Stopped Responding by Using C#
For more general information about Visual Basic .NET, see the following Usenet newsgroup:
Additional query words: try...catch
Keywords: kbvs2005swept kbvs2005applies kbhowtomaster KB304990