如何判断任务是否“观察到”? [英] How to tell if Task has been "observed"?

查看:123
本文介绍了如何判断任务是否“观察到”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是此问题的后续行动。我也读过Stephen Toub的任务和未处理的异常,我想我了解任务和异常如何工作以及观察到的任务的意思。 然而,我不知道如何判断是否已经观察到任务是否可以使用反射?

This is a follow-up to this question. I've also read Stephen Toub's "Tasks and Unhandled Exceptions" and I think I understand how tasks and exceptions work and what "observed task" means. I however cannot figure out how to tell if a Task has been observed or not. Is that possible at all without using reflection?

我想借用 @ Noseratio的代码作为示例: p>

I'd like to borrow @Noseratio's code as an example:

static async void Observe(Task task)
{        
    await task; 
}

// ...

var taskObserved = false;
var task = DoSomething()
try
{
    bool ready = await DoSomethingElse();
    if (!ready) 
      return null;

    var value = await DoThirdThing(); // depends on DoSomethingElse
    taskObserved = true;
    return value + await task;
 }
 finally
 {
     if (!taskObserved)
        Observe(task);
 }

如果我们可以判断是否遵守了任务,这可以简化并且更可读:

If we could tell whether the task had been observed, this could be made simpler and more readable:

static async void Observe(Task task)
{        
    if (!task.WasObserved) 
        await task; 
}

// ...

var task = DoSomething()
try
{
    bool ready = await DoSomethingElse();
    if (!ready) 
      return null;

    var value = await DoThirdThing(); // depends on DoSomethingElse
    return value + await task;
}
finally
{
    Observe(task);
}


推荐答案

任务不知道他们是否已经被期待已久这有点像是否知道一个整数是否被添加到另一个整数。

Tasks have no idea whether they've been awaited or not. It's kind of like asking if an integer knows if it's been added to another integer or not.

但是,可以观察到任务的异常这个例外情况是否实际上被这个任务所记住。区分不可观察的例外和未经授权的任务很重要。运行时具有一些不可观察的任务异常的特殊逻辑,但是对于未被任务的任务没有任何特殊的处理。

However, the task's exception can be observed, and whether the exception has been observed is in fact remembered by the task. It's important to distinguish between unobserved exceptions and unawaited tasks. The runtime has some special logic for unobserved task exceptions, but does not do anything special for unawaited tasks.

你真的不应该编写代码,这取决于任务是否已经期待已久的。如果 DoSomething 的语义是,即使结果被忽略(一个非常奇怪但技术上有效的要求),它总是应该等待,那么这段代码就足够了: / p>

You really should not write code that depends on whether a task has been awaited. If the semantics for DoSomething are that it always should be awaited even if the result is ignored (a very odd - but technically valid - requirement), then this code should suffice:

var task = DoSomething();
try
{
  bool ready = await DoSomethingElse();
  if (!ready) 
    return null;

  var value = await DoThirdThing(); // depends on DoSomethingElse
  return value + await task;
}
finally
{
  await task;
}

另一方面,如果 DoSomething的语义是,如果结果完全不需要,这个任务可以忽略(这种情况的可能性更大),那么这个代码就足够了:

On the other hand, if the semantics of DoSomething are that the task can be ignored if the result isn't needed after all (which is far more likely the case), then this code should suffice:

var task = DoSomething();
bool ready = await DoSomethingElse();
if (!ready) 
  return null;

var value = await DoThirdThing(); // depends on DoSomethingElse
return value + await task;

不用担心是否等待任务。

No need to mess around with worrying about whether a task has been awaited.

这篇关于如何判断任务是否“观察到”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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