In .NET Framework 2.0, a new pattern for asynchronous programming was added, based on events to signal an operation’s completion. This approach makes dealing with asynchrony a little bit easier in the context of UI programming because you can use an event handler to handle the completion case. Controls such as the
PictureBox in Windows Forms use this pattern to handle long-running load operations (for example, when a picture is downloaded from the Web). Because this control is added to a designer surface, hooking up a
LoadCompleted event handler is fairly straightforward.
To understand the essentials of this pattern, we study the
WebClient class. To perform download operations, various methods are provided. To keep things simple, we look at the
DownloadString family of synchronous and asynchronous operations. The synchronous method is quite predictable: