string _Resource1; // 共用資源. public void Run() { Console.WriteLine($"Task6.Run(), ThreadId={Thread.CurrentThread.ManagedThreadId}"); _Resource1 = "共用資源初始化"; State1 myPara1 = new State1 { _Value1 = "參數初始化" }; _CTS1 = new CancellationTokenSource(); // 需每次重建. CancellationToken myToken = _CTS1.Token; // (CancellationToken 取消權杖)比共用資源更安全、更有效率控制工作是否取消. Task <string> task1 = Task.Factory.StartNew <string>(myFunction1, myPara1, myToken); // 交付系統工作工廠排班執行 Function(參數, 取消權杖). Console.WriteLine("若提早按Escape鍵, 就會取消子執行緒/工作繼續執行, 並看到'共用資源使用中'. 因子執行緒/工作需要5秒才能完成."); Console.WriteLine("Press any key to continuing..."); ConsoleKeyInfo key1 = Console.ReadKey(); if (key1.Key == ConsoleKey.Escape) { _CTS1.Cancel(); } Console.WriteLine($"_Resource1={_Resource1}."); // 共用資源結果. Console.WriteLine($"myPara1._Value1={myPara1._Value1}."); // 參數處理結果. Console.WriteLine($"task1.Result={task1.Result}."); // Task 處理結果. Console.WriteLine("Press any key for continuing..."); Console.ReadKey(); }
private string myFunction1(object object1) { Console.WriteLine($"myFunction1(), ThreadId={Thread.CurrentThread.ManagedThreadId}."); Console.WriteLine($"Task.CurrentId={Task.CurrentId}."); State1 c1 = object1 as State1; // 接收輸入參數 _Resource1 = "共用資源使用中"; c1._Value1 = "參數使用中"; for (int i = 0; i < 5; i++) { if (_CTS1.IsCancellationRequested) { _Resource1 = "取消執行"; Console.WriteLine($"_Resource1={_Resource1}."); // 共用資源結果. return("取消執行."); } Thread.Sleep(1000); } c1._Value1 = "參數處理完成"; _Resource1 = "共用資源處理完成"; return("工作處理完成"); // Task 也可以回傳值. }
private void myCompleted(object sender, RunWorkerCompletedEventArgs e) { Console.WriteLine($"myCompleted() ThreadId={Thread.CurrentThread.ManagedThreadId}"); State1 c1 = e.Result as State1; // 接收參數處理結果 c1._Value1 = "參數處理完成"; _Resource1 = "共用資源處理完成"; }
private void myDoWork(object sender, DoWorkEventArgs e) { Console.WriteLine($"myDoWork(), ThreadId={Thread.CurrentThread.ManagedThreadId}"); State1 c1 = e.Argument as State1; // 接收參數 _Resource1 = "共用資源使用中"; c1._Value1 = "參數使用中"; Thread.Sleep(5000); e.Result = c1; // 存入參數處理結果. }
private void myAction1(object object1) { Console.WriteLine($"myAction1(), ThreadId={Thread.CurrentThread.ManagedThreadId}."); State1 c1 = object1 as State1; // 接收輸入參數 _Resource1 = "共用資源使用中"; c1._Value1 = "參數使用中"; Thread.Sleep(5000); c1._Value1 = "參數處理完成"; _Resource1 = "共用資源處理完成"; }
private string myFunction1(object object1) { Console.WriteLine($"myFunction1(), ThreadId={Thread.CurrentThread.ManagedThreadId}."); Console.WriteLine($"Task.CurrentId={Task.CurrentId}."); State1 c1 = object1 as State1; // 接收輸入參數 _Resource1 = "共用資源使用中"; c1._Value1 = "參數使用中"; Thread.Sleep(5000); c1._Value1 = "參數處理完成"; _Resource1 = "共用資源處理完成"; return("工作處理完成"); // Task 也可以回傳值. }
string _Resource1; // 共用資源. public void Run() { Console.WriteLine($"Thread5.Run(), ThreadId={Thread.CurrentThread.ManagedThreadId}"); _Resource1 = "共用資源初始化"; State1 myPara1 = new State1 { _Value1 = "參數初始化" }; ThreadPool.QueueUserWorkItem(new WaitCallback(myAction1), myPara1); // 交付系統佇列排班執行 Action(參數). Console.WriteLine("若提早按鍵, 就會看到'共用資源使用中'. 因子執行緒/工作需要5秒才能完成."); Console.WriteLine("Press any key to continuing..."); Console.ReadKey(); Console.WriteLine($"_Resource1={_Resource1}."); // 共用資源結果. Console.WriteLine($"myPara1._Value1={myPara1._Value1}."); // 參數處理結果. Console.WriteLine("Press any key for continuing..."); Console.ReadKey(); }
string _Resource1; // 共用資源. public void Run() { Console.WriteLine($"Thread3.Run(), ThreadId={Thread.CurrentThread.ManagedThreadId}"); _Resource1 = "共用資源初始化"; State1 myPara1 = new State1 { _Value1 = "參數初始化" }; ParameterizedThreadStart threadDelegate = new ParameterizedThreadStart(myAction1); // 建立包含傳入參數的委派物件. Thread thread1 = new Thread(threadDelegate); thread1.Start(myPara1); // ParameterizedThreadStart(Action).Start() 啟動子執行緒, 執行有參數傳入的 Action. thread1.Join(); // 封鎖主執行緒, 等候子執行緒結束. Console.WriteLine($"_Resource1={_Resource1}."); // 共用資源結果. Console.WriteLine($"myPara1._Value1={myPara1._Value1}."); // 參數處理結果. Console.WriteLine("Press any key for continuing..."); Console.ReadKey(); }
string _Resource1; // 共用資源. public void Run() { Console.WriteLine($"Task3.Run(), ThreadId={Thread.CurrentThread.ManagedThreadId}"); _Resource1 = "共用資源初始化"; State1 myPara1 = new State1 { _Value1 = "參數初始化" }; Task <string> task1 = new Task <string>(myFunction1, myPara1); // 建立工作並傳入參數. task1.Start(); // Task(Function, 參數).Start() 啟動執行(有參數傳入且有回傳值)的 Function. task1.Wait(); // 封鎖主執行緒, 等候工作結束. Console.WriteLine($"_Resource1={_Resource1}."); // 共用資源結果. Console.WriteLine($"myPara1._Value1={myPara1._Value1}."); // 參數處理結果. Console.WriteLine($"task1.Result={task1.Result}."); // Task 處理結果. Console.WriteLine("Press any key for continuing..."); Console.ReadKey(); }
string _Resource1; // 共用資源. public void Run() { Console.WriteLine($"Task2.Run(), ThreadId={Thread.CurrentThread.ManagedThreadId}"); _Resource1 = "共用資源初始化"; State1 myPara1 = new State1 { _Value1 = "參數初始化" }; Task task1 = new Task(myAction1, myPara1); // 建立工作並傳入參數. task1.Start(); // Task(Action, 參數).Start() 啟動執行有參數傳入的 Action Console.WriteLine("若提早按鍵, 就會看到'共用資源使用中'. 因子執行緒/工作需要5秒才能完成."); Console.WriteLine("Press any key to continuing..."); Console.ReadKey(); Console.WriteLine($"_Resource1={_Resource1}."); // 共用資源結果. Console.WriteLine($"myPara1._Value1={myPara1._Value1}."); // 參數處理結果. Console.WriteLine("Press any key for continuing..."); Console.ReadKey(); }
string _Resource1; // 共用資源. public void Run() { Console.WriteLine($"Task5.Run(), ThreadId={Thread.CurrentThread.ManagedThreadId}"); _Resource1 = "共用資源初始化"; State1 myPara1 = new State1 { _Value1 = "參數初始化" }; Task <string> task1 = Task.Factory.StartNew <string>(myFunction1, myPara1); // 交付系統工作工廠排班執行 Function(參數). Console.WriteLine("若提早按鍵, 就會看到'共用資源使用中'. 因子執行緒/工作需要5秒才能完成."); Console.WriteLine("Press any key to continuing..."); Console.ReadKey(); Console.WriteLine($"_Resource1={_Resource1}."); // 共用資源結果. Console.WriteLine($"myPara1._Value1={myPara1._Value1}."); // 參數處理結果. Console.WriteLine($"task1.Result={task1.Result}."); // Task 處理結果. Console.WriteLine("Press any key for continuing..."); Console.ReadKey(); }
string _Resource1; // 共用資源. public void Run() { Console.WriteLine($"Thread2.Run(), ThreadId={Thread.CurrentThread.ManagedThreadId}."); _Resource1 = "共用資源初始化"; State1 myPara1 = new State1 { _Value1 = "參數初始化" }; ParameterizedThreadStart threadDelegate = new ParameterizedThreadStart(myAction1); // 建立包含傳入參數的委派物件. Thread thread1 = new Thread(threadDelegate); // 建立執行緒. thread1.Start(myPara1); // ParameterizedThreadStart(Action).Start(Parameter) 啟動子執行緒, 執行有參數傳入的 Action. Console.WriteLine("若提早按鍵, 就會看到'共用資源使用中'. 因子執行緒/工作需要5秒才能完成."); Console.WriteLine("Press any key to continuing..."); Console.ReadKey(); Console.WriteLine($"_Resource1={_Resource1}."); // 共用資源結果. Console.WriteLine($"myPara1._Value1={myPara1._Value1}."); // 參數處理結果. Console.WriteLine("Press any key for continuing..."); Console.ReadKey(); }
string _Resource1; // 共用資源. public void Run() { Console.WriteLine($"Thread4.Run(), ThreadId={Thread.CurrentThread.ManagedThreadId}"); _Resource1 = "共用資源初始化"; State1 myPara1 = new State1 { _Value1 = "參數初始化" }; BackgroundWorker bkWorker = new BackgroundWorker(); // 建立背景工作 BackgroundWorker. bkWorker.DoWork += new DoWorkEventHandler(myDoWork); // 指定啟動時回呼函數. bkWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(myCompleted); // 指定完成時回呼函數. bkWorker.RunWorkerAsync(myPara1); // 啟動背景工作, 等候(啟動或完成)事件通知執行子執行緒. Console.WriteLine("若提早按鍵, 就會看到'共用資源使用中'. 因子執行緒/工作需要5秒才能完成."); Console.WriteLine("Press any key to continuing..."); Console.ReadKey(); Console.WriteLine($"_Resource1={_Resource1}."); // 共用資源結果. Console.WriteLine($"myPara1._Value1={myPara1._Value1}."); // 參數處理結果. Console.WriteLine("Press any key for continuing..."); Console.ReadKey(); }
private void myAction1(object object1) { Console.WriteLine($"myAction1(), ThreadId={Thread.CurrentThread.ManagedThreadId}."); State1 c1 = object1 as State1; // 接收輸入參數 _Resource1 = "共用資源使用中"; c1._Value1 = "參數使用中"; for (int i = 0; i < 5; i++) { if (_Cancel) { _Resource1 = "取消執行"; Console.WriteLine($"_Resource1={_Resource1}."); // 共用資源結果. return; } Thread.Sleep(1000); } c1._Value1 = "參數處理完成"; _Resource1 = "共用資源處理完成"; }
Boolean _Cancel; // 共用資源, 是否取消執行? public void Run() { Console.WriteLine($"Thread6.Run(), ThreadId={Thread.CurrentThread.ManagedThreadId}"); _Resource1 = "共用資源初始化"; State1 myPara1 = new State1 { _Value1 = "參數初始化" }; _Cancel = false; ThreadPool.QueueUserWorkItem(new WaitCallback(myAction1), myPara1); // 交付系統佇列排班執行 Action(參數). Console.WriteLine("若提早按Escape鍵, 就會取消子執行緒/工作繼續執行, 並看到'共用資源使用中'. 因子執行緒/工作需要5秒才能完成."); Console.WriteLine("Press any key to continuing..."); ConsoleKeyInfo key1 = Console.ReadKey(); if (key1.Key == ConsoleKey.Escape) { _Cancel = true; } Console.WriteLine($"_Resource1={_Resource1}."); // 共用資源結果. Console.WriteLine($"myPara1._Value1={myPara1._Value1}."); // 參數處理結果. Console.WriteLine("Press any key for continuing..."); Console.ReadKey(); }