|
-
May 29th, 2006, 09:14 PM
#1
Thread Starter
Hyperactive Member
[2005] Tricky Question
Ok, here is what i want to do, I want to be able to type in a text to stop any process, for example:
If i type in, notepad.exe then press the stop button it will stop that process or any other process name that is givin, i know the code is
process.start("taskkill", "/f /im <i dont no what to put here>")
how would i get the name of the process from the textbox into that string?
Im using VB 2005 Express
-
May 29th, 2006, 09:38 PM
#2
Re: [2005] Tricky Question
Taskkill is a Windows commandline utility. If you search Windows Help and Support from the Start menu it gives you the syntax and some examples, including one using Notepad. I'd not be inclined to go that route though. If you're using VB.NET then use VB.NET. Use Process.GetProcessesByName to get a list of all runing processes with the specified name, then Process.CloseMainWindow to try to end the process gracefully, then Process.Kill to force the process to exit if it didn't repond to CloseMainWindow for whatever reason. Reasons would be that the process didn't have a main window or it was waiting for input from the user, like a prompt to save any pending changes. For that reason you should wait for a short time to allow the process to exit naturally by its own action or that of the user.
VB Code:
For Each proc As Process In Process.GetProcessesByName("process name here")
proc.CloseMainWindow() 'ask the process to exit.
proc.WaitForExit(10000) 'wait up to 10 seconds.
If Not proc.HasExited Then
proc.Kill() 'force the process to exit.
End If
Next proc
-
May 30th, 2006, 12:40 AM
#3
Thread Starter
Hyperactive Member
Re: [2005] Tricky Question
Ok for this part ("process name here"), how do i have the user put in the process name via the interface. would i simply put txtinput.text, and have the user type in the process name that will be closed?
-
May 30th, 2006, 12:54 AM
#4
Re: [2005] Tricky Question
It's just a string so it's up to you how you get that string. The two most likely methods would be to have the user enter it in a TextBox or you could get the name of all running processes and display them in a ComboBox.
-
May 30th, 2006, 01:52 AM
#5
Thread Starter
Hyperactive Member
Re: [2005] Tricky Question
I know how to get the processes, i put them in a list box, but if i wanted the user to type in a textbox to stop that process or simply right click it, how would i tell it to cancel that one process
-
May 30th, 2006, 02:24 AM
#6
Re: [2005] Tricky Question
I'm not sure I'm following you. You're saying that you have a list of running processes in a ListBox and you want to know how to end one of them, right? If so then you'd just get the name string from the ListBox, either by casting an item as String or using the GetItemText method, and place it where I've put "process name here". One thing to note is that if you want to identify individual processes from a number with the same name then you'd have to have get the process ID in the first place too. You could quite easily create a list of process IDs and names and bind it to a ListBox using the name as the DisplayMember and the ID as the ValueMember. If you do that though, make sure you check that that PID is still active with the same name when you try to end it as there is no guarantee that things won't have changed in the interim.
-
Jun 2nd, 2006, 12:18 AM
#7
Junior Member
Re: [2005] Tricky Question
Here try this for the input of the process by text box. You will have to know of the name of the process though (ex.. "notepad.exe" not just "notepad").
For Each proc As Process In Process.GetProcessesByName(textbox.text)
proc.CloseMainWindow() 'ask the process to exit.
proc.WaitForExit(10000) 'wait up to 10 seconds.
If Not proc.HasExited Then
proc.Kill() 'force the process to exit.
End If
Next proc
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|