123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- 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<int> bag = new ConcurrentBag<int>();
- 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<string>(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;
- }
- }
- }
- }
|