rerat rerat - 2 months ago 15
Vb.net Question

Need help forcefully terminating a process in vb.net

From vb.net, I need to terminate a process that often hangs. I normally use process.kill, which works on most processes, but not on a hung process. If I use taskkill /im process.exe it does not terminate either. But if I use taskkill /im process.exe /f, it terminates fine. So the questions is, what is the vb.net equivalent for taskkill /f?

Thank You.

Answer

You will need to create a ProcessStartInfo with your arguments and the file name, in this case "taskkill" then you can start the process and it will run the taskkill command. This is a Subroutine that you can call that will do it. you will need to put Imports System.Diagnostics at the top of your Class file.

Imports System.Diagnostics
Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        KillHungProcess("Notepad.exe") 'Put your process name here
    End Sub
    Public Sub KillHungProcess(processName As String)
        Dim psi As ProcessStartInfo = New ProcessStartInfo
        psi.Arguments = "/im " & processName & " /f"
        psi.FileName = "taskkill"
        Dim p As Process = New Process()
        p.StartInfo = psi
        p.Start()
    End Sub
End Class
Comments