# VBForums CodeBank > CodeBank - C# >  Closing an External Process

## jmcilhinney

VB version here.

You can use the .NET Process class to close an external application.  First you must create a Process object that represents the application.  You can then call Kill on that Process to force the application to close.  That's the only way to close applications that don't have a GUI but, for those that do, it's better to call CloseMainWindow first and only call Kill as a last resort.  It's like the difference between just throwing someone out and asking them to leave first.  CloseMainWindow gives the application a chance to clean up before exiting, while Kill just removes it from memory.  E.g.
csharp Code:
private void CloseProcessesByName(string processName){    foreach (Process p in Process.GetProcessesByName(processName))    {        // Ask nicely for the process to close.        p.CloseMainWindow();         // Wait up to 10 seconds for the process to close.        p.WaitForExit(10000);         if (! p.HasExited)        {            // The process did not close itself so force it to close.            p.Kill();        }         // Dispose the Process object, which is different to closing the running process.        p.Close();    }}
The process name is what gets displayed in Windows Task Manager.

NOTE: Code converted using Instant C# by Tangible Software Solutions.

----------


## Jmacp

That's nice clean and simple compared to say the lines and lines you used to need for VB6.

----------


## BenJones

Cool is it ok if i can use this cos i am going to make a process viewer.
thanks for shareing.

----------


## a_almisery

Ok , Thanks To share ... 
.. 
I'm try to *kill* some program .. but that's no work !! 
What's problem ?

----------


## jmcilhinney

> Ok , Thanks To share ... 
> .. 
> I'm try to *kill* some program .. but that's no work !! 
> What's problem ?


How could anyone answer that question based on the information you have provided?

----------


## GRPsuper9

> How could anyone answer that question based on the information you have provided?


I'm pretty sure he's following your posts around and spamming them. He's the same one that commented on my thread that you just replied to.

----------

