一、async、await用法的作用
async用法主要是用来作为修饰符将方法作为异步方法使用,await关键字只用作为在异步方法才能使用,也就是只有当方法有async修饰后,才能在方法中使用await,await后跟Task新的任务启动。(await和async是配对使用)
二、async、await用法实例
第一个关键点:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace 线程test1005 { public delegate void test(string s1); class Program { static void Main(string[] args) { test(); for (int i = 0; i < 400; i++) { Console.Write(0); } Console.Read(); } public async static void test() { for (int i = 0; i < 300; i++) { Console.Write(1); } await Task.Run(() => { for (int i = 0; i < 300; i++) { Console.Write(2); } }); for (int i = 0; i < 300; i++) { Console.Write(3); } } } }
await的核心作用:如上述代码当中,我们在test函数中使用Task.Run开启新线程后,主线程和支线程交替执行,也就是输出结果为1,然后2和3交替输出,最后输出0,只是在函数内部开启支线程,并没有跳出函数。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace 线程test1005 { public delegate void test(string s1); class Program { static void Main(string[] args) { test(); for (int i = 0; i < 1000; i++) { Console.Write(0); } Console.WriteLine("0的支线程号为" + Thread.CurrentThread.ManagedThreadId); Console.Read(); } public async static void test() { for (int i = 0; i < 300; i++) { Console.Write(1); } Console.WriteLine("1的支线程号为" + Thread.CurrentThread.ManagedThreadId); Task.Run(() => { for (int i = 0; i < 300; i++) { Console.Write(2); } Console.WriteLine("2的支线程号为" + Thread.CurrentThread.ManagedThreadId); }); for (int i = 0; i < 300; i++) { Console.Write(3); } Console.WriteLine("3的支线程号为" + Thread.CurrentThread.ManagedThreadId); } } }
运行结果为:
在添加await后,当输出2的时候,会跳出test函数,2和0交替输出,等待Task.run运行完,再继续执行函数后的内容,输出结果也就是1,2和0交替输出,最后再输出3,如果0的数量比较多,则0和3也会交替输出!
总结:test中的代码在await以前的代码都是在主函数的线程中输出,但是在await以后的代码会在Task.Run的线程中运行。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace 线程test1005 { public delegate void test(string s1); class Program { static void Main(string[] args) { test(); for (int i = 0; i < 1000; i++) { Console.Write(0); } Console.WriteLine("0的支线程号为" + Thread.CurrentThread.ManagedThreadId); Console.Read(); } public async static void test() { for (int i = 0; i < 300; i++) { Console.Write(1); } Console.WriteLine("1的支线程号为" + Thread.CurrentThread.ManagedThreadId); await Task.Run(() => { for (int i = 0; i < 300; i++) { Console.Write(2); } Console.WriteLine("2的支线程号为" + Thread.CurrentThread.ManagedThreadId); }); for (int i = 0; i < 300; i++) { Console.Write(3); } Console.WriteLine("3的支线程号为" + Thread.CurrentThread.ManagedThreadId); } } }
运行结果如图:
第二个关键点:
await会返回一个结果,返回的结果类型是Task类型,当我们主函数需要该结果的result的时候,我们的代码会等待函数全部完成后才会继续执行。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace 线程test1005 { public delegate void test(string s1); class Program { static void Main(string[] args) { var tt=test(); for (int i = 0; i < 1000; i++) { Console.Write(0); } Console.WriteLine("0的支线程号为" + Thread.CurrentThread.ManagedThreadId); Console.WriteLine(tt.Result); for (int i = 0; i < 1000; i++) { Console.Write(8); } Console.Read(); } public async static Task<int> test() { for (int i = 0; i < 300; i++) { Console.Write(1); } Console.WriteLine("1的支线程号为" + Thread.CurrentThread.ManagedThreadId); var result =await Task<int>.Run(() => { for (int i = 0; i < 300; i++) { Console.Write(2); } Console.WriteLine("2的支线程号为" + Thread.CurrentThread.ManagedThreadId); return 10; }); for (int i = 0; i < 300; i++) { Console.Write(3); } Console.WriteLine("3的支线程号为" + Thread.CurrentThread.ManagedThreadId); return result; } } }
上述代码中当主函数中需要tt.result时,主函数代码会等待test函数运行完返回result结果后再继续执行后续代码。
运行结果为:
提示:async修饰的函数类型只有三种,void类型、Task和Task!!!
Task.Run的单独返回类型为内部运行函数的返回值T,但await修饰后的返回值为Task