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的更多相关文章

  1. async & await 的前世今生(Updated)

    async 和 await 出现在C# 5.0之后,给并行编程带来了不少的方便,特别是当在MVC中的Action也变成async之后,有点开始什么都是async的味道了.但是这也给我们编程埋下了一些隐 ...

  2. [.NET] 利用 async & await 的异步编程

    利用 async & await 的异步编程 [博主]反骨仔 [出处]http://www.cnblogs.com/liqingwen/p/5922573.html  目录 异步编程的简介 异 ...

  3. [.NET] 怎样使用 async & await 一步步将同步代码转换为异步编程

    怎样使用 async & await 一步步将同步代码转换为异步编程 [博主]反骨仔 [出处]http://www.cnblogs.com/liqingwen/p/6079707.html  ...

  4. [.NET] 利用 async & await 进行异步 IO 操作

    利用 async & await 进行异步 IO 操作 [博主]反骨仔 [出处]http://www.cnblogs.com/liqingwen/p/6082673.html  序 上次,博主 ...

  5. [C#] 走进异步编程的世界 - 开始接触 async/await

    走进异步编程的世界 - 开始接触 async/await 序 这是学习异步编程的入门篇. 涉及 C# 5.0 引入的 async/await,但在控制台输出示例时经常会采用 C# 6.0 的 $&qu ...

  6. [译] C# 5.0 中的 Async 和 Await (整理中...)

    C# 5.0 中的 Async 和 Await [博主]反骨仔 [本文]http://www.cnblogs.com/liqingwen/p/6069062.html 伴随着 .NET 4.5 和 V ...

  7. await and async

    Most people have already heard about the new “async” and “await” functionality coming in Visual Stud ...

  8. C# await和async

    基础阅读:http://www.cnblogs.com/jesse2013/p/async-and-await.html 答疑阅读:http://www.cnblogs.com/heyuquan/ar ...

  9. C#~异步编程再续~await与async引起的w3wp.exe崩溃-问题友好的解决

    返回目录 关于死锁的原因 理解该死锁的原因在于理解await 处理contexts的方式,默认的,当一个未完成的Task 被await的时候,当前的上下文将在该Task完成的时候重新获得并继续执行剩余 ...

随机推荐

  1. windows批处理命令之ren

    1.批处理批量修改文件后缀名(假设我需要把一个文件夹中的很多txt文件改为sql文件): 1)在需要被处理的文件的文件夹里先新建一个txt文本,然后在文本中写入: ren *.txt *.sql 2) ...

  2. 在jQuery.getData中renderCallback使用不同创建方式的结果

    JavaScript 中需要创建函数的话,有两种方法:函数声明.函数表达式,各自写法如下:// 方法一:函数声明function foo() {}// 方法二:函数表达式var foo = funct ...

  3. Firefox--Date.parse()

    比较两个时间字符串的时间大小用到了Date.parse()这个静态方法. 火狐下:Date.parse("2014-07-05 22:05:05") 返回的是NaN. 其它浏览器: ...

  4. centos 安装 mongo3.0

    官方网站传贴,每次去翻doc,麻烦 vi  /etc/yum.repos.d/mongodb-org-3.0.repo [mongodb-org-3.0]name=MongoDB Repository ...

  5. CLOUDSTACK接管VCENTER,意外频出,但最终搞定

    没办法,第一次吃螃蟹,总是经常住. 还好,我有一颗不肯媚俗的心~~ 但二级存储,和 存储的性能,也必须纳入考虑范围了.

  6. hadoop安装问题记录

    start-yarn.sh 启动正常,但是无法访问网页http://localhost:8088/cluster 原因: 可能是ipv6 的问题 解决方法: http://stackoverflow. ...

  7. Sql语句中IN等方面的用法

    select * from txt1 select * from txt2 select * from txt1 where name in (select name from txt2 where ...

  8. J - Borg Maze - poj 3026(BFS+prim)

    在一个迷宫里面需要把一些字母.也就是 ‘A’ 和 ‘B’连接起来,求出来最短的连接方式需要多长,也就是最小生成树,地图需要预处理一下,用BFS先求出来两点间的最短距离, *************** ...

  9. XML的四种解析器原理及性能比较

    转自zsq 1.DOM     DOM 是用与平台和语言无关的方式表示 XML 文档的官方 W3C 标准.DOM 是以层次结构组织的节点或信息片断的集合.这个层次结构允许开发人员在树中寻找特定信息.分 ...

  10. JS验证手机号码

    对于表单的验证是添加信息的时候必不可少的. 下面是基于EasyUI-validatebox拓展的对手机.座机号的验证方法. PhoneAndMobile: { validator: function ...