Async Await 多线程等待 应用
作者:互联网
不同框架的应用
///1.Winform--存在特殊处理
///2.ASP.NETCore---放心用
///3.控制台---放心用
///4.WPF----没试过---
///5.Core WebApi---放心用
Winform
/// <summary> ///异步方法: 正常执行 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void btnAsync_Click(object sender, EventArgs e) { Debug.WriteLine($"**********************************btnAsync_Click******************************************"); Debug.WriteLine($"This is btnAsync_Click Start,ThreadId={Thread.CurrentThread.ManagedThreadId}"); long lResult = await this.CalculationAsync(1_000_000); Debug.WriteLine($"This is btnAsync_Click End,ThreadId={Thread.CurrentThread.ManagedThreadId}"); this.textAsyncResult.Text = lResult.ToString(); //这句话必须要主线程来执行的 //更改控件的值,这里必须是(UI线程)主线程去执行;每次执行都能成功,说明每次这里都是主线程来执行的;跟Winform设计有关系;在Winform中,await后面的内容,都会让主线程来执行; } private async Task<long> CalculationAsync(long total) { var task = await Task.Run(() => { Debug.WriteLine($"This is CalculationAsync Start,ThreadId={Thread.CurrentThread.ManagedThreadId}"); long lResult = 0; for (int i = 0; i < total; i++) { lResult += i; } Debug.WriteLine($"This is CalculationAsync End,ThreadId={Thread.CurrentThread.ManagedThreadId}"); return lResult; }); return task; //这句话必须由主线程来执行,线程在同一时刻只能做一件事儿 }
/// <summary> /// 同步方法:界面卡死了 新加一个线程变成三个线程等待 就不会死锁了 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnSync_Click(object sender, EventArgs e) { Debug.WriteLine($"**********************************btnSync_Click******************************************"); Debug.WriteLine($"This is btnSync_Click Start,ThreadId={Thread.CurrentThread.ManagedThreadId}"); //var task = this.CalculationAsync(1_000_000); ////task.Wait(); //主线程阻塞等待 ////1.主线程要等待 Winform特殊设计:await后面的内容必须由主线程执行; ////2.主线程在这儿也等待着在 ////3.主线程无暇分身导致死锁 ////4.怎么解决这个死锁? //long lResult = task.Result;//主线程阻塞等待,主线程在等结果,经过分析可以确定,界面卡死问题肯定是出在这儿 long lResult = this.GetCalculationAsync(1_000_000); Debug.WriteLine($"This is btnSync_Click End,ThreadId={Thread.CurrentThread.ManagedThreadId}"); this.textSync.Text = lResult.ToString(); } private long GetCalculationAsync(long total) { var taskLong = Task.Run(() => { //新开一个线程 var task = this.CalculationAsync(total); long lResult = task.Result;//子线程 return lResult; }); return taskLong.Result;//主线程在等Result }
private void btnSync_Click(object sender, EventArgs e) { Debug.WriteLine($"**********************************btnSync_Click******************************************"); Debug.WriteLine($"This is btnSync_Click Start,ThreadId={Thread.CurrentThread.ManagedThreadId}"); var task = this.CalculationAsync(1_000_000); //task.Wait(); //主线程阻塞等待 ////1.主线程要等待 Winform特殊设计:await后面的内容必须由主线程执行; ////2.主线程在这儿也等待着在 ////3.主线程无暇分身导致死锁 ////4.怎么解决这个死锁? long lResult = task.Result;//主线程阻塞等待,主线程在等结果,经过分析可以确定,界面卡死问题肯定是出在这儿 ------等待---相互等待锁死 // long lResult = this.GetCalculationAsync(1_000_000); windform 中await后面必须由主线程完成 Debug.WriteLine($"This is btnSync_Click End,ThreadId={Thread.CurrentThread.ManagedThreadId}"); this.textSync.Text = lResult.ToString(); } private async Task<long> CalculationAsync(long total) { var task = await Task.Run(() => { Debug.WriteLine($"This is CalculationAsync Start,ThreadId={Thread.CurrentThread.ManagedThreadId}"); long lResult = 0; for (int i = 0; i < total; i++) { lResult += i; } Debug.WriteLine($"This is CalculationAsync End,ThreadId={Thread.CurrentThread.ManagedThreadId}"); return lResult; }); return task; //这句话必须由主线程来执行,线程在同一时刻只能做一件事儿 -----(主线程在等待)---- }
标签:Await,主线,long,Debug,WriteLine,lResult,Async,多线程,Click 来源: https://www.cnblogs.com/soulice/p/15650127.html