hitechoutlaw Posted March 23, 2003 Posted March 23, 2003 is there a way in VB.NET to kil processes if they exist in memory and then start them if they dont. i have some programs that i use alot and it is a pain in the *** to exit them when i dont need them and need the memory, i want a fast way to close them, like a program that does it for me. Thx.:) i'm new to vb.net, i programed in vb6 a little but i didnt learn much, only how to do make a simple dart game and some other little stuff. any help would be greatly appreciated. Quote
*Experts* jfackler Posted March 23, 2003 *Experts* Posted March 23, 2003 kill an app From inside the app you've written and running, Application.exit kills the app. Quote
*Experts* jfackler Posted March 23, 2003 *Experts* Posted March 23, 2003 GC then frees the memory automatically, no more memory leaks in .net. Quote
hitechoutlaw Posted March 23, 2003 Author Posted March 23, 2003 i dunno what u mean, will that kill another app or the one its run in? Quote
*Experts* jfackler Posted March 23, 2003 *Experts* Posted March 23, 2003 The app that the Application.exit method is called within. GC is Garbage Collector. You can actually call it up to free memory (I've read otherwise in this forum) by calling GC.GetTotalMemory(True) That call will force the garbage collector to perform a collection and returns the amount of heap space the program has allocated. Quote
hitechoutlaw Posted March 23, 2003 Author Posted March 23, 2003 that will come in handy later on, thx. but i wanted to know if there was a way to kill another app. Quote
*Experts* jfackler Posted March 23, 2003 *Experts* Posted March 23, 2003 To start a process from within your program; Process.Start(C:\someprogram.exe) should do the trick. Quote
*Experts* jfackler Posted March 23, 2003 *Experts* Posted March 23, 2003 Ok to kill another app; Process.GetCurrentProcess.Kill() or Process.GetCurrentProcessByID(id).Kill() Quote
hitechoutlaw Posted March 23, 2003 Author Posted March 23, 2003 what would be the syntax to kill an app that is running called "app.exe"? Quote
*Experts* jfackler Posted March 23, 2003 *Experts* Posted March 23, 2003 Want to get the process Id? as in; Process.GetCurrentProcessByID(Id).Kill() Try this piece of code; Module Module1 Sub Main() Dim ProcessList as Process() ProcessList = Process.GetProcesses() Dim Proc As Process For Each Proc In ProcessList Console.Writeline("Name {0} ID {1}", Proc.ProcessName, Proc.Id) Next Console.ReadLine() ' Delay to view output End Sub End Module Of course to call this in a program and assign the Proc.Id to a variable and then call the aforementioned Process.GetCurrentProcessByID(Idvariable).Kill() would then kill the app you desire to kill. Quote
*Experts* jfackler Posted March 23, 2003 *Experts* Posted March 23, 2003 If the app.exe is running and has focus, just call the Process.GetCurrentProcess.Kill() and it will die very nicely. Quote
*Experts* jfackler Posted March 23, 2003 *Experts* Posted March 23, 2003 But; you can't do that with a button control unless I suppose you give that app focus from the button click method before calling the process.getcurrentprocess.kill() If you call that event with a button without changing the focus it will just kill your app that contains the button. Quote
hitechoutlaw Posted March 23, 2003 Author Posted March 23, 2003 maybe i'm not getting it or something. can it kill the app by the file name that is being run? i want this program to kill the app and then exit the program i just run. can this be done? i'm new to vb.net and some ppl might call me stupid but in vb6 i learned better if i started a hard project and then i learned it alot better when i finaly got it to work and from there i learned alot more by experimenting with the code i have, then when i go back to the easy stuff its alot easier. this worked for me in vb6 but i havent been using that very long eighter so i dont know much in that. thanks for your help Quote
*Experts* jfackler Posted March 23, 2003 *Experts* Posted March 23, 2003 Ok, Say you start the app as we discussed; Process.Start(C:\..\app.exe) You can immediatly kill it (i.e. while it has focus) by calling Process.GetCurrentProcess.Kill() Quote
*Experts* jfackler Posted March 23, 2003 *Experts* Posted March 23, 2003 Since it has just been started, it will have the system focus and will therefore be the 'CurrentProcess' unless you move focus to another process. Quote
hitechoutlaw Posted March 23, 2003 Author Posted March 23, 2003 ok i understand the start app code and the kill app code. say i have a program running in the background before i start the app i just made. i want to kill that app that was running with the one i just made, can this be done? Quote
*Experts* jfackler Posted March 23, 2003 *Experts* Posted March 23, 2003 If your ok with oop, try instantiating an instance of your process by creating a class of your app.exe. Once this is done, you should be able to reference the instance and give it focus before killing it with the Process.GetCurrentProcess.Kill() Unfortunately, in answer to your question you cant just refer to a running app by name and kill it as far as I know. Any one else have thoughts? Quote
*Gurus* divil Posted March 23, 2003 *Gurus* Posted March 23, 2003 Goodness, what a lot of misunderstandings :) Here's the code you're looking for, in this example it looks through all processes until it finds notepad.exe, and when it does, it closes it. Dim processes(), p As Process processes = Process.GetProcesses() For Each p In processes If Not (p.MainModule Is Nothing) Then If System.IO.Path.GetFileName(p.MainModule.FileName).ToLower() = "notepad.exe" Then p.CloseMainWindow() Exit For End If End If Next Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
neo_thefox Posted May 12, 2004 Posted May 12, 2004 Here is my bit of code i think that this is a bit cleaner... i modify and take a bit of all your samples coz any has worked for me until now. and this can be on a botton control without problems. Dim pro As Integer Dim ProcessList As Process() ProcessList = Process.GetProcesses() Dim Proc As Process For Each Proc In ProcessList If Proc.ProcessName = "Name of Process to be closed" Then pro = Proc.Id Next Process.GetProcessById(pro).Kill() If you dont know how the process is called u can use this code to know how is the names of all the processes currently running on the machine. :cool: Dim ProcessList As Process() ProcessList = Process.GetProcesses() Dim Proc As Process For Each Proc In ProcessList msgBox(Proc.ProcessName & " " & Proc.Id) Next This will show you one by one all the process currently running with their Id on a Message Box, you only has to write down the name of the app that u want to close. :p Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.