C# 异步操作 async await 的用法
2016-12-14 12:34
881 查看
1. async与 await 成对出现 async 在方法前使用 ,方法体面面用 await .
2. 使用async 和await定义异步方法不会创建新线程.
3.await 后面一定是一个扫行时间长的任务,要用 Task.Run(()=>{ }); 类似的代码来执行.
4.返回写成 Task<Type>. 后面的代码要等 这个执行完后 再执行.
5.界面不阻塞
例子:
private async void button2_Click(object sender, EventArgs e)
{
//异步方法
this.button2.Enabled = false;
long length = await AccessWebAsync();
this.button2.Enabled = true;
// 这里可以做一些不依赖回复的操作
OtherWork(); // 这里要等 AccessWebAsync 执行完后 才能执行
this.textBox1.Text += String.Format("\n 回复的字节长度为: {0}.\r\n", length);
txbMainThreadID.Text = Thread.CurrentThread.ManagedThreadId.ToString();
}
private void OtherWork()
{
this.textBox1.Text += "\r\n等待服务器回复中.................\n";
}
private async Task<long> AccessWebAsync()
{
//await Task.Run(() => { Thread.Sleep(5000); Console.WriteLine("bbb1"); Console.WriteLine("bbb2"); Console.WriteLine("bbb3"); });
MemoryStream content = new MemoryStream();
await Task.Run(() =>
{
// 对MSDN发起一个Web请求
HttpWebRequest webRequest = WebRequest.Create("http://msdn.microsoft.com/zh-cn/") as HttpWebRequest;
if (webRequest != null)
{
// 返回回复结果
using (WebResponse response = webRequest.GetResponse())
{
using (Stream responseStream = response.GetResponseStream())
{
responseStream.CopyTo(content);
}
}
}
});
txbAsynMethodID.Text = Thread.CurrentThread.ManagedThreadId.ToString();
return content.Length;
}
2. 使用async 和await定义异步方法不会创建新线程.
3.await 后面一定是一个扫行时间长的任务,要用 Task.Run(()=>{ }); 类似的代码来执行.
4.返回写成 Task<Type>. 后面的代码要等 这个执行完后 再执行.
5.界面不阻塞
例子:
private async void button2_Click(object sender, EventArgs e)
{
//异步方法
this.button2.Enabled = false;
long length = await AccessWebAsync();
this.button2.Enabled = true;
// 这里可以做一些不依赖回复的操作
OtherWork(); // 这里要等 AccessWebAsync 执行完后 才能执行
this.textBox1.Text += String.Format("\n 回复的字节长度为: {0}.\r\n", length);
txbMainThreadID.Text = Thread.CurrentThread.ManagedThreadId.ToString();
}
private void OtherWork()
{
this.textBox1.Text += "\r\n等待服务器回复中.................\n";
}
private async Task<long> AccessWebAsync()
{
//await Task.Run(() => { Thread.Sleep(5000); Console.WriteLine("bbb1"); Console.WriteLine("bbb2"); Console.WriteLine("bbb3"); });
MemoryStream content = new MemoryStream();
await Task.Run(() =>
{
// 对MSDN发起一个Web请求
HttpWebRequest webRequest = WebRequest.Create("http://msdn.microsoft.com/zh-cn/") as HttpWebRequest;
if (webRequest != null)
{
// 返回回复结果
using (WebResponse response = webRequest.GetResponse())
{
using (Stream responseStream = response.GetResponseStream())
{
responseStream.CopyTo(content);
}
}
}
});
txbAsynMethodID.Text = Thread.CurrentThread.ManagedThreadId.ToString();
return content.Length;
}
相关文章推荐
- async/await 异步操作 .net csharp C#
- C# 异步操作 async await
- async/await与promise(nodejs中的异步操作问题)
- C# 异步调用之async及await关键字
- C#中的异步:async和await
- C#异步编程之async、await
- [.NET] 利用 async & await 进行异步 IO 操作
- [C#] .NET4.0中使用4.5中的 async/await 功能实现异步
- 说说C#的async和await的具体用法
- 请教 C# 异步 async await 问题
- .NET/C# 使用async和await关键字调用异步方法
- .NET异步操作学习之一:Async/Await中异常的处理
- C#使用SocketAsyncEventArgs操作套接字的简单异步通讯
- 利用async和await异步操作解决node.js里面fs模块异步读写,同步结果的问题
- C# 笔记 异步操作:回调函数__AsyncCallback 委托
- C# Async/await 异步多线程编程
- 详解C#中的Async和Await用法
- C# Async/await 异步多线程编程
- [C#] .NET4.0中使用4.5中的 async/await 功能实现异步
- C#异步编程async await