Asynchronous operations can throw exceptions if the

Info icon This preview shows pages 679–681. Sign up to view the full content.

View Full Document Right Arrow Icon
Asynchronous operations can throw exceptions. If the exception is the result of bad input, such as a null reference where an object is required, the Begin method will throw an exception. But it’s possible that something failed while the operation was in progress—perhaps we lost network connectivity partway through some work. In this case, the End method will throw an exception. The Asynchronous Programming Model is widely used in the .NET Framework class library, and while it is an efficient and flexible way to support asynchronous operations, it’s slightly awkward to use in user interfaces. The completion callback typically hap- pens on some random thread, so you can’t update the UI in that callback. And the support for multiple simultaneous operations, possible because each operation is rep- resented by a distinct IAsyncResult object, may be useful in server environments, but it’s often just an unnecessary complication for client-side code. So there’s an alternative pattern better suited to the UI. The Event-Based Asynchronous Pattern Some classes offer an alternative pattern for asynchronous programming. You start an operation by calling a method whose name typically ends in Async ; for example, the WebClient class’s DownloadDataAsync method. And unlike the APM, you do not pass a delegate to the method. Completion is indicated through an event, such as the DownloadDataCompleted event. Classes that implement this pattern are required to use the SynchronizationContext class (or the related AsyncOperationManager ) to ensure that the event is raised in the same context in which the operation was started. So in a user interface, this means that completion events are raised on the UI thread. This is, in effect, a single-threaded asynchronous model. You have the responsiveness benefits of asynchronous handling of slow operations, with fewer complications than multithreaded code. So in scenarios where this pattern is an option, it’s usually the best choice, as it is far simpler than the alternatives. It’s not always available, because some classes offer only the APM. (And some don’t offer any kind of asynchronous API, in which case you’d need to use one of the other multithreading mechanisms in this chapter to maintain a responsive UI.) Asynchronous Programming | 655
Image of page 679

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Single-threaded asynchronous code is more complex than sequential code, of course, so there’s still scope for trouble. For example, you need to be careful that you don’t attempt to set multiple asynchronous op- erations in flight simultaneously that might conflict. Also, components that implement this pattern call you back on the right thread only if you use them from the right thread in the first place—if you use a mixture of this pattern and other multithreading mechanisms, be aware that operations you kick off from worker threads will not complete on the UI thread.
Image of page 680
Image of page 681
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern