Running Processes in Windows, we display using the C# program. I am going to explain step-by-step, developing and running the C# program to display currently running processes in Windows.
I have tested this Program in Windows 7 Operating System. In Windows 64-bit Operating System, we have 32-bit & 64-bit processes. In Task Manager, you can see “32” with the processes, which are for 32-bit Operating systems. We can run 32-bit programs on 64-bit Operating Systems, but the other way is not possible; that means, we can’t run 64-bit programs on 32-bit Operating Systems.
Step 1. System.Diagnostics.Process
is the Class we use to get the processes that are currently running in the System. This class provides the GetProcesses
method, to get all the processes that are running.
Step 2. Once we get the processes, after calling the GetProcesses
method; go through each process in the processes list and display each process detail.
Step 3. Lets’ put it all together into a Program and the program looks like below:
Step 4. Build & Run the program. If no errors, the program displays the list of processes.
That’s it.? Yes, we have successfully completed the listing of processes currently running in the System. Lets’ extend our program to display more details about each process.
If we see, Task Manager; it displays “Image Name, User Name, CPU Utilization, Memory Usage & Description of the Process”. We also extend our program to display some of these details.
Step 5. GetProcesses
method returns an array of Process
objects. The Process
class contains the below attributes, and we use the same here.
- Process.Id => Process ID
- Process.ProcessName => Process Name
- Process.PagedSystemMemorySize64 => Memory Usage (in bytes)
- Process.MainModule.FileName => Filename of the process (Image Name)
Step 6. Lets’ use these attributes in our program. After the changes, build & run the program. Have you seen a list of processes.? If not, maybe our program displays below Error:
Unhandled Exception: System.ComponentModel.Win32Exception: A 32 bit processes cannot access modules of a 64 bit process.
at System.Diagnostics.NtProcessManager.GetModuleInfos(Int32 processId, Boolean firstModuleOnly)
at System.Diagnostics.NtProcessManager.GetFirstModuleInfo(Int32 processId)
at System.Diagnostics.Process.get_MainModule()
at ProcessesList.Program.Main(String[] args) in c:\Visual Studio 2012\Projects\ProcessesList\ProcessesList\Program.cs:line 18
Step 7. Lets’ analyze the error. The error clearly says, 32-bit process, can’t access modules of a 64-bit process. I am testing this Program, on Windows 7 64-bit Operating System. And the System may have both 32-bit & 64-bit processes running in the System.
When we call the GetProcesses
method, that returns all the processes (32-bit & 64-bit processes). Our program is a 32-bit process and trying to access a module of a 64-bit process. Hence we are seeing this issue.
How to resolve this issue.? Either we change our program to 64-bit or for both.
Step 8. We will change our program to target 32-bit & 64-bit platforms. To set this:
- Go to Project Properties and select the Build option.
- Under Platform target: select “Any CPU” and un-check “Prefer 32-bit” to keep our program running on both 32-bit & 64-bit Operating Systems.
Step 9. Lets’ re-build & run the program. Have you observed our program displays the list of processes.? And also it displays below error:
Unhandled Exception: System.ComponentModel.Win32Exception: Unable to enumerate the process modules.
at System.Diagnostics.NtProcessManager.GetModuleInfos(Int32 processId, Boolean firstModuleOnly)
at System.Diagnostics.NtProcessManager.GetFirstModuleInfo(Int32 processId)
at System.Diagnostics.Process.get_MainModule()
at ProcessesList.Program.Main(String[] args) in c:\Visual Studio 2012\Projects\ProcessesList\ProcessesList\Program.cs:line 24
Step 10. Why did this time are seeing we the error even though we set our program’s target to “Any CPU”.? The reason is, our program is trying to access modules of System processes. These are “System Idle Process” & “System“. We need to handle this separately.
- Lets’ include exception-handling to handle Win32Exception exceptions.
- And handle the program differently for “System Idle Process” & “System” processes.
Step 11. After adding exception handling our program looks like below:
Step 12. Re-build & Run the program. Now you see all the processes which are currently running.
Enjoy coding. 🙂
I hope you like this Article. Please give your feedback through comments.
(Raju)