本文介绍了Task.Start(),Task.Run()和Task.Factory.StartNew关于使用()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只看到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加入到帮助的异步日益频繁的使用和卸载工作到线程池

It was added in .Net 4.5 to help with the increasingly frequent usage of async and offloading work to the ThreadPool.

Task.Factory.StartNew (加入TPL在.NET 4.0中)被更强大。当你想使用你应该只使用它,如果 Task.Run 是远远不够的,例如 TaskCreationOptions.LongRunning (虽然这是没有必要的时候代表是异步更多的在我的博客:)。更多关于 Task.Factory.StartNew 中的

Task.Factory.StartNew (added with TPL in .Net 4.0) is much more robust. You should only use it if Task.Run isn't enough, for example when you want to use TaskCreationOptions.LongRunning (though it's unnecessary when the delegate is async. More on that on my blog: LongRunning Is Useless For Task.Run With async-await). More on Task.Factory.StartNew in Task.Run vs 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.

这篇关于Task.Start(),Task.Run()和Task.Factory.StartNew关于使用()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 09:07