Skip to Main Content
Programming WPF, 2nd Edition
book

Programming WPF, 2nd Edition

by Chris Sells, Ian Griffiths
August 2007
Intermediate to advanced content levelIntermediate to advanced
864 pages
25h 52m
English
O'Reilly Media, Inc.
Content preview from Programming WPF, 2nd Edition

The Event-Based Asynchronous Pattern

Some components allow you to perform asynchronous work without having to worry about the details of the dispatcher. This is possible thanks to the event-based asynchronous pattern, which was introduced in NET 2.0. Components that implement this pattern manage the necessary thread switching for you. They do so using the AsyncOperationManager family of classes,[129] which abstract away the details of UI threading requirements, supporting both Windows Forms and WPF through a common API. This means that classes designed for use in Windows Forms applications will also work correctly in WPF applications.

The event-based asynchronous pattern is fairly simple. A class will provide one or more methods whose names end in Async. For example, the WebClient class in the System.Net namespace offers an UploadFileAsync method. Each asynchronous method has a corresponding event to signal completion—UploadFileCompleted, in this case. There may optionally be other events to indicate partial progress, such as the UploadProgressChanged event offered by WebClient. The crucial feature of the event-based asynchronous pattern is that the events are raised on the UI thread. For example, if you call UploadFileAsync from the UI thread of a WPF application, the object will raise the UploadFileCompleted event on the same thread.

Not all components offer this pattern. Fortunately, .NET provides an implementation of the pattern that you can use to wrap slow, synchronous code: ...

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.
Start your free trial

You might also like

Programming C#, 4th Edition

Programming C#, 4th Edition

Jesse Liberty
Programming C# 10

Programming C# 10

Ian Griffiths

Publisher Resources

ISBN: 9780596510374Supplemental ContentErrata Page