How to Kill a Process Using Vb.Net or C#

How do I kill a process using Vb.NET or C#?

You'll want to use the System.Diagnostics.Process.Kill method. You can obtain the process you want using
System.Diagnostics.Proccess.GetProcessesByName.

Examples have already been posted here, but I found that the non-.exe version worked better, so something like:

foreach ( Process p in System.Diagnostics.Process.GetProcessesByName("winword") )
{
try
{
p.Kill();
p.WaitForExit(); // possibly with a timeout
}
catch ( Win32Exception winException )
{
// process was terminating or can't be terminated - deal with it
}
catch ( InvalidOperationException invalidException )
{
// process has already exited - might be able to let this one go
}
}

You probably don't have to deal with NotSupportedException, which suggests that the process is remote.

Killing of running process in window with VB.NET

You and simply kill the process by kill() method.

    Dim processList() As Process
processList = Process.GetProcessesByName(ListBox1.Items(ListBox1.SelectedIndex).ToString)

For Each proc As Process In processList
If MsgBox("Terminate " & proc.ProcessName & "?", MsgBoxStyle.YesNo, "Terminate?") = MsgBoxResult.Yes Then
Try
proc.Kill()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End If
Next

Need help forcefully terminating a process in vb.net

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

Identify a process started in VB.Net to be able to kill it and all its children later

Based on the C# code given as part of an answer to Find all child processes of my own .NET process, I used the following code that seem to work to kill all children of a process knowing its PID :

Sub killChildrenProcessesOf(ByVal parentProcessId As UInt32)
Dim searcher As New ManagementObjectSearcher(
"SELECT * " &
"FROM Win32_Process " &
"WHERE ParentProcessId=" & parentProcessId)

Dim Collection As ManagementObjectCollection
Collection = searcher.Get()

If (Collection.Count > 0) Then

consoleDisplay("Killing " & Collection.Count & " processes started by process with Id """ & parentProcessId & """.")
For Each item In Collection
Dim childProcessId As Int32
childProcessId = Convert.ToInt32(item("ProcessId"))
If Not (childProcessId = Process.GetCurrentProcess().Id) Then

killChildrenProcessesOf(childProcessId)

Dim childProcess As Process
childProcess = Process.GetProcessById(childProcessId)
consoleDisplay("Killing child process """ & childProcess.ProcessName & """ with Id """ & childProcessId & """.")
childProcess.Kill()
End If
Next
End If
End Sub

For information, it is needed to :

Imports System.Management

Then if you use Visual Studio, you'll need to go under the project menu, select "Add Reference...", under the ".Net" tab, scroll down to "System.Management" and select the line corresponding to "System.Management" and click OK. (as stated in this answer)

When correctly called, this Sub is satisfying to kill all children started by a parent process. If need, a basic parentProcess.kill kill the parent process.

VB.NET: How to terminate a process that is already running outside of the current project

I have done something similar to this before. If I am understanding your requirements correctly then you need to use the GetProcess in the System.Diagnostic namespace. There is more information about it here: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.kill.aspx.

killing a process by handle VB.net

The name of the Internet Explorer process is iexplore rather than iexplorer (notice the absence of r in the end of the first one I mentioned).

Thus your code should be:

Dim IEProcesses() As Process = System.Diagnostics.Process.GetProcessesByName("iexplore")

However, something that has already been pointed out to you (in a now deleted answer) is that you don't need to iterate the processes like that. All you need to do is call Kill() on the variable you already have:

IE.Kill()

Finally, if you want/need to ensure that the URL is opened by IE only (and not Chrome or whatever the user has as his/her default browser) you should start it specifically, passing the URL as an argument:

Dim IE As Process = Process.Start("iexplore.exe", "google.com")


Related Topics



Leave a reply



Submit