2

我正在尝试在循环中实现嵌套任务 - 这是我目前的模式,但我不确定,因为这是我第一次使用并行任务库。

父(层)任务应等待子(节点)任务完成。

    public int NestedTask(IEnumerable<MatchTier> tierNodes)
    {
        var tier = Task<int>.Factory.StartNew(() =>
        {
            Task<int> node = null;

            foreach(var n in tierNodes)
            {
                node = Task<int>.Factory.StartNew(() =>
                {
                    // Task logic goes here

                    return 1; // temp placeholder
                });

                // if a valid value is returned then exit this loop
            }

            return node.Result;

        });

        return tier.Result;
    }

子节点循环直到返回第一个有效值,然后应该退出循环,将有效值传递给父节点。

子节点和父节点都需要超时。每个子节点将被允许运行大约 3 秒,之后该进程将超时并询问下一个节点。

父级的总超时值约为 15 - 20 秒,在此之后,如果没有收到有效响应,它也应该终止。

这看起来合乎逻辑吗?

4

2 回答 2

2

等待任务完成

node.Wait();

等待任务完成,直到一些滴答声完成

node.Wait(timeToWait);

或等待他们全部完成

Task.WaitAll(tasks);

你应该在这里阅读更多信息

于 2013-08-27T08:28:49.780 回答
1

如前所述task.Wait()task.Result(等待并获取结果)Task.WaitAll(theTaskCollection)是执行此操作的方法。我已经稍微改变了你的实现来解决这个问题,但我非常不确定你真正想要返回什么。我删除了外部任务,因为它似乎不需要。

public int NestedTask(IEnumerable<MatchTier> tierNodes)
{
  var tasks = tierNodes.Select(node => Task<int>.Factory.StartNew(() =>
            {
                // Task logic goes here
                return 1; // temp placeholder
            })).ToList(); // Enumerate to start tasks, not doing this would case WaitAll to start them one at a time (i believe)

  if (!Task.WaitAll(tasks, timeOutInMilliseconds))
    // Handle timeout...

  return tasks.First().Result; // Is this what you want?
}

编辑:添加修改后的解决方案。

public int NestedTask(IEnumerable<string> tierNodes)
{
  int parentTimeout = 15 * 1000;
  int childTimeout = 3 * 1000;

  var tier = Task<int>.Factory.StartNew(() =>
  {
      foreach (var n in tierNodes)
      {
          var node = Task<int>.Factory.StartNew(() =>
          {
              // Task logic goes here
              return 1;
          });

          // If we get the result we return it, else we wait
          if (node.Wait(childTimeout))
              return node.Result;
      }
      throw new Exception("did not get result!");
  });

  if (!tier.Wait(parentTimeout))
  {
      // The child will continue on running though.
      throw new TimeoutException("parent timed out");
  }
  else if (tier.Exception != null)
  {
      // We have an error
      throw tier.Exception.InnerException;
  }

  return tier.Result;
}
于 2013-08-27T08:46:17.977 回答