嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元微信扫码支付:2 元
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
Asynchronous Programming Succinctly
Table of Contents
About the Author ......................................................................................................................................... 5
Chapter 1 Getting Started.......................................................................................................................... 6
What is asynchronous programming? ..................................................................................................... 6
Examples of potentially blocking activities ............................................................................................... 6
Writing async methods ............................................................................................................................. 7
Avoid deadlocks when using async ....................................................................................................... 11
Chapter 2 How Do I Use Async ............................................................................................................... 15
Exploring async further .......................................................................................................................... 15
Async method return types .................................................................................................................... 15
Object Oriented Programming and async .............................................................................................. 29
Abstract classes ..................................................................................................................................... 29
Chapter 3 Some Real World Examples .................................................................................................. 34
Displaying the progress of an async method ......................................................................................... 34
Pausing the progress of an async method ............................................................................................ 38
Using Task.WhenAll() to wait for all tasks to complete .......................................................................... 42
Using Task.WhenAny() to wait for any tasks to complete ..................................................................... 47
Process tasks as they complete ............................................................................................................ 54
Chapter 4 Use SemaphoreSlim to Access Shared Data ...................................................................... 61
Chapter 5 Unit Tests and async and await ............................................................................................ 69
An overview of unit testing async methods............................................................................................ 69
Create a unit test in Visual Studio .......................................................................................................... 69
Avoiding deadlocks in unit tests ............................................................................................................. 82
Final thoughts ........................................................................................................................................ 84
Additional resources—where to learn more........................................................................................... 85
5
About the Author
Dirk Strauss is a software developer and Microsoft .NET MVP from South Africa with over 13
years of programming experience.
Starting his career at a small software development house in Port Elizabeth, he moved on to
EOH Applications in 2007. He spent the next 8 years writing software to integrate into SYSPRO.
It was during this time that he started blogging frequently to avoid stagnating and to keep on
learning.
In 2015 he joined the Evolution Software team where he lives out his creativity and works with
incredibly inspirational individuals. During this time he published two books on C# as well as a
few e-books. He continues learning and sharing whenever he can. You can find him
@DirkStrauss on Twitter or on his blog dirkstrauss.com.
6
Chapter 1 Getting Started
What is asynchronous programming?
Recently, a friend of mine was tasked with improving a Windows Forms application that had
been written about four years ago. As I looked on, I saw that calls to the database locked up the
UI for several seconds. The search functionality (which searched a table consisting of only
60,000 entries) took from 30 seconds to a minute to return results to the form. This meant that
while the application was querying the database and doing what it needed to do, the UI was
totally unresponsive. The user could not resize it, move it around, or interact with it in any way.
Instead, users had to sit and wait for the application to respond. Reading and writing files was
equally frustrating. The unresponsiveness of the application created negative feelings—that the
application was malfunctioning, was old (which was technically true), and was no good.
In actual fact, the application was not malfunctioning. The search returned true results from the
database. Saving information to the database worked every time, and the files the application
created or read always worked. Nothing was “broken,” but the fact that the UI was slow and
unresponsive at times (especially as the customer’s workload increased) made using the app
extremely frustrating.
While this situation might have been due to a combination of poor code and inefficient SQL
statements, in fact the form was not responsive because it was doing everything
synchronously—any process that accesses the UI thread (UI-related tasks usually share a
single thread) will be blocked in a synchronous application. Therefore, if one process is blocked,
all processes are blocked.
Asynchrony is the saving grace for these kinds of applications. Using asynchronous methods
will allow the application to remain responsive. The user can resize, minimize, maximize, move
the form around, or even close the application should they wish to. Introducing asynchronous
functionality into applications had long been a complicated endeavour for developers, but the
introduction of async programming in Visual Studio 2012 introduced a simplified approach.
Async programming leveraged the .NET Framework 4.5, which made it easier to code and
moved all the heavy lifting and complicated code to the compiler. Don’t get me wrong, though—
while asynchronous programming is still a complex bit of technology, it has been made much
easier to use.
Examples of potentially blocking activities
We now know one thing: async improves the responsiveness of your application. But just which
activities are potentially blocking? For example, take accessing a web resource. In a
synchronous scenario, the entire program will be blocked if the web resource being accessed is
slow or overloaded. The following list defines several areas in which async programming will
improve the responsiveness of your application.
7
Web Access
• HttpClient
• SyndicationClient