C# Async, Await and using statements
Async, Await 是基于 .NEt 4.5架构的, 用于处理异步,防止死锁的方法的开始和结束, 提高程序的响应能力。比如:
Application area Supporting APIs that contain async methods
Web access HttpClient , SyndicationClient
Working with files StorageFile, StreamWriter, StreamReader, XmlReader
Working with images MediaCapture, BitmapEncoder, BitmapDecoder
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的更多相关文章
- Async/Await FAQ
From time to time, I receive questions from developers which highlight either a need for more inform ...
- 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 ...
- promise async await使用
1.Promise (名字含义:promise为承诺,表示其他手段无法改变) Promise 对象代表一个异步操作,其不受外界影响,有三种状态: Pending(进行中.未完成的) Resolved( ...
- JavaScript 的 Async\/Await 完胜 Promise 的六
参考:http://www.10tiao.com/html/558/201705/2650964601/1.html Node 现在从版本 7.6 开始就支持 async/await 了. 简介: A ...
- 【转】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 ...
- async/await的使用以及注意事项
使用 async / await, 搭配 promise, 可以通过编写形似同步的代码来处理异步流程, 提高代码的简洁性和可读性. 本文介绍 async / await 的基本用法和一些注意事项. a ...
- 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 ...
随机推荐
- 最实用的IT类网站及工具大集合
1.聚合数据 大家在开发过程中,可能会用到各种各样的数据,想找一些接口来提供一些数据.比如天气预报查询,火车时刻表查询,彩票查询,身份证查询等等.有了这个接口,直接调用即可.各种各样的API接口满足你 ...
- 项目自动化建构工具gradle 入门4——javaWeb在浏览器中显示helloWorld
在java应用中,其实做的最多的还是java web应用.所以现在我们做的就是用gradle构建一个简单的web项目,简单点,直接上代码吧. 1.进入目录D:\work\gradle\web,新建文件 ...
- Ubuntu 14.04 LTS 下升级 gcc 到 gcc-4.9、gcc-5 版本
如果没记错的话,阿里云ECS上的Ubuntu也是LTS版本. 如果还在使用较旧版本的Ubuntu,或者是Ubuntu LTS,那么我们是很难体验新版gcc的.怎么办呢? 我们或许可以自己去编译用旧版本 ...
- [转载]fullPage.js中文api 配置参数~
fullPage.js中文api 配置参数 选项 类型 默认值 说明 verticalCentered 字符串 true 内容是否垂直居中 resize 布尔值 false 字体是否随着窗口缩放而缩放 ...
- swift 简介和常量与变量 --- swift 入门
一.思维导图 二. 代码 //创建UIView 和按钮 let views = UIView(frame: CGRect(x:20, y: 20, width: 100, height: 100))/ ...
- mybatis关联映射
多对一: <!-- 方法一: --> <select id="getNewsListByPage" parameterType="com.zqc.sha ...
- [LeetCode] Nested List Weight Sum II 嵌套链表权重和之二
Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...
- lumia手机wp系统应用列表如何设置按照拼音
1.安装应用多了就会这样·· 2.想用拼音排列,请把系统设置里的区域语言中的区域和格式改为中国,此时,屏幕壁纸上是"四月十五日". 3.想用笔画排列,请把系统设置里的区域语言中的区 ...
- js小练习去掉指定的字符组成一句话输出
今天在codewar做练习题时,要求写一个函数把一个字符串去掉WUB这些多余的字符然后把剩下的组成一句话输出,如传入"WUBAWUBBWUBCWUB"后返回"A B C& ...
- 清北 Noip 2016 考前刷题冲刺济南班
2016 10 29 周六 第一天 %%%,%ZHX大神 上午,60分, 下午,爆零orz 2016 10 30 周天 第二天 炒鸡倒霉的一天 %%%,%ZHX大神 据大神第一天的题最简单. 上午,和 ...