TaskTEST.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Diagnostics;
  7. using System.Drawing;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14. namespace TestProject
  15. {
  16. public partial class TaskTEST : Form
  17. {
  18. public TaskTEST()
  19. {
  20. InitializeComponent();
  21. }
  22. private Stopwatch stopWatch = new Stopwatch();
  23. public void ParallelInvokeMethod1()
  24. {
  25. Parallel.For(0, 100, item =>
  26. {
  27. DoWork(item);
  28. });
  29. }
  30. private void DoWork(int item)
  31. {
  32. }
  33. public void ParallelBreak()
  34. {
  35. ConcurrentBag<int> bag = new ConcurrentBag<int>();
  36. stopWatch.Start();
  37. Parallel.For(0, 5000, (i, state) =>
  38. {
  39. if (bag.Count == 3000)
  40. {
  41. state.Stop();
  42. return;
  43. }
  44. bag.Add(i);
  45. });
  46. stopWatch.Stop();
  47. Console.WriteLine("Bag count is " + bag.Count + ", " + stopWatch.ElapsedMilliseconds);
  48. }
  49. public void Run1()
  50. {
  51. Thread.Sleep(2000);
  52. Console.WriteLine("Task 1 is cost 2 sec");
  53. throw new Exception("Exception in task 1");
  54. }
  55. public void Run2()
  56. {
  57. Thread.Sleep(3000);
  58. Console.WriteLine("Task 2 is cost 3 sec");
  59. throw new Exception("Exception in task 2");
  60. }
  61. public void ParallelInvokeMethod()
  62. {
  63. stopWatch.Start();
  64. try
  65. {
  66. Parallel.Invoke(Run1, Run2);
  67. }
  68. catch (AggregateException aex)
  69. {
  70. foreach (var ex in aex.InnerExceptions)
  71. {
  72. Console.WriteLine(ex.Message);
  73. }
  74. }
  75. stopWatch.Stop();
  76. Console.WriteLine("Parallel run " + stopWatch.ElapsedMilliseconds + " ms.");
  77. stopWatch.Reset();
  78. stopWatch.Start();
  79. try
  80. {
  81. Run1();
  82. Run2();
  83. }
  84. catch (Exception ex)
  85. {
  86. Console.WriteLine(ex.Message);
  87. }
  88. stopWatch.Stop();
  89. Console.WriteLine("Normal run " + stopWatch.ElapsedMilliseconds + " ms.");
  90. }
  91. private void Form3_Load(object sender, EventArgs e)
  92. {
  93. //var task1 = new Task(() =>
  94. //{
  95. // Console.WriteLine("Begin");
  96. // Thread.Sleep(2000);
  97. // Console.WriteLine("Finish");
  98. //});
  99. //Console.WriteLine("Before start:" + task1.Status);
  100. //task1.Start();
  101. //Console.WriteLine("After start:" + task1.Status);
  102. //task1.Wait();
  103. //Console.WriteLine("After Finish:" + task1.Status);
  104. //Console.Read();
  105. //var task1 = new System.Threading.Tasks.Task(() =>
  106. //{
  107. // Console.WriteLine("Task 1 Begin");
  108. // Thread.Sleep(2000);
  109. // Console.WriteLine("Task 1 Finish");
  110. //});
  111. //var task2 = new System.Threading.Tasks.Task(() =>
  112. //{
  113. // Console.WriteLine("Task 2 Begin");
  114. // Thread.Sleep(3000);
  115. // Console.WriteLine("Task 2 Finish");
  116. //});
  117. //task1.Start();
  118. //task2.Start();
  119. //var result = task1.ContinueWith(task =>
  120. //{
  121. // Console.WriteLine("task1 finished!");
  122. // return "This is task result!";
  123. //});
  124. //Console.WriteLine(result.Result.ToString());
  125. //TaskFactory tf = new TaskFactory();
  126. //var SendFeedBackTask = tf.StartNew(() => { Console.WriteLine("Get some Data!"); })
  127. // .ContinueWith(s => { return false; })
  128. // .ContinueWith<string>(r =>
  129. // {
  130. // if (r.Result)
  131. // {
  132. // return "Finished";
  133. // }
  134. // else
  135. // {
  136. // return "Error";
  137. // }
  138. // });
  139. //Console.WriteLine(SendFeedBackTask.Result);
  140. //线程取消
  141. var tokenSource = new CancellationTokenSource();
  142. var token = tokenSource.Token;
  143. var task = new TaskFactory().StartNew(() =>
  144. {
  145. for (var i = 0; i < 1000; i++)
  146. {
  147. Thread.Sleep(1000);
  148. if (token.IsCancellationRequested)
  149. {
  150. Console.WriteLine("Abort mission success!");
  151. return;
  152. }
  153. Console.WriteLine("TASK");
  154. }
  155. }, token);
  156. token.Register(() =>
  157. {
  158. Console.WriteLine("Canceled");
  159. });
  160. Console.WriteLine("Press enter to cancel task...");
  161. Thread.Sleep(10000);
  162. tokenSource.Cancel();
  163. //关联子进程
  164. //var pTask = new TaskFactory().StartNew(() =>
  165. //{
  166. // var cTask = new TaskFactory().StartNew(() =>
  167. // {
  168. // Thread.Sleep(2000);
  169. // Console.WriteLine("Childen task finished!");
  170. // });
  171. // Console.WriteLine("Parent task finished!");
  172. //});
  173. //pTask.Wait();
  174. //Console.WriteLine("Flag");
  175. //Console.Read();
  176. //加入关联关系,父Task会等子Task完成后才继续执行
  177. //var pTask = new TaskFactory().StartNew(() =>
  178. //{
  179. // var cTask = new TaskFactory().StartNew(() =>
  180. // {
  181. // Console.WriteLine("Childen task finished!");
  182. // }, TaskCreationOptions.AttachedToParent);
  183. // Thread.Sleep(2000);
  184. // Console.WriteLine("Parent task finished!");
  185. //});
  186. //pTask.Wait();
  187. //System.Threading.Tasks.Task.WaitAll();
  188. //Console.WriteLine("Flag");
  189. }
  190. private void button1_Click(object sender, EventArgs e)
  191. {
  192. }
  193. private void TaskTEST_Load(object sender, EventArgs e)
  194. {
  195. DataTable dt = new DataTable();
  196. OpenCSVFile(ref dt, @"C:\Users\callm\Desktop\品网\2019_04_04_19_59_18_788.csv");
  197. dataGridView1.DataSource = dt;
  198. }
  199. public bool OpenCSVFile(ref DataTable mycsvdt, string filepath)
  200. {
  201. string strpath = filepath; //csv文件的路径
  202.             try
  203. {
  204. int intColCount = 0;
  205. bool blnFlag = true;
  206. DataColumn mydc;
  207. DataRow mydr;
  208. string strline;
  209. string[] aryline;
  210. StreamReader mysr = new StreamReader(strpath, System.Text.Encoding.Default);
  211. while ((strline = mysr.ReadLine()) != null)
  212. {
  213. aryline = strline.Split(new char[] { ',' });
  214. //给datatable加上列名
  215. if (blnFlag)
  216. {
  217. blnFlag = false;
  218. intColCount = aryline.Length;
  219. int col = 0;
  220. for (int i = 0; i < aryline.Length; i++)
  221. {
  222. col = i + 1;
  223. mydc = new DataColumn(col.ToString());
  224. mycsvdt.Columns.Add(mydc);
  225. }
  226. }
  227. //填充数据并加入到datatable中
  228. mydr = mycsvdt.NewRow();
  229. for (int i = 0; i < intColCount; i++)
  230. {
  231. mydr[i] = aryline[i];
  232. }
  233. if (mydr[0].ToString() != "")
  234. mycsvdt.Rows.Add(mydr);
  235. }
  236. return true;
  237. }
  238. catch (Exception e)
  239. {
  240. return false;
  241. }
  242. }
  243. }
  244. }