在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例常用C#方法 → Asynchronous Programming Succinctly

Asynchronous Programming Succinctly

常用C#方法

下载此实例
  • 开发语言:C#
  • 实例大小:2.11M
  • 下载次数:10
  • 浏览次数:79
  • 发布时间:2022-04-23
  • 实例类别:常用C#方法
  • 发 布 人:孔佑
  • 文件格式:.pdf
  • 所需积分: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

标签: 异步编程

实例下载地址

Asynchronous Programming Succinctly

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警