-
C#教程之一个简单的例子看明白 async await Task
本站最新发布 C#从入门到精通
试听地址 https://www.xin3721.com/eschool/CSharpxin3721/
试听地址 https://www.xin3721.com/eschool/CSharpxin3721/
测试代码:

1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading; 9 using System.Threading.Tasks; 10 using System.Windows.Forms; 11 using Utils; 12 13 namespace test 14 { 15 public partial class Form1 : Form 16 { 17 public Form1() 18 { 19 InitializeComponent(); 20 } 21 22 private void Form1_Load(object sender, EventArgs e) 23 { 24 25 } 26 27 /// <summary> 28 /// 执行任务 29 /// </summary> 30 public string DoWork(string str) 31 { 32 Thread.Sleep(3000); //模拟延迟 比如网络请求 用了3000毫秒 33 return "输出:" + str; 34 } 35 36 /// <summary> 37 /// 执行任务 38 /// </summary> 39 public Task<string> DoWorkByTask(string str) 40 { 41 return Task.Run(() => 42 { 43 Thread.Sleep(3000); //模拟延迟 比如网络请求 用了3000毫秒 44 return "输出:" + str; 45 }); 46 } 47 48 /// <summary> 49 /// 测试1 50 /// </summary> 51 private async void button1_Click(object sender, EventArgs e) 52 { 53 LogTimeUtil logTime = new LogTimeUtil(); 54 55 var t1 = DoWorkByTask("测试值1"); 56 57 var t2 = DoWorkByTask("测试值2"); 58 59 string r1 = await t1; 60 61 textBox1.AppendText(r1 + "\r\n"); 62 63 string r2 = await t2; 64 65 textBox1.AppendText(r2 + "\r\n"); 66 67 logTime.LogTime("耗时", textBox1); 68 } 69 70 /// <summary> 71 /// 测试2 72 /// 73 /// 等效于 测试1 74 /// </summary> 75 private void button2_Click(object sender, EventArgs e) 76 { 77 Task.Run(() => //如果不加Task.Run,界面会卡 78 { 79 LogTimeUtil logTime = new LogTimeUtil(); 80 81 Task<string> t1 = Task.Run<string>(() => { return DoWork("测试值1"); }); 82 Task<string> t2 = Task.Run<string>(() => { return DoWork("测试值2"); }); 83 84 Task.WaitAll(t1, t2); 85 86 this.Invoke(new Action(() => //线程中修改控件数据要使用Invoke 87 { 88 textBox1.AppendText(t1.Result + "\r\n"); 89 textBox1.AppendText(t2.Result + "\r\n"); 90 })); 91 92 logTime.LogTime("耗时", textBox1); 93 }); 94 } 95 96 /// <summary> 97 /// 测试3 98 /// </summary> 99 private async void button3_Click(object sender, EventArgs e) 100 { 101 LogTimeUtil logTime = new LogTimeUtil(); 102 103 var r1 = await DoWorkByTask("测试值1"); 104 105 textBox1.AppendText(r1 + "\r\n"); 106 107 var r2 = await DoWorkByTask("测试值2"); 108 109 textBox1.AppendText(r2 + "\r\n"); 110 111 logTime.LogTime("耗时", textBox1); 112 } 113 114 /// <summary> 115 /// 测试4 116 /// 117 /// 等效于 测试3 118 /// </summary> 119 private void button4_Click(object sender, EventArgs e) 120 { 121 Task.Run(() => //如果不加Task.Run,界面会卡 122 { 123 LogTimeUtil logTime = new LogTimeUtil(); 124 125 Task<string> t1 = Task.Run<string>(() => { return DoWork("测试值1"); }); 126 t1.Wait(); 127 this.Invoke(new Action(() => //线程中修改控件数据要使用Invoke 128 { 129 textBox1.AppendText(t1.Result + "\r\n"); 130 })); 131 132 Task<string> t2 = Task.Run<string>(() => { return DoWork("测试值2"); }); 133 t2.Wait(); 134 this.Invoke(new Action(() => //线程中修改控件数据要使用Invoke 135 { 136 textBox1.AppendText(t2.Result + "\r\n"); 137 })); 138 139 logTime.LogTime("耗时", textBox1); 140 }); 141 } 142 143 } 144 }
测试输出:
附 LogTimeUtil.cs:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Utils { /// <summary> /// 记录耗时 /// </summary> public class LogTimeUtil { private DateTime _lastTime; public LogTimeUtil() { _lastTime = DateTime.Now; } /// <summary> /// 记录耗时 /// </summary> public void LogTime(string msg) { double d = DateTime.Now.Subtract(_lastTime).TotalSeconds; LogUtil.Log(msg + ",耗时:" + d.ToString("0.000") + " 秒"); } /// <summary> /// 记录耗时 /// </summary> public void LogTime(string msg, TextBox txt) { double d = DateTime.Now.Subtract(_lastTime).TotalSeconds; msg = msg + ",耗时:" + d.ToString("0.000") + " 秒\r\n"; if (txt.InvokeRequired) { txt.Invoke(new Action(() => { txt.AppendText(msg); })); } else { txt.AppendText(msg); } } } }
栏目列表
最新更新
Winform中怎样跨窗体获取另一窗体的控件对
Winform中使用FastReport的PictureObject时通过代
三分钟掌握,使用Quqrtz.Net实现定时发送邮
NET/Regex 处理连续空格
QR 码详解(下)
C#中的等值判断1
C#编写了一个基于Lucene.Net的搜索引擎查询
使用FastReport报表工具生成报表PDF文档
基于JieBaNet+Lucene.Net实现全文搜索
C#取视频某一帧图片
.Net Standard(.Net Core)实现获取配置信息
Linux PXE + Kickstart 自动装机
Shell 编程 基础
Shell 编程 条件语句
CentOS8-网卡配置及详解
Linux中LVM逻辑卷管理
1.数码相框-相框框架分析(1)
Ubuntu armhf 版本国内源
Linux中raid磁盘阵列
搭建简易网站
Dubbo(五):深入理解Dubbo核心模型Invok
vfp教程之VFP与Excel交互编程
vfp教程之在VFP中实现跟变式组合框及椭圆
SQL SERVER查询数据库所有表的大小,按照记
使用 SQL 服务器时,"评估期已过期"错
sql server无法连接本地服务器
使用sql语句创建表
VB操作Access数据库小记 ————————
access数据库远程连接
java web操作Access数据库