Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

How to determine the operating system service pack level in Visual Basic .NET or in Visual Basic 2005


For a Microsoft Visual Basic 5.0 and Microsoft Visual Basic 6.0 version of this article, see 231844 .

↑ Back to the top


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.

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

  1. Start a new Visual Basic Windows Application in Visual Basic .NET or in Visual Basic 2005.
  2. In the Solution Explorer window, right-click References, and then click Add Reference.
  3. 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.
  4. Click OK to close the Add Reference dialog box.
  5. In the Solution Explorer window, right-click Form1.vb, and then click View Code.
  6. Delete all of the code from the Form1.vb code window.
  7. 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.
  8. 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.
  9. Press F5 to build and run the project.
  10. After Internet Explorer is running, click the buttons on the form to test the code.

↑ Back to the top


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#

↑ Back to the top


Keywords: kbvs2005swept, kbvs2005applies, kbvs2003swept, kbdsupport, kbhowtomaster, kb

↑ Back to the top

Article Info
Article ID : 304990
Revision : 3
Created on : 4/17/2018
Published on : 4/19/2018
Exists online : False
Views : 119