WPF 异步执行 Action<> Func<> https://www.cnblogs.com/vipsoft/p/4535037.html
代码如下
using System; using System.Threading; using System.Threading.Tasks; namespace Demo { class Program { static void Main(string[] args) { Console.WriteLine($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")} Main 111"); Task.Run(() => Post<bool>("http://www.vipsoft.com.cn","VipSoft")); Console.WriteLine($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")} Main 222"); Console.ReadLine(); } public static async Task<bool> Post<T>(string url, string param) { Console.WriteLine($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")} Post 111"); bool flag = await SendMessage<T>(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); if (flag != null) { Console.WriteLine($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")} 等 SendMessage 执行完"); } Console.WriteLine($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")} Post 222"); return flag; } public static async Task<bool> SendMessage<T>(string param) { Console.WriteLine($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")} SendMessage 111"); Thread.Sleep(2000); Console.WriteLine($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")} SendMessage 222"); return true; } } }