Hello,
I got a running service that starts process (exe file with some parameters), when requests comes. That process is start with Process.Start. After executing Start() I am firing new thread that tries to attach to that started process by ID and sets an event when that process ends. ShellExecute is set to true.
runnerProc.Exited += ProcessEnded;
Now - everything works fine until some moment when I get lots of requests and many process are being started at almost the same time. Most of them are ok, but some hang. Meaning that new thread is started, it finds the process by id which is alive and sets the ending event for that process - this thread will be alive until the process is done running. But the problem is that the new process start with an error (not Application, but System type!!). In System logs I see this:"
Application popup: Runner.exe - Application Error : The application was unable to start correctly (0xc0000142). Click OK to close the application.
Now - it looks like my thread has started and attached to process, but since the process did not close - it thinks that it's running infinitely. What is more odd - when I check the task manager - such process with that ID is not running any more oddly somewhy the exit event was not called...
I was thinking if this is somehow related to ShellExecute set to True, but not really sure. All code has error handling so if anything goes wrong I get log about the process not being able to start. So in my case - I get no errors and RunnerProc.Exited gets event assigned properly - it just not getting called 1 out of ~50.
Any ideas what am I doing wrong?
Thanks!
What I have tried:
Tried various things here... kind of out of ideas now