C# async await的使用
async 声明一个包含异步代码的函数,该函数执行时不会阻塞调用线程。
async标记的函数返回值必须为 void ,Task,Task<TResult>
await 必须修饰Task 或者Task<TResult>
await之后的代码运行线程:对于纯console工程,还是耗时任务的线程,
对于winform线程,则是调用线程。
典型代码

public static async Task<int> CalAsync()
{
string tid = Thread.CurrentThread.ManagedThreadId.ToString();
Console.WriteLine("当前位置async函数,await之前,线程ID"+tid);
int result = await Task.Run(new Func<int>(Cal));
tid = Thread.CurrentThread.ManagedThreadId.ToString();
Console.WriteLine("当前位置async函数,await之后,线程ID" + tid);
return result;
}
全部代码
class Program
{
static void Main(string[] args)
{
string tid = Thread.CurrentThread.ManagedThreadId.ToString();
Console.WriteLine("当前位置主函数,调用async异步之前,线程ID"+tid);
Task<int> t = CalAsync();
Console.WriteLine("当前位置主函数,调用async异步之后,线程ID" + tid);
Console.Read(); }
public static async Task<int> CalAsync()
{
string tid = Thread.CurrentThread.ManagedThreadId.ToString();
Console.WriteLine("当前位置async函数,await之前,线程ID"+tid);
int result = await Task.Run(new Func<int>(Cal));
tid = Thread.CurrentThread.ManagedThreadId.ToString();
Console.WriteLine("当前位置async函数,await之后,线程ID" + tid);
return result;
} public static int Cal()
{
string tid = Thread.CurrentThread.ManagedThreadId.ToString();
Console.WriteLine("当前位置耗时函数,线程ID"+tid);
int sum = ;
for (int i = ; i < ; i++)
{
sum = sum + i;
}
Console.WriteLine("当前位置耗时函数完成,线程ID" + tid);
return sum;
} }
输入内容

winform中优雅的实现
private async void button1_Click(object sender, EventArgs e)
{
var t = Task.Run(() =>
{
Thread.Sleep();
return "Returning from TimeConsuming task";
});
this.Text = await t;
}
参考
https://www.cnblogs.com/doforfuture/p/6293926.html
C# async await的使用的更多相关文章
- async & await 的前世今生(Updated)
async 和 await 出现在C# 5.0之后,给并行编程带来了不少的方便,特别是当在MVC中的Action也变成async之后,有点开始什么都是async的味道了.但是这也给我们编程埋下了一些隐 ...
- [.NET] 利用 async & await 的异步编程
利用 async & await 的异步编程 [博主]反骨仔 [出处]http://www.cnblogs.com/liqingwen/p/5922573.html 目录 异步编程的简介 异 ...
- [.NET] 怎样使用 async & await 一步步将同步代码转换为异步编程
怎样使用 async & await 一步步将同步代码转换为异步编程 [博主]反骨仔 [出处]http://www.cnblogs.com/liqingwen/p/6079707.html ...
- [.NET] 利用 async & await 进行异步 IO 操作
利用 async & await 进行异步 IO 操作 [博主]反骨仔 [出处]http://www.cnblogs.com/liqingwen/p/6082673.html 序 上次,博主 ...
- [C#] 走进异步编程的世界 - 开始接触 async/await
走进异步编程的世界 - 开始接触 async/await 序 这是学习异步编程的入门篇. 涉及 C# 5.0 引入的 async/await,但在控制台输出示例时经常会采用 C# 6.0 的 $&qu ...
- ASP.NET 中的 Async/Await 简介
本文转载自MSDN 作者:Stephen Cleary 原文地址:https://msdn.microsoft.com/en-us/magazine/dn802603.aspx 大多数有关 async ...
- C# async/await 使用总结
今天搞这两个关键字搞得有点晕,主要还是没有彻底理解其中的原理. 混淆了一个调用异步方法的概念: 在调用异步方法时,虽然方法返回一个 Task,但是其中的代码已经开始执行.该方法在调用时,即刻执行了一部 ...
- 【转】async & await 的前世今生
async 和 await 出现在C# 5.0之后,给并行编程带来了不少的方便,特别是当在MVC中的Action也变成async之后,有点开始什么都是async的味道了.但是这也给我们编程埋下了一些隐 ...
- async & await 的前世今生
async 和 await 出现在C# 5.0之后,给并行编程带来了不少的方便,特别是当在MVC中的Action也变成async之后,有点开始什么都是async的味道了.但是这也给我们编程埋下了一些隐 ...
- C# Async, Await and using statements
Async, Await 是基于 .NEt 4.5架构的, 用于处理异步,防止死锁的方法的开始和结束, 提高程序的响应能力.比如: Application area Support ...
随机推荐
- 题解 [ZJOI2008]树的统计Count
[ZJOI2008]树的统计Count Description 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u ...
- C# List分组
//分组 8个为一组 List<List<string>> ArrayList = sArray.Select((x, i) => new { Index = i, Va ...
- mysql:where和having的区别
where在查询数据库结果返回之前对查询条件进行约束,就是结果返回之前起作用,而having是查询数据库,已经得到返回的结果了,再对结果进行过滤.(结果返回前,结果返回后) where条件不能使用聚合 ...
- 与word、excel交互问题总结
不同版本的Office对应的型号不同,往往问题出现在注册表中有多个版本,所以程序运行经常提示错误. 1.找不到引用microsoft.office.core解决办法 (引用中有感叹号,说明引用不成功) ...
- 从gcc到Makefile简易版
1.Makefile的应用 我们主要用它来编译源代码,生成结果代码,然后把结果代码连接起来生成可执行文件或者库文件.2.Makefle简单例子的深入学习 程序概述:为了连接makefile的流程,我将 ...
- codeforces297B
Fish Weight CodeForces - 297B It is known that there are k fish species in the polar ocean, numbered ...
- JavaWeb_(Hibernate框架)使用c3p0与Dbutils开发用户注册功能
使用c3p0与Dbutils开发用户注册功能: 用户在register.jsp表单成功后,页面跳转到login.html,数据库中会存放用户注册的信息 <%@ page language=&qu ...
- HDU 5894 hannnnah_j’s Biological Test ——(组合数)
思路来自于:http://blog.csdn.net/lzedo/article/details/52585170. 不过并不需要卢卡斯定理,直接组合数就可以了. 代码如下: #include < ...
- koa 基础(十五)cookie 设置中文
1.app.js // 引入模块 const Koa = require('koa'); const router = require('koa-router')(); /*引入是实例化路由 推荐*/ ...
- 用Java实现一个二叉树
介绍 使用Java实现一个int值类型的排序二叉树 二叉树 二叉树是一个递归的数据结构,每个节点最多有两个子节点. 通常二叉树是二分查找树,每个节点它的值大于或者等于在它左子树节点上的值,小于或者等于 ...