Microsoft KB Archive/304990

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

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.





For a Microsoft Visual C# .NET version of this article, see 300991.

IN THIS TASK

 * SUMMARY
 * Start the Application
 * Determine If the Application Is Responding
 * Close the Application
 * About the Sample
 * Steps to Build the Sample
 * REFERENCES



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(&quot;IEXPLORE&quot;) back to the top

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(&quot;IEXPLORE is responding&quot;) Else MessageBox.Show(&quot;IEXPLORE is not responding&quot;) End If   Catch MessageBox.Show(&quot;IEXPLORE is not running&quot;) End Try back to the top

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(&quot;Could Not Find the IEXPLORE Process&quot;) End Try back to the top

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.</li> Delete all of the code from the Form1.vb code window.</li>  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


 * 1) Region &quot; Windows Form Designer generated code &quot;

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 = &quot;btnClose&quot; Me.btnClose.Size = New System.Drawing.Size(160, 23) Me.btnClose.TabIndex = 2 Me.btnClose.Text = &quot;Close Internet Explorer&quot; '       'btnStart '       Me.btnStart.Location = New System.Drawing.Point(66, 72) Me.btnStart.Name = &quot;btnStart&quot; Me.btnStart.Size = New System.Drawing.Size(160, 23) Me.btnStart.TabIndex = 0 Me.btnStart.Text = &quot;Start Internet Explorer&quot; '       'btnCheck '       Me.btnCheck.Location = New System.Drawing.Point(66, 124) Me.btnCheck.Name = &quot;btnCheck&quot; Me.btnCheck.Size = New System.Drawing.Size(160, 23) Me.btnCheck.TabIndex = 1 Me.btnCheck.Text = &quot;Check Internet Explorer&quot; '       '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 = &quot;Form1&quot; Me.Text = &quot;Form1&quot; Me.ResumeLayout(False)

End Sub


 * 1) 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(&quot;IEXPLORE&quot;) 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(&quot;IEXPLORE is responding&quot;) Else MessageBox.Show(&quot;IEXPLORE is not responding&quot;) End If       Catch MessageBox.Show(&quot;IEXPLORE is not running&quot;) 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(&quot;Could Not Find the IEXPLORE Process&quot;) 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:

http://msdn2.microsoft.com/en-us/library/ms379584(vs.80).aspx

For more information about partial classes and the Windows Forms Designer, visit the following MSDN Web site:

http://msdn2.microsoft.com/en-us/library/ms171843.aspx

NOTE: The preceding code draws, positions, and names the three buttons on the form automatically, so you do not need to add them manually. </li> 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.</li> Press F5 to build and run the project.</li> After Internet Explorer is running, click the buttons on the form to test the code.</li></ol>

back to the top

<div class="references_section">