Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

  • *Experts*
Posted

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.

  • *Experts*
Posted

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.

  • *Experts*
Posted

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.

Posted

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

  • *Experts*
Posted

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()

  • *Experts*
Posted
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.
Posted

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?

  • *Experts*
Posted

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?

  • *Gurus*
Posted

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

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

  • 1 year later...
Posted

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...