C# Process Kill Example

Kill. Sometimes we want to stop or "kill" a process. Either the process was previously started or was opened before our application started. To kill a process, the violently-named Kill method on the Process instance is appropriate.

Example. As we begin, please recall the Process.Start method, which returns a Process instance. This instance is an object that represents a system-level process. It has many helpful methods.

In this example, we start the Notepad program, and then wait one second. Finally, we call the Kill method on the process variable. When you run this program, Notepad will start up, and then it will close in one second.

Using Kill method on process type: C#

using System.Diagnostics;
using System.Threading;

class Program
    static void Main()
	// Start notepad.
	Process process = Process.Start("notepad.exe");
	// Wait one second.
	// End notepad.

Discussion. It is probably more common to use the Kill method to search for and stop applications that are not working. First loop through the running processes. Then, you can acquire a reference to the process and call Kill.

Warning: Don't terminate applications in a console program if you happen to be doing something useful in another program.

And: As an expert in this mistake, I can assure you this is not a good productivity booster.

Summary. The Kill method is an excellent way to cause a Process to meet a violent and swift end. The Kill method can throw some exceptions. But it often does not. It usually will accomplish your desired objective.