Recipe 14.7. Terminating a Running Process


Problem

You need to stop a running process immediately.

Solution

Sample code folder: Chapter 14\ProcessTerminate

Use the Process object's Kill() method to stop the running process.

Discussion

This recipe's code creates a simple program that lets you stop any running application, similar to using the End Task button on the Windows Task Manager. Create a new Windows Forms application, and add to the form a ListBox control named ProcessList and a Button control named KillProcess. Change the Button control's Text property to Kill, and set the ListBox control's Sorted property to TRue. Now open the source code for the form, and replace the default empty class template with the following code:

 Public Class Form1    Private Sub Form1_Load(ByVal sender As Object, _          ByVal e As System.EventArgs) Handles Me.Load       ' ----- Display all top-level windows.       For Each oneProcess As Process In _               Process.GetProcesses( )          If (oneProcess.MainWindowTitle <> "") Then             ProcessList.Items.Add(New SmallProcess( _                oneProcess.MainWindowTitle, oneProcess.Id))          End If       Next oneProcess    End Sub    Private Sub KillProcess_Click( _          ByVal sender As System.Object, _          ByVal e As System.EventArgs) _          Handles KillProcess.Click       ' ----- Kill the selected process.       Dim oneProcess As Process       Dim selectedProcess As SmallProcess       On Error Resume Next       If (ProcessList.SelectedIndex = -1) Then Exit Sub       selectedProcess = CType(ProcessList.SelectedItem, _          SmallProcess)       ' ----- Confirm with the user.       If (MsgBox("Really kill '" & _          selectedProcess.ToString( ) & "'?", _          MsgBoxStyle.Question Or MsgBoxStyle.YesNo) <> _          MsgBoxResult.Yes) Then Exit Sub       ' ----- Locate and kill the process.       oneProcess = Process.GetProcessById(selectedProcess.ID)       oneProcess.Kill( )       ' ----- Remove the process from the list.       ProcessList.Items.Remove(ProcessList.SelectedItem)    End Sub End Class Public Class SmallProcess    ' ----- A small class that makes it easier to    '       track processes in the on-screen list.    Public WindowTitle As String    Public ID As Integer    Public Sub New(ByVal processTitle As String, _          ByVal processID As Integer)       WindowTitle = processTitle       ID = processID    End Sub    Public Overrides Function ToString( ) As String       Return WindowTitle    End Function End Class 

To kill a process, run this program, select a process from the list, and click the Kill button. Be careful: it will stop the indicated program.

By providing the Process.Kill() method, .NET endows your application with a lot of power. However, the system administrator may establish limits on the user running your program that will prevent access to or modification of process state.

This recipe's code includes a secondary class, SmallProcess, that helps keep track of items in the ListBox control. The Items collection of a ListBox control can hold any type of object, but how to display its own text is up to the object. You can store an entire Process object in the list, but the output from Process.ToString() is not as user-friendly. By storing just the parts you need in a separate class instance that includes its own ToString() method, you can get the results you need, both in terms of display and of access to the process IDs.




Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net