ColumnHeader1 ' Me.ColumnHeader1.Text = "Window Title" Me.ColumnHeader1.Width = 160 ' 'ColumnHeader2 ' Me.ColumnHeader2.Text = "Process ID" Me.ColumnHeader2.Width = 85 ' 'btnCloseAll ' Me.btnCloseAll.Location = New System.Drawing.Point(160, 216) Me.btnCloseAll.Name = "btnCloseAll" Me.btnCloseAll.Size = New System.Drawing.Size(112, 32) Me.btnCloseAll.TabIndex = 3 Me.btnCloseAll.Text = "Close All Processes" ' 'btnLaunch1 ' Me.btnLaunch1.Location = New System.Drawing.Point(32, 176) Me.btnLaunch1.Name = "btnLaunch1" Me.btnLaunch1.Size = New System.Drawing.Size(112, 72) Me.btnLaunch1.TabIndex = 1 Me.btnLaunch1.Text = "Start Notepad" ' '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.ListView1, Me.btnClose1, Me.btnCloseAll, Me.btnLaunch1}) Me.Name = "Form1" Me.Text = "Process Example" Me.ResumeLayout(False) End Sub#End Region Private procName As String = "notepad" Private proc As Process Private processes() As Process Private specialFolder As String = System.Environment.GetFolderPath(Environment.SpecialFolder.System) Private Sub buildList() 'this sub populates the listview control with the instances of 'procName' 'that are currently running. Dim itemAdd As ListViewItem ListView1.Items.Clear() processes = Process.GetProcessesByName(procName) For Each proc In processes itemAdd = ListView1.Items.Add(proc.MainWindowTitle) itemAdd.SubItems.Add(proc.Id.ToString) Next End Sub Private Sub btnLaunch1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLaunch1.Click 'Launch notepad and open the end user license agreement. Dim pInfo As New ProcessStartInfo() pInfo.FileName = specialFolder & "\eula.txt" pInfo.WindowStyle = ProcessWindowStyle.Minimized Dim p As Process = Process.Start(pInfo) p.WaitForInputIdle() buildList() End Sub Private Sub btnClose1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose1.Click 'Close one of the instances of notepad based on the selected item in the ListView. 'There are several ways to identify a process. For example, 'you can use the mainwindowtitle, windowhandle, or processname, depending 'on your needs. The process ID is used here because there may be several 'windows open with the same title. Try Dim procID As Integer = System.Convert.ToInt32(ListView1.SelectedItems(0).SubItems(1).Text) Dim tempProc As Process = Process.GetProcessById(procID) tempProc.CloseMainWindow() tempProc.WaitForExit() buildList() Catch MessageBox.Show("Please select a process in the ListView before clicking this button." + _ " Or the Process may have been closed by somebody.") buildList() End Try End Sub Private Sub btnCloseAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCloseAll.Click 'Walk the process array and close all processes. Try For Each proc In processes proc.CloseMainWindow() proc.WaitForExit() Next buildList() Catch ex As System.NullReferenceException MessageBox.Show("No instances of Notepad running.") End Try End Sub Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing 'Make sure that you do not leave any instances running. If Not (processes Is Nothing) Then If (processes.Length <> 0) Then Me.btnCloseAll_Click(Me, e) End If End If End SubEnd Class หมายเหตุ: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:
หมายเหตุ:It is recommended that you collapse the region that is labeledWindows Form Designer generated code.
  • เรียกใช้โปรแกรมประยุกต์
  • คลิกStart Notepadone or more times.
  • Click an instance of Notepad in theListViewcontrol window and selectClose Process. This closes the specific instance of Notepad that you selected. You can also selectClose All Processesto close all of the running instances of Notepad.
  • หมายเหตุ

    ตัวอย่างนี้ใช้นั้นIdคุณสมบัตินี้กระบวนการclass to differentiate between instances of the application. กระบวนการIdproperty is a good candidate for this task because all process IDs are unique. The same is true of theWindowHandleproperty, so you can also use theWindowHandleคุณสมบัติของคำกระบวนการobject to differentiate between instances of the application.

    Other properties can also be used, although they are less well-suited to the task. For example, if you do not know the process ID of a specific process or have the handle of the main window, you can use theMainWindowTitleproperty to help identify the proper instance. กระบวนการMainWindowTitleproperty may not be unique, but it can help you to isolate the desired application.

    The sample in this article uses a Windows application and employs theCloseMainWindowmethod to close the application.CloseMainWindowdoes not work with a non-Windows application. If the application that you are attempting to close does not have a window (for example, a console application), you must use theทำลายmethod to close the application.

    back to the top


    For more information, see the following Microsoft Developer Network (MSDN) Web site:
    Process Class
    .aspx (VS.71)
    back to the top

    คำเตือน: บทความนี้ได้รับการแปลโดยอัตโนมัติ


    รหัสบทความ: 305602 - การตรวจสอบครั้งสุดท้าย: 01/10/2011 08:26:00 - ฉบับแก้ไข: 2.0

    • Microsoft Visual Basic 2005
    • Microsoft Visual Basic .NET 2003 Standard Edition
    • Microsoft Visual Basic .NET 2002 Standard Edition
    • kbvs2005swept kbvs2005applies kbhowtomaster kbmt KB305602 KbMtth