Killing processes with listview (.net 2003)
-
I have created a program that lists all the processes running on the computer by using getprocesses then i have used a loop to list them all For each process Next Then i have made it so that each process is listed in a listview object. The problem i am having though is that i need to be able to use the process.kill method to exit the process that has been selected. How would i do this? If it is possible can you provide an example? Could i use the process.mainmodule.filename_(This just provides the filename/command line for the mainmodule of the program which is the process itself_) then give the filename(From the process.mainmodule.filename) to process.startinfo.filename. Then use the process.kill method, to exit the process? Any help would be much appreciated Thankyou! A*****
-
I have created a program that lists all the processes running on the computer by using getprocesses then i have used a loop to list them all For each process Next Then i have made it so that each process is listed in a listview object. The problem i am having though is that i need to be able to use the process.kill method to exit the process that has been selected. How would i do this? If it is possible can you provide an example? Could i use the process.mainmodule.filename_(This just provides the filename/command line for the mainmodule of the program which is the process itself_) then give the filename(From the process.mainmodule.filename) to process.startinfo.filename. Then use the process.kill method, to exit the process? Any help would be much appreciated Thankyou! A*****
you can use this API function: Private Declare Function TerminateProcess Lib "kernel32" Alias "TerminateProcess" (ByVal hProcess As Integer, ByVal uExitCode As Integer) As Integer for an example of what you want you can see this link: Terminate a process immediately in VB.NET[^]
-
I have created a program that lists all the processes running on the computer by using getprocesses then i have used a loop to list them all For each process Next Then i have made it so that each process is listed in a listview object. The problem i am having though is that i need to be able to use the process.kill method to exit the process that has been selected. How would i do this? If it is possible can you provide an example? Could i use the process.mainmodule.filename_(This just provides the filename/command line for the mainmodule of the program which is the process itself_) then give the filename(From the process.mainmodule.filename) to process.startinfo.filename. Then use the process.kill method, to exit the process? Any help would be much appreciated Thankyou! A*****
This should help. I'm using VS2005 so I hope this will be the same for you.
Private Sub Button1\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 'Get the processes with the name 'qryspree' 'You probably stored the process name in the listview so whatever item is selected 'will tell you the process name to get Dim proc() As System.Diagnostics.Process = System.Diagnostics.Process.GetProcessesByName("qryspree") 'Loop through processes found 'Note: Finding a process by name returns an array so the loop just 'ensures you kill each one with that name For Each instance As System.Diagnostics.Process In proc instance.Kill() Next End Sub
As another option you could store the process id's when you add them to the listview. The process object will tell you the ID. You could store the name with the id in a hashtable.
-
This should help. I'm using VS2005 so I hope this will be the same for you.
Private Sub Button1\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 'Get the processes with the name 'qryspree' 'You probably stored the process name in the listview so whatever item is selected 'will tell you the process name to get Dim proc() As System.Diagnostics.Process = System.Diagnostics.Process.GetProcessesByName("qryspree") 'Loop through processes found 'Note: Finding a process by name returns an array so the loop just 'ensures you kill each one with that name For Each instance As System.Diagnostics.Process In proc instance.Kill() Next End Sub
As another option you could store the process id's when you add them to the listview. The process object will tell you the ID. You could store the name with the id in a hashtable.
I had another idea. Why not store the ID as a subitem in the listview. Here is an example.
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ListView1.View = View.Details
ListView1.Columns.Add("Process", 150)
ListView1.Columns.Add("ID", 75)
'---Popullate listview1 with running processes
For Each proc As System.Diagnostics.Process In System.Diagnostics.Process.GetProcesses
'Add ProcessName with a subitem as the ID
ListView1.Items.Add(proc.ProcessName).SubItems.Add(proc.Id)
Next
End SubPrivate Sub Button1\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click For Each item As ListViewItem In ListView1.SelectedItems MsgBox(item.SubItems(1).text) Next End Sub
End Class