AsyncEx icon indicating copy to clipboard operation
AsyncEx copied to clipboard

AsyncStream helper class

Open manandre opened this issue 6 years ago • 4 comments

I have recently used the AsyncEx librairies in a personal GitHub project and I identified an helper class which could be added to it. I have called it AsyncStream as it is the asynchronous version of the System.IO.Stream class. It can be used to create a custom implementation of the Sytem.IO.Stream class by overriding only asynchronous ReadAsync, WriteAsync and FlushAsync methods.

Would it fit in well with AsyncEx ?

manandre avatar Aug 30 '19 23:08 manandre

I have just discovered that @StephenCleary has already written such a class that he called AsyncStreamBase inside the ProducerConsumerStream repository. It is quite the same approach, except that the protected methods have an additional bool sync parameter… I do not see why we should support a sync mode inside an async method !?! Does someone have an explanation ?

manandre avatar Sep 07 '19 17:09 manandre

I've not seen that particular class, but being familiar with Stephen's style, I'd surmise that the class exposes both synchronous and asynchronous api. The "bool sync" parameter is used by stephen to allow the public synchronous methods to re-use code that is also used by the public asynchronous methods, when there is no way to avoid having the synchronous method synchronously wait on async code.

bboyle1234 avatar Sep 24 '19 00:09 bboyle1234

Yes, the bool sync technique is described here: https://msdn.microsoft.com/en-us/magazine/mt238404.aspx

StephenCleary avatar Sep 26 '19 00:09 StephenCleary

Thanks, now I understand the intent of the bool sync pattern. I have updated the AsyncStream class inside my project accordingly. I propose to add it as an helper class to the AsyncEx library. Do you agree ? If yes, are you interested in a PR from me ?

manandre avatar Sep 30 '19 17:09 manandre