I was testing my C# console app today (ConsoleApp1), and ran pslist (Sysinternal tool) in terminal mode (pslist -s ConsoleApp1) to observe if only one instance is created. All was working ok, so I started to make some changes in code, and ended with an unhandled exception. When it was raised, I noticed that pslist showed 2 instances of a process with ConsoleApp1 name, and different PIDs. Out of curiosity, I did a minimal reproduction, and it looks like just throwing an exception in Main causes the new process to appear.
I was not able to see this second process in Task Manager, even in Administrative mode. I was only able to see it in pslist and in another console app, which was executing Process.GetProcessesByName in a loop. Parent process of the first one is explorer.exe (expected), while parent of the second one is the first process. Second one has the same MainModule (i.e. originates from the same executable), but its Main method is never executed.
Could anyone enlighten me what the second process is and why Task Manager doesn't display it?
Steps to reproduce:
- Create new Console app (tried in .NET 4.8 and .NET 6)
- Replace content of Main with
throw new Exception(); - Compile the code (no matter if this is Debug or Release)
- Run
pslist -s ConsoleApp1in a separate cmd window. Change the name if you used different one. - Run app by double clicking it in File Explorer and observe pslist window - both processes should be displayed for a second.