rerat
rerat

Reputation: 375

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.

Upvotes: 0

Views: 4809

Answers (2)

Cyclo
Cyclo

Reputation: 1

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

Shell("taskkill /F /IM process-name-here.exe /T", 0)

End Sub

Upvotes: 0

Mark Hall
Mark Hall

Reputation: 54532

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

Upvotes: 1

Related Questions