Introduction
Many Windows folks are aware of the Windows Task Manager, probably because of its popularity among Windows users. Moreover, developers have probably used it in some way or another to stop or end a program that appears to be in the “Process” tab. If you haven’t heard of or tried it, you can Google search it, or just press CTRL + SHIFT + ESC from your keyboard and it will appear.
Going back to our main goal in this article, we are going to explore the Windows process by answering the question of what it is, its role and how to interact with it via C# code samples.
What is a Windows Process?
Even before the .NET Framework came out, the concept of a “process” has existed since the early days of Windows OS. We can define a process as a running program that is being processed by the computer processor in an isolated manner. In addition, a process could be a background application that we don’t normally see running on our computer. A good example could be a printer driver program that runs in the background and watches the ink level attached to our computer.
The Role of Windows Process
For instance, when a developer runs an executable(*.exe) program once it is loaded into memory, the operating system creates a separate and isolated process for its use during its lifetime. Then, several processes may be associated with the same program; for instance, opening 2 instances of notepad results in more than one process being executed. This approach is called, “application isolation” which results in a stable runtime environment and considering that the failure of one process does not affect the function of the other running processes.
Things to Remember When Dealing with Windows Processes
- Every Windows process has a unique process identifier (PID)
- It can be loaded and unloaded by the operating system as well as programmatically.
Getting started interacting with Windows Processes using C#
Enumerate Windows Processes
- [TestMethod]
- public void Test_Enumerating_Running_Processes()
- {
- string localMachineName = Environment.MachineName;
-
- var runningProcess = Process.GetProcesses(localMachineName);
-
- foreach (var process in runningProcess)
- {
- string processName = process.ProcessName;
- string processId = process.Id.ToString();
-
- Console.WriteLine($"Process Id: {processId} Process Name: {processName}");
-
- Assert.IsNotNull(processName);
- }
- }