????? ??????? Visual Basic .NET ?? Visual Basic 2005 ????? ????? ???

?????? ????????? ?????? ?????????
???? ???????: 305602 - ??? ???????? ???? ????? ????? ??? ???????.
????? ???? | ?? ????

?? ??? ??????

??????

???? ??? ??????? ????? ????? ????? ?? ??? ?????. ??? ??? ????? ????? ????? ???? ???? ?? ????? ???? ??? "???????" ? ??? ??? ???? ?? ???? ??????? ??? ???????.

?????? ?????? ???????

???? ?????? ???????

???? ??????? ??? ??? ??????. ???? ??? ??????? ??? ????????? ?? ?????? ??????, ????? ??? ?? ??? ????? ???? ?? ????? ???? ??? ?? ??????. ????? proc ????? ??? ???? ??? ???? ????? ????? ? ???????? ????? ?? ???? ????? ?????? ??????? ???? ??? ??????? ?????? ??????? GetProcessByName:
Private proc As Process
Private processes() As Process
Private procName As String = "notepad"
				

?????? ??? ????? ?????? ???????

???? ????????? ???????? ??????? ???? (???? buildList ?? ???????) ???? ???? ?? ??? ??? ????? ???? ???? ?? ???????. ??? ??????? ?????? ??????? ????? ?? ???? ???? ListView; ????? ???? ???? ???? ListView ??? ????? ??? ?? ??? ??????? ?? ????? ????. ????? ?????? ????? ?? ??? ???????? ???????? ?? ??????? ??????? GetProcessByName ????? ???????. ??? ??????? ?????? ???? ?? ???????? ?????? ???? iterated ?? ???? ??????? ???????... ?? ??? ? ??? ???:
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
				

????? ???? ???? ?? ?????

??? ????? ?????? ?????? ?? ????? ?? ???? ????? ???? ???? "? ??? ?? ??????? ??? ??? ????????. ?????? ????? ?????? ????? ???? ???? ??????? ?????? ???????? ?? ????? ?????. ??? ????? ??????? ???? ???????? MainWindowTitle (????? ??? ???? ???????) ?? ???? ???? ListView. ???? ???????? ???????? ??? ?????? ???? ?????? ?????? ?? ???? ???? ListView ???? ??? ???? ??? ??????? ?????? ??????? ??????? GetProcessById ??? ?????? ????? ??? ??????? ?????? ??????? ??????? CloseMainWindow ??? ???:
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
				

?? ?????? ???? ?????? ?????

????? ???? ?????? ????? ???? ?????? ??????. ???? ???? ?????? ???? ?? ??????? ?????? ??????? GetProcessByName ??????? ??????? CloseMainWindow ??? ?? ???? ??????? ??? ???:
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
				

??????? ??????? ?????? ?? ?????

  1. ??? ????? ????? Windows Visual Basic ???? ?? Visual Studio .NET ?? ?? Visual Studio 2005.
  2. ???? ??? ?????? ?????? ??? ??????? ????????? Form1.vb ? ?? ??? ??? ????????? ????????.
  3. ??????? ????? ?? ???? ????????? ???????? ???????:
    Option Strict On
    Option Explicit On 
    
    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 btnLaunch1 As System.Windows.Forms.Button
      Friend WithEvents ColumnHeader1 As System.Windows.Forms.ColumnHeader
      Friend WithEvents ColumnHeader2 As System.Windows.Forms.ColumnHeader
      Friend WithEvents ListView1 As System.Windows.Forms.ListView
      Friend WithEvents btnCloseAll As System.Windows.Forms.Button
      Friend WithEvents btnClose1 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
      'It can be modified by using the Windows Form Designer.  
      'Do not modify it by using the code editor.
      <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.btnClose1 = New System.Windows.Forms.Button()
        Me.ListView1 = New System.Windows.Forms.ListView()
        Me.ColumnHeader1 = New System.Windows.Forms.ColumnHeader()
        Me.ColumnHeader2 = New System.Windows.Forms.ColumnHeader()
        Me.btnCloseAll = New System.Windows.Forms.Button()
        Me.btnLaunch1 = New System.Windows.Forms.Button()
        Me.SuspendLayout()
        '
        'btnClose1
        '
        Me.btnClose1.Location = New System.Drawing.Point(160, 176)
        Me.btnClose1.Name = "btnClose1"
        Me.btnClose1.Size = New System.Drawing.Size(112, 32)
        Me.btnClose1.TabIndex = 4
        Me.btnClose1.Text = "Close Selected Process"
        '
        'ListView1
        '
        Me.ListView1.Columns.AddRange(New System.Windows.Forms.ColumnHeader() {Me.ColumnHeader1, Me.ColumnHeader2})
        Me.ListView1.Location = New System.Drawing.Point(22, 8)
        Me.ListView1.MultiSelect = False
        Me.ListView1.Name = "ListView1"
        Me.ListView1.Size = New System.Drawing.Size(250, 152)
        Me.ListView1.TabIndex = 7
        Me.ListView1.View = System.Windows.Forms.View.Details
        '
        '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 Sub
    
    End Class
    					
    ?????? ??? ??? ????? ????????? ???????? ?? Visual Basic 2005. ???? ???????? ????? Visual Basic ????? ??????? ??? ????? ????? Windows ???????. ??? ?? ????? ??????? Form1, ??? ????? ??????? ???? ???? ??????? Form1.vb ? Form1.Designer.vb. ????? ????????? ???????? ?? ??? Form1.vb. ???? ????? Windows ?????? ???????? ???????? ?? ??? Form1.Designer.vb. ???? ????? Windows ?????? ?????? ???????? ??????? ?????? ????? Form1 ??? ????? ??????. ???? ??? ?????? ?? ????????? ???????? ???? ?? ??????? ???? ?? ??? interspersed ?? ????????? ???????? ?????? ??.

    ????? ?? ????????? ??? ??????? ????? 2005 Visual Basic ??????? ?? ?????? ???? ???? ????? Microsoft (MSDN) ?????? ??? ?????:
    http://msdn.microsoft.com/en-us/library/ms379584(VS.80).aspx
    ?????? ??? ???? ?? ????????? ??? ???? ????? "?" ????? Windows ???? ? ?? ?????? ???? MSDN ??????:
    http://msdn2.microsoft.com/en-us/library/ms171843.aspx
    ?????? ?? ???????? ?? ??????? ???? ???? ???? ??????? Windows ????? ????????? ????????.
  4. ?? ?????? ???????.
  5. ???? ??? ???? ??????? ??? ????? ?? ????.
  6. ???? ??? ???? ??????? ?? ???? ???? ???? ListView ???? ????? ?????. ??? ????? ?????? ?????? ??????? ???? ??????. ????? ????? ????? ????? ???? ???????? ?????? ???? ?????? ??????? ??? ???????.

?????????

?????? ??? ??????? ????? ???? ??? ?????? ??????? ??? ?????? ???????. ????? ??????? ???? ???? ??? ???? ?????? ???? ?????? ???? ?????? ?????. ???? ????? ???? ??????? WindowHandle ???? ????? ????? ??????? ??????? WindowHandle ???? ??????? ??????? ??? ?????? ???????.

???? ??????? ????? ???? ??? ????? ?? ?? ???? ??? well-suited ??? ??????. ??? ???? ??????? ?? ???? ??? ???? ????? ??????? ??????? ???? ?? ???? ???? ?????? ??????? ??????? ? ????? ??????? ??????? MainWindowTitle ???????? ?? ?????? ??? ???? ????????. ?? ?? ???? ??????? MainWindowTitle ???? ???? ???? ???? ?? ?????? ??? ??????? ???????.

??????? ?? ??? ??????? ?????? Windows ??????? ? ?????? ????? CloseMainWindow ????? ???????. ?? ???? CloseMainWindow ?? ????? ??? Windows. ??? ?? ??? ??????? ???? ????? ????? ???? (??? ???? ??????? ????? ???? ????) ? ??? ??????? ????? ????? ?????? ???????.

?????

????? ?? ?????????? ???? ?????? ?????? Microsoft Network (MSDN) ???? ???:
??? ???????
http://msdn.microsoft.com/en-us/library/system.diagnostics.process(VS.71).aspx

???????

???? ???????: 305602 - ????? ??? ??????: 10/???? ?????/1428 - ??????: 2.7
????? ???
  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
????? ??????: 
kbmt kbvs2005swept kbvs2005applies kbhowtomaster KB305602 KbMtar
????? ????
???: ??? ????? ??? ?????? ???????? ?????? ????? ???? ????? ?????????? ????? ?? ????????? ?????? ????. ???? ???? ?????????? ???? ?? ???????? ???????? ?????? ????????? ????? ????????? ???????? ????? ???????? ?????? ?? ?????? ??? ?? ???????? ???????? ?? ????? ??????? ?????? ??? ??????? ?????? ??. ?????? ?? ???? ??? ??????? ???????? ????? ?? ???? ????? ?????? ??? ????? ??? ????? ??????? ?? ????? ?? ?????? ??? ??? ??????? ??????? ?? ????? ????? ????? ????? ?????. ?? ????? ???? ?????????? ??????? ??? ????? ?? ??????? ?? ????? ?????? ?? ??? ????? ?? ????? ??????? ?? ???????? ?? ??? ???????. ???? ???? ?????????? ???????? ??? ????? ?????? ??????? ??????
???? ??? ????? ??????? ?????? ??????????305602

????? ???????

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com