A lot has been written about the single responsibility principle over the years. Rather than rehash what many others have said better, I want to give a common example of how a seemingly innocuous control structure can subtly but surely…
EventHandler and asynchrony
Events in .NET are for the most part inherently synchronous multicast delegates. It is therefore no surprise that async and traditional EventHandler-based delegates do not mix. Today I’ll illustrate one design approach which I’ve used successfully to get around this…
Fluent async testing
Cellfish writes: When .net 4.5 was first in public beta most test frameworks had a bug making it impossible to write async test methods. I wish they had never fixed that because I don’t think those help you write good…
From sync to async: file I/O
In the previous post, I described the basics of switching to async for network calls. Today I will discuss the same for file operations. Reading and writing To read and write files asynchronously, look no further than System.IO.FileStream. You must…
From sync to async: network I/O
Converting from a fully synchronous design to an asynchronous one can be quite an ordeal. Unlike other types of design changes which can be safely contained, going async necessarily touches essentially every module and function chain that deals with I/O.…
The [a]waiting is the hardest part (in 3.5)
In the previous post I showed how to simulate async/await in .NET 4.0. This works great for Task-based async, but what about those who still use stone tools and .NET 3.5? Well, they can share in the fun, too! I…
Just can’t [a]wait (in 4.0)
Writing managed asynchronous code has been possible since the advent of IAsyncResult in .NET 1.0. However, it has not been easy until async/await in .NET 4.5. Unfortunately, there are situations where you really need to implement asynchrony but are stuck…
Hidden costs of ‘async’
In a very early post, I stated: An ‘async’ method is not free. The compiler does some complex code generation steps to morph your method into a proper asynchronous state machine with all the associated exception handling logic, management of…
If at first you don’t succeed
Retries are a fact of life in testing. That file you are trying to read might be temporarily locked. The network connection could momentarily break. You may need to keep pinging a server for status until it returns the result…
Introducing overlapped I/O
In Windows, I/O operations such as ReadFile can be performed synchronously or asynchronously. Asynchronous I/O is generally referred to as overlapped I/O since multiple operations can be issued at once and “overlap” in their request lifetimes. There are a few…