Task.ContinueWith()对2的任务? [英] Task.ContinueWith() vs 2 Tasks?

查看:241
本文介绍了Task.ContinueWith()对2的任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我应该用

 工作任务1 = Task.Factory.StartNew(()=> {...})
                 .ContinueWith(蚂蚁=> Console.Write(2));
 

VS

 工作任务1 = Task.Factory.StartNew(()=> {...});
任务任务2 = task1.ContinueWith(蚂蚁=> Console.Write(2));
 

解决方案

它的意思是相同的,除了你就会有一个关于第二个任务吧。如果第一个任务需要一定的处理执行的任务都在一起才可以使用第二个选项。一个例子是添加另一个 VAR任务3 = task1.ContinueWith()这样的任务二,三将同时执行,但只有第一个任务做处理。其实这应该是:

 任务TASK2 = Task.Factory.StartNew(()=> {...})ContinueWith。(蚂蚁=> Console.Write(2));

任务任务1 = Task.Factory.StartNew(()=> {...});
任务任务2 = task1.ContinueWith(蚂蚁=> Console.Write(2));
 

请注意我替换任务1 TASK2 。无论是启动任务将导致任务1 率先启动。

When should I use

Task task1 = Task.Factory.StartNew (() => {...})
                 .ContinueWith (ant => Console.Write ("2"));

vs

Task task1 = Task.Factory.StartNew (() => {... });
Task task2 = task1.ContinueWith (ant => Console.Write ("2"));

解决方案

It means the same, except for you'll have a reference to the second task now. You can use the second option if the first task needs some processing before executing the tasks all together. An example is to add another var task3 = task1.ContinueWith() so task two and three will execute concurrently, but only if the first task is done processing. Actually it should be:

Task task2 = Task.Factory.StartNew (() => {...}).ContinueWith (ant => Console.Write ("2"));

Task task1 = Task.Factory.StartNew (() => {... });
Task task2 = task1.ContinueWith (ant => Console.Write ("2"));

Note I replaced task1 to task2. Starting either Task will cause task1 to start first.

这篇关于Task.ContinueWith()对2的任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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