Reputation: 159
When I run a Process
for the second time, I get an error when attempting to Cancel previous read line as shown here:
However this is where things get funky. If I "physically" move the execution point to the BeginErrorReadLine()
it should work right? Because no async operations are in progress right?
WRONG!
This... is just mindboggling and I have no idea how to fix it ;_;
Upvotes: 1
Views: 715
Reputation: 941417
This happens because you are trying to re-use the Process object. Not a valid thing to do, lots of .NET classes behave this way. Like Thread, Form, Socket, etcetera. You only use an object of them once, when you are through with them then you are supposed to Dispose() the object and create a new one if you want to do the same thing again.
After the 1st process stopped running, _process
turns into a zombie. Some of its properties are still useful, you can for example use ExitCode to discover how it ended, ExitTime to find out when that happened. But that's where it ends, the object is no longer useful for anything else and should be disposed. You get the exception on CancelErrorRead() because you're done reading error output. And the exception on BeginErrorReadLine() because you already made that call for the 1st process.
You must insert this line ahead, probably tinker with some other code we cannot see:
_process = new Process();
Favor its Exited event to deal with cleanup.
Upvotes: 4