TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Process Kill Example

This C# article describes the Kill method on the Process type.

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.

Sleep

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.
	Thread.Sleep(1000);
	// End notepad.
	process.Kill();
    }
}

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.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf