static void EasyTask()
{
// 执行一个无返回值的任务
Task.Run(() =>
{
Console.WriteLine("runing...");
});
// 执行一个返回 int 类型结果的任务
Task.Run<int>(() =>
{
return new Random().Next();
});
// 声明一个任务,仅声明,不执行
Task t = new Task(() =>
{
Console.WriteLine("");
});
}
static void Factory()
{
List<Task<int>> tasks = new List<Task<int>>();
TaskFactory factory = new TaskFactory();
tasks.Add(factory.StartNew<int>(() =>
{
return 1;
}));
tasks.Add(factory.StartNew<int>(() =>
{
return 2;
}));
foreach (var t in tasks)
{
Console.WriteLine("Task:{0}", t.Result);
}
}