并行任务对于树形结构 [英] Parallel Task For Tree structure

查看:244
本文介绍了并行任务对于树形结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有一个这样的structiure

HI i have a structiure like this.

Parent-----Children1
|
-------Children2
         |
          -------Children2.1
         |
         --------Children2.2



我怎样才能循环到这个树,是比完成
Children1和Children2并行whene时执行新的任务
父则是完成然后
,则Children2.1和儿童也2.2平行。这样执行的顺序是一样的曲线图。
对不起我的英文不好

推荐答案

使用的 Parallel.Foreach 对所有孩子的正在运行的任务,然后在最终调用所有孩子的subchild:

use Parallel.Foreach for running task on all childs, and then in the end call all childs subchild:

void run()
{
   do action on parent;
   DoTasks(parent);
}

void DoTasks(Node node)
{
  if (node.Childs == null) return;

  Parallel.Foreach(node.Childs, child => {do action})

  foreach(var child in node.Childs)
  {
    DoTasks(child);
  }
}



parallel.foreach等待,直到完成所有任务。

parallel.foreach waits untill all tasks finished.

这篇关于并行任务对于树形结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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