关于Task.Start(),Task.Run()和Task.Factory.StartNew(的使用)TPLRun、Start、Task、TPL

2023-09-03 12:15:34 作者:九辞.

我只看到3例行关于TPL使用它做同样的工作。

I just saw 3 routine regarding TPL usage which does same job

这里是code

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()全部用于相同目的或它们具有不同的意义?

Tell me, are Task.Start(), Task.Run() and Task.Factory.StartNew() all used for the same purpose or they have different significance?

当一个人应该使用 Task.Start(),当 Task.Run()以及何时应该使用 Task.Factory.StartNew()

When one should use Task.Start(), when Task.Run() and when one should use Task.Factory.StartNew()?

请帮我了解他们的实际使用为每个方案非常细致结合实例,谢谢!

Please help me to understand their real usage as per scenario in great detail with examples, thanks.

推荐答案

Task.Run 是一个缩写 Task.Factory.StartNew 与特定的安全参数:

Task.Run is a shorthand for Task.Factory.StartNew with specific safe arguments:

Task.Factory.StartNew(
    action, 
    CancellationToken.None, 
    TaskCreationOptions.DenyChildAttach, 
    TaskScheduler.Default);

据以.Net 4.5加入到帮助的日益频繁使用异步和卸载工作到线程池

Task.Factory.StartNew (与TPL加在.NET 4.0中)被更加灵活。您应该只使用它,如果 Task.Run 是不够的(例如,你要传递 TaskCreationOptions.LongRunning )。在 Task.Run更多VS Task.Factory.StartNew

Task.Factory.StartNew (added with TPL in .Net 4.0) is much more versatile. You should only use it if Task.Run isn't enough (for example you want to pass on TaskCreationOptions.LongRunning). More in Task.Run vs Task.Factory.StartNew

永远不要创建一个工作并调用开始()除非你找到一个非常好的理由不这样做所以。它应该只使用,如果你有一些部分需要创建任务,但没有安排他们和另一部分的时间表,而无需创建。这是几乎从来没有一个合适的解决方案,可能是危险的。更多Task.Factory.StartNew与新任务(...) 。开始

Don't ever create a Task and call Start() unless you find an extremely good reason to do so. It should only be used if you have some part that needs to create tasks but not schedule them and another part that schedules without creating. That's almost never an appropriate solution and could be dangerous. More in "Task.Factory.StartNew" vs "new Task(...).Start"

总之,大多使用 Task.Run ,使用 Task.Factory.StartNew 如果你一定要,从不使用启动

In conclusion, mostly use Task.Run, use Task.Factory.StartNew if you must and never use Start.