List all process in a ListView : Process « Development « VB.Net Tutorial

Home
VB.Net Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statements
5.Date Time
6.Class Module
7.Development
8.Collections
9.Generics
10.Attributes
11.Event
12.LINQ
13.Stream File
14.GUI
15.GUI Applications
16.Windows Presentation Foundation
17.2D Graphics
18.I18N Internationlization
19.Reflection
20.Regular Expressions
21.Security
22.Socket Network
23.Thread
24.Windows
25.XML
26.Database ADO.net
27.Design Patterns
VB.Net
VB.Net by API
VB.Net Tutorial » Development » Process 
7.24.11.List all process in a ListView
List all process in a ListView
Imports System.Diagnostics
Imports System.Windows.Forms

public class ProcessManager
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class
Public Class Form1
    Private Sub ListProcesses()
        Dim ps() As Process
        Try
            ps = Process.GetProcesses()
            lvProcesses.BeginUpdate()
            lvProcesses.Clear()
            lvProcesses.Columns.Add("Name"100, HorizontalAlignment.Left)
            lvProcesses.Columns.Add("ID"60, HorizontalAlignment.Left)
            lvProcesses.Columns.Add("Priority"60, HorizontalAlignment.Right)
            lvProcesses.Columns.Add("Memory"100, HorizontalAlignment.Right)
            Dim As Process
            For Each p In ps
                Dim lvi As ListViewItem = New ListViewItem()
                lvi.Text = p.ProcessName
                lvi.SubItems.Add(p.Id.ToString())
                lvi.SubItems.Add(p.BasePriority.ToString())
                lvi.SubItems.Add(p.WorkingSet64.ToString())
                lvProcesses.Items.Add(lvi)
            Next p
            lvProcesses.EndUpdate()
        Catch As Exception
            MessageBox.Show(e.Message)
        End Try
    End Sub


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgsHandles MyBase.Load
        ListProcesses()
    End Sub

    Private Sub btnKillProcess_Click(ByVal sender As System.Object, ByVal e As System.EventArgsHandles btnKillProcess.Click
        If MessageBox.Show(Me, "Stop Process" + lvProcesses.SelectedItems(0).Text, "Stop", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning= DialogResult.Cancel Then
            Return
        End If
        Dim pid As Integer = Int32.Parse(lvProcesses.SelectedItems(0).SubItems(1).Text)
        Dim As Process = Process.GetProcessById(pid)
        If p Is Nothing Then Return
        If Not p.CloseMainWindow() Then p.Kill()
        p.WaitForExit()
        p.Close()
        ListProcesses()
    End Sub

    Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgsHandles btnRefresh.Click
        ListProcesses()
    End Sub

End Class


<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form

    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        End If
        MyBase.Dispose(disposing)
    End Sub

    Private components As System.ComponentModel.IContainer

    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.btnRefresh = New System.Windows.Forms.Button
        Me.label1 = New System.Windows.Forms.Label
        Me.btnKillProcess = New System.Windows.Forms.Button
        Me.btnNewProcess = New System.Windows.Forms.Button
        Me.lvProcesses = New System.Windows.Forms.ListView
        Me.btnProcessProp = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'btnRefresh
        '
        Me.btnRefresh.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.btnRefresh.Location = New System.Drawing.Point(55711)
        Me.btnRefresh.Margin = New System.Windows.Forms.Padding(4)
        Me.btnRefresh.Name = "btnRefresh"
        Me.btnRefresh.Size = New System.Drawing.Size(11635)
        Me.btnRefresh.TabIndex = 11
        Me.btnRefresh.Text = "Refresh(&R)"
        Me.btnRefresh.UseVisualStyleBackColor = True
        '
        'label1
        '
        Me.label1.AutoSize = True
        Me.label1.Location = New System.Drawing.Point(1324)
        Me.label1.Margin = New System.Windows.Forms.Padding(4040)
        Me.label1.Name = "label1"
        Me.label1.Size = New System.Drawing.Size(11215)
        Me.label1.TabIndex = 10
        Me.label1.Text = "Processes"
        '
        'btnKillProcess
        '
        Me.btnKillProcess.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.btnKillProcess.Location = New System.Drawing.Point(509347)
        Me.btnKillProcess.Margin = New System.Windows.Forms.Padding(4)
        Me.btnKillProcess.Name = "btnKillProcess"
        Me.btnKillProcess.Size = New System.Drawing.Size(16429)
        Me.btnKillProcess.TabIndex = 9
        Me.btnKillProcess.Text = "Stop(&K)"
        Me.btnKillProcess.UseVisualStyleBackColor = True
        '
        'btnNewProcess
        '
        Me.btnNewProcess.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.btnNewProcess.Location = New System.Drawing.Point(362347)
        Me.btnNewProcess.Margin = New System.Windows.Forms.Padding(4)
        Me.btnNewProcess.Name = "btnNewProcess"
        Me.btnNewProcess.Size = New System.Drawing.Size(13929)
        Me.btnNewProcess.TabIndex = 8
        Me.btnNewProcess.Text = "New(&N)"
        Me.btnNewProcess.UseVisualStyleBackColor = True
        '
        'lvProcesses
        '
        Me.lvProcesses.Location = New System.Drawing.Point(1354)
        Me.lvProcesses.Margin = New System.Windows.Forms.Padding(4)
        Me.lvProcesses.MultiSelect = False
        Me.lvProcesses.Name = "lvProcesses"
        Me.lvProcesses.Size = New System.Drawing.Size(659285)
        Me.lvProcesses.TabIndex = 7
        Me.lvProcesses.UseCompatibleStateImageBehavior = False
        Me.lvProcesses.View = System.Windows.Forms.View.Details
        '
        'btnProcessProp
        '
        Me.btnProcessProp.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.btnProcessProp.Location = New System.Drawing.Point(32347)
        Me.btnProcessProp.Margin = New System.Windows.Forms.Padding(4)
        Me.btnProcessProp.Name = "btnProcessProp"
        Me.btnProcessProp.Size = New System.Drawing.Size(15329)
        Me.btnProcessProp.TabIndex = 6
        Me.btnProcessProp.Text = "Properties(&P)"
        Me.btnProcessProp.UseVisualStyleBackColor = True
        '
        'Form1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(8.0!, 15.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(679395)
        Me.Controls.Add(Me.btnRefresh)
        Me.Controls.Add(Me.label1)
        Me.Controls.Add(Me.btnKillProcess)
        Me.Controls.Add(Me.btnNewProcess)
        Me.Controls.Add(Me.lvProcesses)
        Me.Controls.Add(Me.btnProcessProp)
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub
    Private WithEvents btnRefresh As System.Windows.Forms.Button
    Private WithEvents label1 As System.Windows.Forms.Label
    Private WithEvents btnKillProcess As System.Windows.Forms.Button
    Private WithEvents btnNewProcess As System.Windows.Forms.Button
    Private WithEvents lvProcesses As System.Windows.Forms.ListView
    Private WithEvents btnProcessProp As System.Windows.Forms.Button

End Class
7.24.Process
7.24.1.Start process by setting file name
7.24.2.Get current Process
7.24.3.Display properties of current Process
7.24.4.Start main module in current process
7.24.5.Kill current process
7.24.6.Get thread count in current process
7.24.7.Get Process by ID
7.24.8.Print properties of Process
7.24.9.Open Default editor for text file
7.24.10.Start a processStart a process
7.24.11.List all process in a ListViewList all process in a ListView
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.