我刚刚看到了三个关于第三方物流使用的程序,它们做同样的工作;以下是代码:
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();
}
我就是不明白为什么微软在第三方物流中提供了3种不同的方式来运行作业(job),因为它们的工作方式都是一样的:Task.Start()
、Task.Run()
和Task.Factory.StartNew()
.
告诉我,Task.Start()
、Task.Run()
和Task.Factory.StartNew()
是同一用途,还是意义不同?
什么时候用Task.Start()
,什么时候用Task.Run()
,什么时候用Task.Factory.StartNew()
?
请帮助我了解他们的实际使用情况,根据场景非常详细的例子,谢谢.