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. cal命令详解与练习

    cal: 显示日历. 命令格式: cal [-smjy13] [[[day] month] year] 参数说明 -1 显示当前月日历 -3 显示当前月前后3月的日历 -s 以星期天为第一天显示 -m ...

  2. Ajax.BeginForm 防止跳转到新页面

    @using (Ajax.BeginForm("ChangeCompanyId", "navigation", new { area = "confi ...

  3. ECharts 是一款开源

    ECharts

  4. python 列表 字典 读写文件:pickle模块的基本使用

    python数据持久存储:pickle模块的基本使用(转载) 作者: pzxbc 出处: http://pzxbc.cnblogs.com/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保 ...

  5. jquery图片上传

    总结两个图片上传的方法: 一:使用jquery.form中的ajaxSubmit来实现上传. <script src="~/JavaScript/jquery-1.10.2.min.j ...

  6. CentOS 基础安装

    1. 下载了 CentOS 的最小安装版本 与 VMware,基础安装流程参考百度经验:http://jingyan.baidu.com/article/eae0782787b4c01fec54853 ...

  7. Matlab 符号运算

    root(p):多项式求根.多项式等于0时对应方程的根. 例:,则输入p=[5 4 3 2 1]; root(p) 注:多项式系数都是按幂指数递减形式的. poly([a,b,c]):求已知根为a,b ...

  8. 【Git】Git远程操作详解

    Git是目前最流行的版本管理系统,学会Git几乎成了开发者的必备技能. Git有很多优势,其中之一就是远程操作非常简便.本文详细介绍5个Git命令,它们的概念和用法,理解了这些内容,你就会完全掌握Gi ...

  9. java String字符串进行排序

    public String afterSort(String s){        char[] ss = s.toCharArray();        Arrays.sort(ss);       ...

  10. 【转】Android开发学习笔记:EditText的属性介绍

    原文网址:http://liangruijun.blog.51cto.com/3061169/627350 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追 ...