using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace TestProject { public partial class TaskTEST : Form { public TaskTEST() { InitializeComponent(); } private Stopwatch stopWatch = new Stopwatch(); public void ParallelInvokeMethod1() { Parallel.For(0, 100, item => { DoWork(item); }); } private void DoWork(int item) { } public void ParallelBreak() { ConcurrentBag bag = new ConcurrentBag(); stopWatch.Start(); Parallel.For(0, 5000, (i, state) => { if (bag.Count == 3000) { state.Stop(); return; } bag.Add(i); }); stopWatch.Stop(); Console.WriteLine("Bag count is " + bag.Count + ", " + stopWatch.ElapsedMilliseconds); } public void Run1() { Thread.Sleep(2000); Console.WriteLine("Task 1 is cost 2 sec"); throw new Exception("Exception in task 1"); } public void Run2() { Thread.Sleep(3000); Console.WriteLine("Task 2 is cost 3 sec"); throw new Exception("Exception in task 2"); } public void ParallelInvokeMethod() { stopWatch.Start(); try { Parallel.Invoke(Run1, Run2); } catch (AggregateException aex) { foreach (var ex in aex.InnerExceptions) { Console.WriteLine(ex.Message); } } stopWatch.Stop(); Console.WriteLine("Parallel run " + stopWatch.ElapsedMilliseconds + " ms."); stopWatch.Reset(); stopWatch.Start(); try { Run1(); Run2(); } catch (Exception ex) { Console.WriteLine(ex.Message); } stopWatch.Stop(); Console.WriteLine("Normal run " + stopWatch.ElapsedMilliseconds + " ms."); } private void Form3_Load(object sender, EventArgs e) { //var task1 = new Task(() => //{ // Console.WriteLine("Begin"); // Thread.Sleep(2000); // Console.WriteLine("Finish"); //}); //Console.WriteLine("Before start:" + task1.Status); //task1.Start(); //Console.WriteLine("After start:" + task1.Status); //task1.Wait(); //Console.WriteLine("After Finish:" + task1.Status); //Console.Read(); //var task1 = new System.Threading.Tasks.Task(() => //{ // Console.WriteLine("Task 1 Begin"); // Thread.Sleep(2000); // Console.WriteLine("Task 1 Finish"); //}); //var task2 = new System.Threading.Tasks.Task(() => //{ // Console.WriteLine("Task 2 Begin"); // Thread.Sleep(3000); // Console.WriteLine("Task 2 Finish"); //}); //task1.Start(); //task2.Start(); //var result = task1.ContinueWith(task => //{ // Console.WriteLine("task1 finished!"); // return "This is task result!"; //}); //Console.WriteLine(result.Result.ToString()); //TaskFactory tf = new TaskFactory(); //var SendFeedBackTask = tf.StartNew(() => { Console.WriteLine("Get some Data!"); }) // .ContinueWith(s => { return false; }) // .ContinueWith(r => // { // if (r.Result) // { // return "Finished"; // } // else // { // return "Error"; // } // }); //Console.WriteLine(SendFeedBackTask.Result); //线程取消 var tokenSource = new CancellationTokenSource(); var token = tokenSource.Token; var task = new TaskFactory().StartNew(() => { for (var i = 0; i < 1000; i++) { Thread.Sleep(1000); if (token.IsCancellationRequested) { Console.WriteLine("Abort mission success!"); return; } Console.WriteLine("TASK"); } }, token); token.Register(() => { Console.WriteLine("Canceled"); }); Console.WriteLine("Press enter to cancel task..."); Thread.Sleep(10000); tokenSource.Cancel(); //关联子进程 //var pTask = new TaskFactory().StartNew(() => //{ // var cTask = new TaskFactory().StartNew(() => // { // Thread.Sleep(2000); // Console.WriteLine("Childen task finished!"); // }); // Console.WriteLine("Parent task finished!"); //}); //pTask.Wait(); //Console.WriteLine("Flag"); //Console.Read(); //加入关联关系,父Task会等子Task完成后才继续执行 //var pTask = new TaskFactory().StartNew(() => //{ // var cTask = new TaskFactory().StartNew(() => // { // Console.WriteLine("Childen task finished!"); // }, TaskCreationOptions.AttachedToParent); // Thread.Sleep(2000); // Console.WriteLine("Parent task finished!"); //}); //pTask.Wait(); //System.Threading.Tasks.Task.WaitAll(); //Console.WriteLine("Flag"); } private void button1_Click(object sender, EventArgs e) { } private void TaskTEST_Load(object sender, EventArgs e) { DataTable dt = new DataTable(); OpenCSVFile(ref dt, @"C:\Users\callm\Desktop\品网\2019_04_04_19_59_18_788.csv"); dataGridView1.DataSource = dt; } public bool OpenCSVFile(ref DataTable mycsvdt, string filepath) { string strpath = filepath; //csv文件的路径             try { int intColCount = 0; bool blnFlag = true; DataColumn mydc; DataRow mydr; string strline; string[] aryline; StreamReader mysr = new StreamReader(strpath, System.Text.Encoding.Default); while ((strline = mysr.ReadLine()) != null) { aryline = strline.Split(new char[] { ',' }); //给datatable加上列名 if (blnFlag) { blnFlag = false; intColCount = aryline.Length; int col = 0; for (int i = 0; i < aryline.Length; i++) { col = i + 1; mydc = new DataColumn(col.ToString()); mycsvdt.Columns.Add(mydc); } } //填充数据并加入到datatable中 mydr = mycsvdt.NewRow(); for (int i = 0; i < intColCount; i++) { mydr[i] = aryline[i]; } if (mydr[0].ToString() != "") mycsvdt.Rows.Add(mydr); } return true; } catch (Exception e) { return false; } } } }