1 Star 0 Fork 4

lim456561 / AsyncAwaitDemo2

forked from s0611163 / AsyncAwaitDemo2 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

通过一个示例形象地理解C# async await异步

开发环境

  1. VS2022
  2. .NET 6

测试步骤

  1. 点击VS2022的启动按钮,启动程序,它会先启动Server工程,再启动AsyncAwaitDemo2工程
  2. 快速连续点击5次button1
  3. 快速连续点击5次button2
  4. 观察思考输出结果,体会异步的好处

测试截图

测试截图

思考

把button2_Click方法中的await RequestAsync(_url);修改为两行:

await RequestAsync(_url);
await RequestAsync(_url);

那么5次点击,每次点击请求2次,共5×2=10次请求,请问这10次请求的总耗时大约是几秒? 答案是4秒,为什么? 因为每次点击的2次请求是顺序的,耗时是2+2=4秒,而5次点击是并发的,没有增加耗时时长

MIT License Copyright (c) 2023 s0611163 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

通过一个示例形象地理解C# async await异步 展开 收起
C#
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
C#
1
https://gitee.com/liming4675/AsyncAwaitDemo2.git
git@gitee.com:liming4675/AsyncAwaitDemo2.git
liming4675
AsyncAwaitDemo2
AsyncAwaitDemo2
main

搜索帮助