关于 Task.Start() , Task.Run() 和 Task.Factory.StartNew() 的使用 [英] Regarding usage of Task.Start() , Task.Run() and Task.Factory.StartNew()

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

问题描述

我刚刚看到了 3 个关于 TPL 使用的例程,它们可以完成相同的工作;这是代码:

I just saw 3 routines regarding TPL usage which do the same job; here is the 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 给出了 3 种不同的方式来在 TPL 中运行作业,因为它们的工作方式都相同:Task.Start()Task.Run()Task.Factory.StartNew().

I just do not understand why MS gives 3 different ways to run jobs in TPL because they all work the same: Task.Start(), Task.Run() and 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 do they have different significance?

什么时候应该使用Task.Start(),什么时候应该使用Task.Run(),什么时候应该使用Task.Factory.StartNew()?

When should one use Task.Start(), when should one use Task.Run() and when should one 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 中添加的,以帮助越来越频繁地使用 async 并将工作卸载到 ThreadPool.

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(在 .Net 4.0 中添加了 TPL)更加健壮.你应该只在 Task.Run 不够用时才使用它,例如当你想使用 TaskCreationOptions.LongRunning 时(尽管当委托是异步的时候它是不必要的.更多关于在我的博客上:LongRunning 对 Task.Run 无用 async-等待).在 Task.Factory.StartNew>Task.Run 与 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

永远不要创建Task 并调用Start(),除非你找到了一个非常好的理由这样做.仅当您有一部分需要创建任务但不安排它们而另一部分需要安排而不创建时才应使用它.这几乎从来都不是一个合适的解决方案,而且可能很危险.Task.Factory.StartNew"与new"中的更多信息任务(...).开始"

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,不要使用Start.

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆