使用Async和Await的异步编程
Posted vvull
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Async和Await的异步编程相关的知识,希望对你有一定的参考价值。
遇到真正的await后,由新线程接管,即操作开始异步。
不阻塞主线程
class Program
{
static void Main(string[] args)
{
Console.WriteLine($"主线程{Thread.CurrentThread.ManagedThreadId}");
Console.WriteLine($"主线程{Thread.CurrentThread.ManagedThreadId}:{SayAsync().Status}");
Console.WriteLine($"主线程{Thread.CurrentThread.ManagedThreadId}");
Console.ReadKey(true);
}
public static async Task<string> SayAsync()
{
Console.WriteLine($"主线程{Thread.CurrentThread.ManagedThreadId}_SayAsync");
var s = await Task<string>.Run(async () =>
{
await Task.Delay(TimeSpan.FromSeconds(2));
Console.WriteLine("---------------两秒后----------------");
Console.WriteLine($"子线程{Thread.CurrentThread.ManagedThreadId}");
return "vvull";
});
Console.WriteLine($"子线程{Thread.CurrentThread.ManagedThreadId}:{s}");
Console.WriteLine($"子线程{Thread.CurrentThread.ManagedThreadId}");
return s;
}
}
阻塞主线程
class Program
{
static void Main(string[] args)
{
Console.WriteLine($"主线程{Thread.CurrentThread.ManagedThreadId}");
Console.WriteLine($"主线程{Thread.CurrentThread.ManagedThreadId}:{SayAsync().Result}");//阻塞主线程
Console.WriteLine($"主线程{Thread.CurrentThread.ManagedThreadId}");
Console.ReadKey(true);
}
public static async Task<string> SayAsync()
{
Console.WriteLine($"主线程{Thread.CurrentThread.ManagedThreadId}_SayAsync");
var s = await Task<string>.Run(async () =>
{
await Task.Delay(TimeSpan.FromSeconds(2));
Console.WriteLine("---------------两秒后----------------");
Console.WriteLine($"子线程{Thread.CurrentThread.ManagedThreadId}");
return "vvull";
});
Console.WriteLine($"子线程{Thread.CurrentThread.ManagedThreadId}:{s}");
Console.WriteLine($"子线程{Thread.CurrentThread.ManagedThreadId}");
return s;
}
}
进阶版
class Program
{
static void Main(string[] args)
{
Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}->主线程");
giaoAsync();
Thread.Sleep(3000);
Console.WriteLine("---------------三秒后----------------");
Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}->主线程");
Console.ReadKey();
}
static async Task giaoAsync()
{
Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}->giao");
//Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}->giao:{await SayAsync()}");//本句以后由新线程接管
//Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}->giao*");
var s = await SayAsync();//由新线接管
Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}->giao**_{s}");
Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}->giao**");
var ss = await SayAsync();//由新线接管
Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}->giao***_{ss}");
Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}->giao***");
}
public static async Task<string> SayAsync()
{
Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}->SayAsync");
var s = await Task<string>.Run(async () =>
{
Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}->SayAsync*");
await Task.Delay(TimeSpan.FromSeconds(0.001));
Console.WriteLine("---------------两秒后----------------");
Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}->SayAsync**");
return "vvull";
});
Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}->SayAsync**_{s}");
Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}->SayAsync**");
return s;
}
}
以上是关于使用Async和Await的异步编程的主要内容,如果未能解决你的问题,请参考以下文章
C#的多线程——使用async和await来完成异步编程(Asynchronous Programming with async and await)