GZ Visiting Expert Joined Apr 8, 2012 Posts 1,302 Location New Jersey May 29, 2012 #1 Okay... Yesterday, I spent a few hours putting together a program to act as a simple interface that launches a program and 3 shell functions. One of the buttons launches "explorer.exe" (Windows Shell GUI)... I want to add the second function of shutting the program down to that button. I am a complete novice. Code: Private Sub Wshell_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Wshell.Click Process.Start(Environ$("SystemRoot") & ":\explorer.exe") End Sub I am pretty sure I could kill the program by adding something like... Code: Shell("Taskkill -pid ShellUI.exe") Would I just add that line in the sub??? Please bear with me... As I said, I am a complete noob...
Okay... Yesterday, I spent a few hours putting together a program to act as a simple interface that launches a program and 3 shell functions. One of the buttons launches "explorer.exe" (Windows Shell GUI)... I want to add the second function of shutting the program down to that button. I am a complete novice. Code: Private Sub Wshell_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Wshell.Click Process.Start(Environ$("SystemRoot") & ":\explorer.exe") End Sub I am pretty sure I could kill the program by adding something like... Code: Shell("Taskkill -pid ShellUI.exe") Would I just add that line in the sub??? Please bear with me... As I said, I am a complete noob...
GZ Visiting Expert Joined Apr 8, 2012 Posts 1,302 Location New Jersey May 30, 2012 #2 Okay... That worked... But I am sure there is a better way of doing it... But for now... I am using Visual Studio 2010 VB... How can I access the bare code from the form? I want to study what I have done using the design tools. The only thing showing on Form1 are the private subroutines for the buttons.
Okay... That worked... But I am sure there is a better way of doing it... But for now... I am using Visual Studio 2010 VB... How can I access the bare code from the form? I want to study what I have done using the design tools. The only thing showing on Form1 are the private subroutines for the buttons.
niemiro Senior Administrator, Windows Update Expert Staff member Joined Mar 2, 2012 Posts 8,769 Location District 12 May 31, 2012 #3 Hello again :) I will come back to you on everything else I have yet to answer. I think that VB and C# provide access to the designer code in the same way. I will check this evening. If they do, click the small + next to Form1.vb, and double click on Form1.Designer.vb. Please note that you should never, ever, manually change code inside that file, as the designer could well just overwrite your changes, and it could even break the designer. Richard
Hello again :) I will come back to you on everything else I have yet to answer. I think that VB and C# provide access to the designer code in the same way. I will check this evening. If they do, click the small + next to Form1.vb, and double click on Form1.Designer.vb. Please note that you should never, ever, manually change code inside that file, as the designer could well just overwrite your changes, and it could even break the designer. Richard
GZ Visiting Expert Joined Apr 8, 2012 Posts 1,302 Location New Jersey May 31, 2012 #4 That is good to know!!! I will just have to find a standalone compiler to test my "changes" lol.
AceInfinity Emeritus, Contributor Joined Feb 21, 2012 Posts 1,728 Location Canada Jun 1, 2012 #5 Code: Shell("Taskkill -pid ShellUI.exe") This is actually bad habit, in this case you're almost limited to as much as a batch script. Use Process methods instead of Shell as well too (from the System.Diagnostics namespace). There's a few API's you could use as well... Get the handle to that process or just simply it's name, or mediocre way: get the PID when you start the process. Then use the Process.Kill method to kill the process.
Code: Shell("Taskkill -pid ShellUI.exe") This is actually bad habit, in this case you're almost limited to as much as a batch script. Use Process methods instead of Shell as well too (from the System.Diagnostics namespace). There's a few API's you could use as well... Get the handle to that process or just simply it's name, or mediocre way: get the PID when you start the process. Then use the Process.Kill method to kill the process.
GZ Visiting Expert Joined Apr 8, 2012 Posts 1,302 Location New Jersey Jun 2, 2012 #6 I am all too aware of the limitations... I can't rename the file or it fails to quit... Luckily, for this application that code works perfectly, but I am trying to learn, so I want to know every possible way that will accomplish this! (and any other function that I want to perform).
I am all too aware of the limitations... I can't rename the file or it fails to quit... Luckily, for this application that code works perfectly, but I am trying to learn, so I want to know every possible way that will accomplish this! (and any other function that I want to perform).
AceInfinity Emeritus, Contributor Joined Feb 21, 2012 Posts 1,728 Location Canada Jun 2, 2012 #7 This is our placeholder for the PID which is for our started process Code: Private ProcID As Integer To start the process: Code: Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Dim p As New Process With { _ .StartInfo = New ProcessStartInfo With { _ .FileName = "notepad.exe", _ .Verb = "runas" _ }, _ .EnableRaisingEvents = True _ } p.Start() ProcID = p.Id End Sub To kill that process: Code: Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click If Process.GetProcesses.Where(Function(p) p.Id = ProcID).Count > 0 Then Process.GetProcessById(ProcID).Kill() End Sub Last edited: Jun 2, 2012
This is our placeholder for the PID which is for our started process Code: Private ProcID As Integer To start the process: Code: Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Dim p As New Process With { _ .StartInfo = New ProcessStartInfo With { _ .FileName = "notepad.exe", _ .Verb = "runas" _ }, _ .EnableRaisingEvents = True _ } p.Start() ProcID = p.Id End Sub To kill that process: Code: Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click If Process.GetProcesses.Where(Function(p) p.Id = ProcID).Count > 0 Then Process.GetProcessById(ProcID).Kill() End Sub
GZ Visiting Expert Joined Apr 8, 2012 Posts 1,302 Location New Jersey Jun 3, 2012 #8 Okay... That is good to know, but I don't think it will work for this particular application. If I am reading that correctly, launching from button 1 will set the PID as a variable which will be used to kill the operation using button 2. In this case... The process is launched at winlogon so there would have to be another way to set the PID variable.
Okay... That is good to know, but I don't think it will work for this particular application. If I am reading that correctly, launching from button 1 will set the PID as a variable which will be used to kill the operation using button 2. In this case... The process is launched at winlogon so there would have to be another way to set the PID variable.
AceInfinity Emeritus, Contributor Joined Feb 21, 2012 Posts 1,728 Location Canada Jun 3, 2012 #9 If it's launched at WinLogon then you can check to see if the process exists then retrieve the PID. Or kill the process by it's name.
If it's launched at WinLogon then you can check to see if the process exists then retrieve the PID. Or kill the process by it's name.
GZ Visiting Expert Joined Apr 8, 2012 Posts 1,302 Location New Jersey Jun 3, 2012 #10 Is there a way to have the program automatically query the system processes, identify and set the PID for later termination? I could just use the process name (ShellUI.exe) to kill it as I am doing with Shell... It would be... Process.Kill("ShellUI.exe") No?
Is there a way to have the program automatically query the system processes, identify and set the PID for later termination? I could just use the process name (ShellUI.exe) to kill it as I am doing with Shell... It would be... Process.Kill("ShellUI.exe") No?
AceInfinity Emeritus, Contributor Joined Feb 21, 2012 Posts 1,728 Location Canada Jun 3, 2012 #11 gavinzach said: Is there a way to have the program automatically query the system processes, identify and set the PID for later termination? Click to expand... Depends on what you mean by "automatically". gavinzach said: I could just use the process name (ShellUI.exe) to kill it as I am doing with Shell... It would be... Process.Kill("ShellUI.exe") No? Click to expand... Nope... There could be multiple processes with the same name. And Kill() assumes one process. Unless you want to kill them all by that name? Code: Array.ForEach(Of Process)(Process.GetProcessesByName("ShellUI.exe"), Sub(x) x.Kill())
gavinzach said: Is there a way to have the program automatically query the system processes, identify and set the PID for later termination? Click to expand... Depends on what you mean by "automatically". gavinzach said: I could just use the process name (ShellUI.exe) to kill it as I am doing with Shell... It would be... Process.Kill("ShellUI.exe") No? Click to expand... Nope... There could be multiple processes with the same name. And Kill() assumes one process. Unless you want to kill them all by that name? Code: Array.ForEach(Of Process)(Process.GetProcessesByName("ShellUI.exe"), Sub(x) x.Kill())
GZ Visiting Expert Joined Apr 8, 2012 Posts 1,302 Location New Jersey Jun 3, 2012 #12 Automatically... Like when the program loads, get it's PID.
AceInfinity Emeritus, Contributor Joined Feb 21, 2012 Posts 1,728 Location Canada Jun 3, 2012 #13 Get the PID of what though? In the context of "it's"?
AceInfinity Emeritus, Contributor Joined Feb 21, 2012 Posts 1,728 Location Canada Jun 3, 2012 #14 Here's a way that I came up with to get all the PID's of a certain process name: Code: Dim PIDs As Integer() = Process.GetProcessesByName("notepad").Select(Function(p) p.Id).ToArray If PIDs.Count > 0 Then MessageBox.Show(String.Join(", ", PIDs)) When the program starts up, depending on whether this is a console app or a windows form app, you'll either have the main form constructor, the form_load event or the main() method as the entrypoint for the actual program code to start, in which you could place this code. It would then check as soon as you start the program. Last edited: Jun 3, 2012
Here's a way that I came up with to get all the PID's of a certain process name: Code: Dim PIDs As Integer() = Process.GetProcessesByName("notepad").Select(Function(p) p.Id).ToArray If PIDs.Count > 0 Then MessageBox.Show(String.Join(", ", PIDs)) When the program starts up, depending on whether this is a console app or a windows form app, you'll either have the main form constructor, the form_load event or the main() method as the entrypoint for the actual program code to start, in which you could place this code. It would then check as soon as you start the program.