测试代码:
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 + " "); 62 63 string r2 = await t2; 64 65 textBox1.AppendText(r2 + " "); 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 + " "); 89 textBox1.AppendText(t2.Result + " "); 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 + " "); 106 107 var r2 = await DoWorkByTask("测试值2"); 108 109 textBox1.AppendText(r2 + " "); 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 + " "); 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 + " "); 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") + " 秒 "; if (txt.InvokeRequired) { txt.Invoke(new Action(() => { txt.AppendText(msg); })); } else { txt.AppendText(msg); } } } }