IT KNOWLEDGE

IT KNOWLEDGE
BC

Tuesday, May 20, 2014

How to Show and Kill Process in Listbox (VB.Net)

Being an end-user who use computer, although you are a programmer, you have never known all process running in you computer without using some tools. There are a lot of ways that allow you to know what process are running in your computer such as using Task Manager or others Script. In this tutorial, we developed a small application to show all process running in your computer and kill each process at each click using VB.Net. 


Application Design
  I use 3 controls as below:
No Control Type Name Text
1 Listbox ListboxProcess
2 Button1 ButtonShowProcess Show Process
3 Button2 ButtonKillProcess

Kill Process



Code

Show all Process
Private Sub ButtonShowProcess_Click(sender As Object, e As EventArgs) Handles ButtonShowProcess.Click
        ListBoxProcess.Items.Clear()
        Dim pro As System.Diagnostics.Process
        For Each pro In System.Diagnostics.Process.GetProcesses()
            ListBoxProcess.Items.Add(pro.ProcessName)
        Next
    End Sub

Kill Process
Private Sub ButtonKillProcess_Click(sender As Object, e As EventArgs) Handles ButtonKillProcess.Click
        killpro = ListBoxProcess.SelectedItem.ToString
        Try
            Dim pProcess() As Process = System.Diagnostics.Process.GetProcessesByName(killpro)
            For Each p As Process In pProcess
                DialogResult = MessageBox.Show("Are you sure you want to kill this process?" & Environment.NewLine & Environment.NewLine & "Process Name: " & ListBoxProcess.SelectedItem.ToString, "Process Manager", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation)
                If DialogResult = Windows.Forms.DialogResult.Yes Then
                    p.Kill()
                Else
                    MessageBox.Show("Process is aborted! to kill by user", "Process Manager", MessageBoxButtons.OK, MessageBoxIcon.Information)
                End If
            Next
        Catch ex As Exception

        End Try
    End Sub

Whole Code

Public Class FormShowKilledProccess
    Dim killpro As String
    Private Sub ButtonShowProcess_Click(sender As Object, e As EventArgs) Handles ButtonShowProcess.Click
        ListBoxProcess.Items.Clear()
        Dim pro As System.Diagnostics.Process
        For Each pro In System.Diagnostics.Process.GetProcesses()
            ListBoxProcess.Items.Add(pro.ProcessName)
        Next
    End Sub

    Private Sub ButtonKillProcess_Click(sender As Object, e As EventArgs) Handles ButtonKillProcess.Click
        killpro = ListBoxProcess.SelectedItem.ToString
        Try
            Dim pProcess() As Process = System.Diagnostics.Process.GetProcessesByName(killpro)
            For Each p As Process In pProcess
                DialogResult = MessageBox.Show("Are you sure you want to kill this process?" & Environment.NewLine & Environment.NewLine & "Process Name: " & ListBoxProcess.SelectedItem.ToString, "Process Manager", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation)
                If DialogResult = Windows.Forms.DialogResult.Yes Then
                    p.Kill()
                Else
                    MessageBox.Show("Process is aborted! to kill by user", "Process Manager", MessageBoxButtons.OK, MessageBoxIcon.Information)
                End If
            Next
        Catch ex As Exception

        End Try
    End Sub
End Class

























Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Powered By: itech-9999

0 comments: