Appendix B. Recognizing and Interpreting Asynchronous Patterns
The benefits of asynchronous code have been well understood for decades before .NET was invented. In the early days of .NET, several different styles of asynchronous code were developed, used here and there, and eventually discarded. These were not all bad ideas; many of them paved the way for the modern async/await approach. However, there’s a lot of legacy code out there that uses older asynchronous patterns. This appendix will discuss the more common patterns, explaining how they work and how to integrate them with modern code.
Sometimes, the same type is updated over the years, acquiring more and more members as it supports multiple asynchronous patterns. Perhaps the best example of this is the Socket class. Here are some of the members of the Socket class for the core Send operation:
classSocket{// SynchronouspublicintSend(byte[]buffer,intoffset,intsize,SocketFlagsflags);// APMpublicIAsyncResultBeginSend(byte[]buffer,intoffset,intsize,SocketFlagsflags,AsyncCallbackcallback,objectstate);publicintEndSend(IAsyncResultresult);// Custom, very close to APMpublicIAsyncResultBeginSend(byte[]buffer,intoffset,intsize,SocketFlagsflags,outSocketErrorerror,AsyncCallbackcallback,objectstate);publicintEndSend(IAsyncResultresult,outSocketErrorerror);// CustompublicboolSendAsync(SocketAsyncEventArgse);// TAP (as an extension method)publicTask<int>SendAsync(ArraySegment ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access