Async, Await 是基于 .NEt 4.5架构的, 用于处理异步,防止死锁的方法的开始和结束, 提高程序的响应能力。比如:

Application area           Supporting APIs that contain async methods

Web access                    HttpClient , SyndicationClient

Working with files           StorageFileStreamWriterStreamReaderXmlReader

Working with images       MediaCaptureBitmapEncoderBitmapDecoder

WCF programming          Synchronous and Asynchronous Operations

Async, Await 不会产生新的线程。 但是Task.Run 方法就是多线程的。 Asynchronous code is code that returns upon completion. By Task.Run, we can make is a multithreaded.

Take files as an example I encountered today:

        public async Task SendFallMail(string path){
try
{
mail.Subject = subjectFall;
mail.Body = "Fall down!"; using (MailMessage ms = mail) {
ms.Attachments.Add(new Attachment(path));
using (SmtpClient sc = SmtpServer)
{
Task t = Task.Factory.StartNew(() =>
{
sc.Send(ms);
});
await t;
}
}
File.Delete(path);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}

It should wait t which is sending an e-mail wihich attached an picture in path.

Then delete the pic in path, wihch is working properly after I use the using statement...

Without using the using statement, it shows IO exception said:

Process Cannot Access the file “\Path” because it is being used by some other process

C# Async, Await and using statements的更多相关文章

  1. Async/Await FAQ

    From time to time, I receive questions from developers which highlight either a need for more inform ...

  2. Async/Await - Best Practices in Asynchronous Programming z

    These days there’s a wealth of information about the new async and await support in the Microsoft .N ...

  3. promise async await使用

    1.Promise (名字含义:promise为承诺,表示其他手段无法改变) Promise 对象代表一个异步操作,其不受外界影响,有三种状态: Pending(进行中.未完成的) Resolved( ...

  4. JavaScript 的 Async\/Await 完胜 Promise 的六

    参考:http://www.10tiao.com/html/558/201705/2650964601/1.html Node 现在从版本 7.6 开始就支持 async/await 了. 简介: A ...

  5. 【转】6 Reasons Why JavaScript’s Async/Await Blows Promises Away (Tutorial)

    原文:https://hackernoon.com/6-reasons-why-javascripts-async-await-blows-promises-away-tutorial-c7ec105 ...

  6. async/await的使用以及注意事项

    使用 async / await, 搭配 promise, 可以通过编写形似同步的代码来处理异步流程, 提高代码的简洁性和可读性. 本文介绍 async / await 的基本用法和一些注意事项. a ...

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

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

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

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

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

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

随机推荐

  1. vsftpd安装配置 530 Permission denied.错误

    yum install vsftpd service vsftpd start 530 Permission denied.错误 /etc/vsftpd/user_list    该文件里的用户账户在 ...

  2. MyBatis源码分析-SQL语句执行的完整流程

    MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可以对配置和原生Map使用简 ...

  3. [No00009E]几种常见的命名规则

    变量命名规则 必须遵循的命名规则 1.    变量名首字母必须为字母(a-z A-Z),下划线(_),或者美元符号($)开始php编程中所有变量必须以$开始. 2.    变量名只能是字母(a-z A ...

  4. 解决子元素设置margin-top,效果到父元素上的问题

    有时当我们设置子元素的margin-top,但是却发现子元素没有出现上外边距的效果,反而是父元素出现了上外边距的效果. 这种问题的解决方法如下: 1.给父元素加边框. 2.给父元素设置padding- ...

  5. 《----css样式---------浮动带来的影响与解决方法---------------》

    浮动就是让我们的元素脱离标准文档流,目的是为了布局好看! 浮动的现象: 脱离标准文档流被叫做脱流,同时会出现字围现象. 浮动的元素会相互贴靠,而且如果父容器空间足够大,则浮动的元素会正常紧靠也就是后一 ...

  6. [LeetCode] Binary Tree Right Side View 二叉树的右侧视图

    Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...

  7. JSP中编译指令include与动作指令include的区别

    include指令是编译阶段的指令,即include所包含的文件的内容是编译的时候插入到JSP文件中,JSP引擎在判断JSP页面未被修改, 否则视为已被修改.由于被包含的文件是在编译时才插入的,因此如 ...

  8. framebuffer line_length 參數

    以下是在 trace 關機充電動畫所遇到一個疑惑, 關於 framebuffer device 的屬性 line_length 大小值, 一個 display panel,1920 × 1080, 每 ...

  9. SpringMVC org.xml.sax.SAXParseException: cvc-complex-type.2.4.c 报错处理方式

    使用SpringMVC的过程中需要访问静态文件,即在spring-servlet.xml文件中添加了 <mvc:default-servlet-handler /> 标签,以开启Sprin ...

  10. 【BZOJ-4726】Sabota? 树形DP

    4726: [POI2017]Sabota? Time Limit: 20 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 128  Solved ...