1 Star 5 Fork 4

s0611163 / AsyncAwaitDemo2

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
Clone or Download
contribute
Sync branch
Cancel
Notice: Creating folder will generate an empty file .keep, because not support in Git
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.

About

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

Releases

No release

Contributors

All

Activities

Load More
can not load any more
C#
1
https://gitee.com/s0611163/AsyncAwaitDemo2.git
git@gitee.com:s0611163/AsyncAwaitDemo2.git
s0611163
AsyncAwaitDemo2
AsyncAwaitDemo2
main

Search

53164aa7 5694891 3bd8fe86 5694891