谁取消了我的任务是什么? [英] Who canceled my Task?

查看:117
本文介绍了谁取消了我的任务是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的C#的任务正变得取消,但不是我。我没有得到一个堆栈跟踪,并在那里发生的问题,我想不通。

我的任务调用看起来是这样的:

 变种T =任务<布尔> .Factory.StartNew(()=>
    {
        布尔BOK = DoSomthingImportant();
        返回BOK;
    },TaskCreationOptions.AttachedToParent)
    .ContinueWith<布尔>((theTask)=>
    {
        VAR原因= theTask.IsCanceled? 它被取消:故故障;
        的Debug.WriteLine(Error:任务结束,因为+理由+。);
        ...记录异常到我的对象之一...
        返回false;
    },TaskContinuationOptions.NotOnRanToCompletion);
 

我要继续任务运行,如果该任务发生故障或者被取消了,但如果它跑好了。 永远不会执行的延续。

后来我的程序捕捉到一个AggregateException其包装一TaskCanceledException。

我和我的任务等重大互动是调用了WaitAny(taskArray,超时),直到我没有更多的任务开始,然后WaitAll调用,没有超时,直到最后一个任务就完成了。

可以用一个超时了WaitAny导致取消?为什么没有我的延续被调用?

这只是我第二次刷任务库,让我无言以对。

更新:

我发现这太问题:如何将一个任务的取消状态传播到后续任务。 在我的code以上一个错误(但撤消,而不是原因)是我认为的延续任务的状态是一样的原始任务的状态。事实上,你必须做一些工作,以获得对方的任何一个,因为其他职位描述。

更新2:

布莱恩:谢谢你的文件建立参考。我已经搜查高和低的任务的备用原因被取消,但错过了这句话:

  

如果你正在等待一个任务是过渡到取消状态,   任务(包装在AggregateException)制造和抛出。   请注意,此异常指示成功取消,而不是   出现故障的情况。因此,该任务的异常属性返回   空。

解决方案

您还在等待继续和由于原任务运行,以完成后续任务被取消。这种行为是覆盖在的文档

My C# Task is getting canceled, but not by me. I don't get a stacktrace and I can't figure out where the problem occurs.

My task invocation looks like this:

var t = Task<Boolean>.Factory.StartNew(() =>
    {
        Boolean bOk = DoSomthingImportant();
        return bOk;
    }, TaskCreationOptions.AttachedToParent)
    .ContinueWith<Boolean>((theTask) =>
    {
        var reason = theTask.IsCanceled ? "it was canceled" : "it faulted";
        Debug.WriteLine("Error: Task ended because " + reason + ".");
        ... log the exception to one of my objects...
        return false;
    }, TaskContinuationOptions.NotOnRanToCompletion);

I want the continuation task to run if the task faulted or was canceled, but not if it ran okay. The continuation is never executed.

Later on my program catches an AggregateException which is wrapping a TaskCanceledException.

My other major interaction with my tasks is to call WaitAny(taskArray, timeout) until I have no more tasks to start, then call WaitAll with no timeout until the last task is done.

Could WaitAny with a timeout cause a cancellation? Why didn't my continuation get called?

This is only my second brush with the Task library, so I am clueless.

UPDATE:

I found this SO question: How to propagate a Task's Canceled status to a continuation task. One error in my code above (but not the cause of the Cancelation) is that I assumed that the Continuation tasks status was the same as the original task's status. In fact you have to do some work to get the one from the other, as the other post describes.

UPDATE 2:

Brian: Thanks for the documentaion reference. I had searched high and low for alternate causes of a Task being canceled, but missed these words:

"If you are waiting on a Task that transitions to the Canceled state, a Task (wrapped in an AggregateException) is manufactured and thrown. Note that this exception indicates successful cancellation instead of a faulty situation. Therefore, the Task's Exception property returns null."

解决方案

You're waiting on the continuation and since the original task ran to completion the continuation task was cancelled. This behavior is covered in the documentation.

这篇关于谁取消了我的任务是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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