AdBlock Detected

We provide high-quality source code for free. Please consider disabling your AdBlocker to support our work.

Buy me a Coffee

Saved Tutorials

No saved posts yet.

Press Enter to see all results

[Solved]How to kill any process using VB.NET

By pushpam abhishek
Listen to this article

[Solved] How to kill any process using VB.NET


Close running processes

.Net made it very easy for handling processes. It is much easier to close the applications using Process.Kill(). Processes can be iterated using the ‘for each’ loop. The process object taken from enumeration has enough methods and properties to deal with processes.

Get the list of processes

To get the list of instances running with the current application's name, we can use the Process.GetProcessesByName(“ProcessName”). This method returns a collection of processes with the name “ProcessName”. Thereafter accessing each process is easy with a ‘for each’ loop.

Kill other processes running under the same name

I had a requirement to ensure that, the current process is the only running process. The rest of the other instances of the processes have to be closed. Using the GetProcessesByName method we can get the list of current instances. While iterating through the collection, we can identify the current process by the ID property. We can get the current running process by Process.GetCurrentProcess(). Now it is very easy to kill other processes than the current one.

Source code 


Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        For Each RunningProcess In Process.GetProcessesByName("Notepad")
            RunningProcess.Kill()
        Next
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        For Each RunningProcess In Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName)
            If (Not RunningProcess.Id = Process.GetCurrentProcess().Id) Then
                RunningProcess.Kill()
            End If
        Next
    End Sub
End Class

Read Also:-

Share this post

pushpam abhishek

About pushpam abhishek

Pushpam Abhishek is a Software & web developer and designer who specializes in back-end as well as front-end development. If you'd like to connect with him, follow him on Twitter as @pushpambhshk

Comments