await, anync
public Form1()
{
InitializeComponent();
} // The following method runs asynchronously. The UI thread is not
// blocked during the delay. You can move or resize the Form1 window
// while Task.Delay is running.
public async Task<string> WaitAsynchronouslyAsync()
{
await Task.Delay();
MessageBox.Show("fi");
return "Finished";
} // The following method runs synchronously, despite the use of async.
// You cannot move or resize the Form1 window while Thread.Sleep
// is running because the UI thread is blocked.
public async Task<string> WaitSynchronously()
{
// Add a using directive for System.Threading.
Thread.Sleep();
return "Finished";
} private async void button1_Click_1(object sender, EventArgs e)
{
string result = string.Empty;
WaitAsynchronouslyAsync(); // Call the method that runs asynchronously.
//result = await WaitAsynchronouslyAsync();
// Call the method that runs synchronously.
// result = await WaitSynchronously (); // Display the result.
textBox1.Text += result;
MessageBox.Show("hi");
}
上述代码得出至少两点:
1. await WaitAsynchronouslyAsync() 执行时要等待这个调用完成,才会执下后面的代码,但在这个调用没有完成前,其thread是可以被其它代码占用的,表现就是UI仍可以接收其它消息
2. WaitAsynchronouslyAsync()函数执行的线程与UI主线程是同一个,这也是为什么在这个函数中的messagebox可以显示,Thread.sleep可以阻止UI线程接收消息
await, anync的更多相关文章
- 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 ...
- [译] C# 5.0 中的 Async 和 Await (整理中...)
C# 5.0 中的 Async 和 Await [博主]反骨仔 [本文]http://www.cnblogs.com/liqingwen/p/6069062.html 伴随着 .NET 4.5 和 V ...
- await and async
Most people have already heard about the new “async” and “await” functionality coming in Visual Stud ...
- C# await和async
基础阅读:http://www.cnblogs.com/jesse2013/p/async-and-await.html 答疑阅读:http://www.cnblogs.com/heyuquan/ar ...
- C#~异步编程再续~await与async引起的w3wp.exe崩溃-问题友好的解决
返回目录 关于死锁的原因 理解该死锁的原因在于理解await 处理contexts的方式,默认的,当一个未完成的Task 被await的时候,当前的上下文将在该Task完成的时候重新获得并继续执行剩余 ...
随机推荐
- javascript小数相减会出现一长串的小数位数的原因
javascript小数相减会出现一长串的小数位数的原因 <script> var a='38.8'; var b='6.8'; alert(parseFloat(a)-parseFloa ...
- java常用用代码
/** *Java获取IP代码 */ import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.ev ...
- 多目标遗传算法 ------ NSGA-II (部分源码解析) 目标函数值计算 eval.c
这部分比较简单,具体的函数数值计算是需要调用设定的目标函数的,此部分一个不能忽略的问题是 超出限制条件的处理 , 故对此加以解释. 首先是包装函数, 核心操作调用 evaluate_ind 实现 ...
- C51与汇编语言混合编程
函数内部混合编程 若想在C语言函数内部使用汇编语言,应使用以下Cx51编译器控制命令: #pragma asm ; Assembly code #pragma endasm 功能作用:asm和end ...
- OpenRisc-47-or1200的WB模块分析
引言 “善妖善老,善始善终”,说的是无论什么事情要从有头有尾,别三分钟热度. 对于or1200的流水线来说,MA阶段是最后一个阶段,也是整条流水线的收尾阶段,负责战场的清扫工作.比如,把运算指令的运算 ...
- 【HDOJ】1134 Game of Connections
Catlan数. /* 1134 */ import java.util.Scanner; import java.math.BigInteger; /* Catalan: (1) h(n) = h( ...
- poj2240 - Arbitrage(汇率问题,floyd)
题目大意: 给你一个汇率图, 让你判断能否根据汇率盈利 #include <iostream> #include <cstdlib> #include <cstdio&g ...
- //string scriptstrs = "<script>alert('欢迎光临!');</script>";
//string scriptstrs = "<script>alert('欢迎光临!');</script>"; //if (!Page.ClientSc ...
- ISNULL-sqlserver语句
语法 ISNULL ( check_expression , replacement_value ) 参数 check_expression 将被检查是否为 NULL的表达式.check_expres ...
- 【索引】XBox360玩机心得
基础知识 查看XBox360的系统版本信息:http://www.cnblogs.com/duxiuxing/p/4292140.html XBox360光盘游戏的安装: 自制系统 XBox360自制 ...