Task class example
一、Task class example - 任務無需取得結果
using System;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Action<object> action = (object obj) =>
{
Console.WriteLine("obj = {0}, Task.CurrentId = {1}, Thread.CurrentThread.ManagedThreadId = {2}",
obj, Task.CurrentId,
Thread.CurrentThread.ManagedThreadId);
};
// .NET framework 4.0 開始提供 Task 類別。
Task alpha = new Task(action, "alpha");
alpha.Start();
//---------------------------------------------------------------
Task beta = new Task(
(object obj) =>
{
Console.WriteLine("obj = {0}, Task.CurrentId = {1}, Thread.CurrentThread.ManagedThreadId = {2}",
obj,
Task.CurrentId,
Thread.CurrentThread.ManagedThreadId);
}, "beta");
beta.Start();
//---------------------------------------------------------------
// 已不需要固定呼叫 Start(); 了
Task delta = Task.Factory.StartNew(action, "delta");
//---------------------------------------------------------------
// .NET framework 4.5 的 Task 類別新增了靜態方法 Run。
// 已不需要固定呼叫 Start(); 了
Task gamma = Task.Run(() =>
{
Console.WriteLine("obj = {0}, Task.CurrentId = {1}, Thread.CurrentThread.ManagedThreadId = {2}",
"gamma",
Task.CurrentId, Thread.CurrentThread.ManagedThreadId);
});
Console.ReadKey();
}
}
}
使用 Task 類別有三種寫法
1、Task.Start();
2、Task.Factory.StartNew
3、Task.Run(() =>{});
二、Task<TResult> - 任務需要取得結果
1、使用 Task.Result 屬性來取得函式執行結果
a、Task<string>.Run(() =>{}); 型式
using System;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Task<string> t = Task<string>.Run(() =>
{
return "test";
});
Console.WriteLine(t.Result);
Console.ReadKey();
}
}
}
b、Task.Start 型式
using System;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string mission = "Landing Mars";
Task<string> task = new Task<string>(execute, mission); // 建立工作。
task.Start(); // 起始工作。
task.Wait(); // 等待工作。
// 取得結果。
Console.WriteLine(task.Result);
Console.ReadKey();
}
static string execute(object state)
{
return (string)state + " was mission complete!";
}
}
}
c、使用 GetAwaiter、GetResult 方法
using System;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Task<string> task = Task.Run(() =>
{
Console.WriteLine("processing...");
Thread.Sleep(2000);
return "complete";
});
System.Runtime.CompilerServices.TaskAwaiter<string> awaiter = task.GetAwaiter();
awaiter.OnCompleted(() =>
{
string result = awaiter.GetResult();
Console.WriteLine("the result is " + result);
});
Console.ReadKey();
}
}
}
三、其他範例
1、示範 IsCompleted 屬性與 Wait 方法的使用
using System;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Task task = Task.Run(() =>
{
Console.WriteLine("processing...");
Thread.Sleep(2000);
});
Console.WriteLine("task is complete? " + task.IsCompleted);
task.Wait();
Console.WriteLine("task is complete? " + task.IsCompleted);
Console.ReadKey();
}
}
}
IsCompleted 屬性用來判斷 Task 的委派方法是否已經執行完畢。(不管結果是否會跳出 exception)
Wait 方法用來確保 Task 的委派方法會執行完畢。
參考資料: