有什么方法可以使用ContinueWith任务启动任务吗? [英] Is there any way to start task using ContinueWith task?

查看:103
本文介绍了有什么方法可以使用ContinueWith任务启动任务吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码:

var r = from x in new Task<int>(() => 1)
        from y in new Task<int>(() => x + 1) 
        select y;
r.ContinueWith(x => Console.WriteLine(x.Result)).Start();   

new Task<int>(() => 1)
    .ContinueWith(x => x.Result + 1)
    .ContinueWith(x => Console.WriteLine(x.Result))
    .Start();

例外:

可能不会在继续任务上调用开始.

Start may not be called on a continuation task.

所以我需要开始第一个任务.有什么方法可以调用上一个任务的Start方法来运行所有任务?

So I need to start the first task. Is there any way to call last task Start method to run all tasks?

推荐答案

任何不使用Task.Factory.StartNew

Any reason not to use Task.Factory.StartNewmsdn, ms docs for the first task? Yes, it's inconsistent - but it's fundamentally a different kind of task, in terms of being started explicitly rather than just as a continuation.

这篇关于有什么方法可以使用ContinueWith任务启动任务吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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