我刚看到3个关于TPL使用的例程,它们执行相同的工作; 这是代码:
public static void Main()
{
Thread.CurrentThread.Name = "Main";
// Create a task and supply a user delegate by using a lambda expression.
Task taskA = new Task( () => Console.WriteLine("Hello from taskA."));
// Start the task.
taskA.Start();
// Output a message from the calling thread.
Console.WriteLine("Hello from thread '{0}'.",
Thread.CurrentThread.Name);
taskA.Wait();
}
public static void Main()
{
Thread.CurrentThread.Name = "Main";
// Define and run the task.
Task taskA = Task.Run( () => Console.WriteLine("Hello from taskA."));
// Output a message from the calling thread.
Console.WriteLine("Hello from thread '{0}'.",
Thread.CurrentThread.Name);
taskA.Wait();
}
public static void Main()
{
Thread.CurrentThread.Name = "Main";
// Better: Create and start the task in one operation.
Task taskA = Task.Factory.StartNew(() => Console.WriteLine("Hello from taskA."));
// Output a message from the calling thread.
Console.WriteLine("Hello from thread '{0}'.",
Thread.CurrentThread.Name);
taskA.Wait();
}
我只是不明白为什么MS给出了三种不同的方式在运行第三方物流工作,因为他们所有的工作一样的:Task.Start(),Task.Run()和Task.Factory.StartNew()。
告诉我,是Task.Start(),Task.Run()并且Task.Factory.StartNew()所有用于同一目的或者它们有不同的意义吗?
应该Task.Start()何时使用,何时应该使用Task.Run()何时使用Task.Factory.StartNew()?
请帮助我通过示例非常详细地了解它们的实际用法,谢谢。
Qyouu