In Microsoft docs there's an example using DataReceivedEventHandler Delegate. However, since all variables are static, if we call SortInputListText()
2 times asynchronously, there would be a conflict where sortOutput
would be used by both. Seems like a pretty bad idea..
Is that wrong? What am I missing?
Does documentation generally contains a lot of errors?
What should I do to not have this conflict? (I'm currently learning delegates, events, etc.)
private static StringBuilder sortOutput = null; private static int numOutputLines = 0; ... public static void SortInputListText() { ... sortOutput = new StringBuilder(); ... } ... private static void SortOutputHandler(object sendingProcess, DataReceivedEventArgs outLine) { ... sortOutput.Append(Environment.NewLine $"[{numOutputLines}] - {outLine.Data}"); ... }
CodePudding user response:
Is that wrong? What am I missing?
It's a code example. Emphasis on example. Code examples are meant to illustrate the discussed/explained topic (in this case, the topic is the DataReceivedEventHandler). Code examples are not meant to be production code to be blindly copy&pasted into one's application.
This specific code example here is not about async or concurrent/parallel programming. The code example here is about DataReceivedEventHandler. Nothing more, nothing less.
(Don't get confused by the namespace name chosen in the code example. The example namespace "ProcessAsyncStreamSamples" does not refer to typical async programming using Task and the async/await keywords, but rather refers to the Output/ErrorDataReceived event handler being invoked/executed while the program is doing something else - in the case of this specific code example "doing something else" is simply waiting for the process to exit.)
Does documentation generally contains a lot of errors?
What??? No, generally not. Sure, there is bad and lackluster documentation. Sometimes there is barely any documentation at all. But there is also good and excellent documentation. I don't even know how you came to ask this question...
What should I do to not have this conflict? (I'm currently learning delegates, events, etc.)
As you already noticed, static methods are probably not a good fit for your application scenario. That's no error or fault in the code example, because the code example is not meant to be part of your application (because it's just a code example).
One possible approach of how you could address your issue (it's not the only one, but one that's perhaps relatively easy to implement and debug): Define a class that encapsulates a Process
instance, the handler method to be subscribed to the process' Output/ErrorDataReceived event, and any data that's specific to the process and/or the handler. This way, by having individual instances of this class, you can handle multiple processes each with their own Output/ErrorDataReceivedEvent handler and data without each of the handlers or their data getting in each others way. (Side note: since Process
is an IDisposable
, you probably want your class to implement IDisposable
as well...)