c# 多執行緒(task,thread)
阿新 • • 發佈:2019-01-08
1.task(委託)
Task t = new Task(() => { //do something while (true) { } }); t.ContinueWith((task) => { try { task.Wait(); } catch (AggregateException ex) { foreach (Exception item in ex.InnerExceptions) { } } },TaskContinuationOptions.OnlyOnFaulted); t.Start();
2.Thread (可不加阻滯狀態)
//WaitHandle(EventWaitHandle:AutoResetEvent,ManualResetEvent;Semaphore;Mutex) //給出阻滯狀態。給出訊號後自己回到阻滯 private AutoResetEvent autoResetEvent = new AutoResetEvent(false); public void Test1() { Thread TH = new Thread(()=> { //DO SOMETHING bool re=autoResetEvent.WaitOne(); //CONTINUE DOING }); } public void Test2() { //給等待的執行緒一個訊號 autoResetEvent.Set(); } #endregion