父任务不会等待子任务完成 [英] Parent task does not wait for child task to complete
问题描述
因此,这里是我的code
任务< INT [] GT;父= Task.Run(()=>
{
变种结果=新INT [3];
TaskFactory TF =新TaskFactory(TaskCreationOptions.AttachedToParent,TaskContinuationOptions.ExecuteSynchronously);
的for(int i = 0; I< result.Length;我++)
{
INT J =;
tf.StartNew(()=>结果[J] = getRandomNumber的(J));
}
返回结果;
});
VAR finalTask = parent.ContinueWith(parentTask =>
{
的foreach(在parentTask.Result变种I)
{
Console.WriteLine(ⅰ);
}
});
finalTask.Wait();
基本上我创建3 工作
■哪些是工作
称为父(我猜)的儿童。我excpect父任务等待所有三个子任务来完成。在此之后, finalTask
将等待,因为 finalTask.Wait(执行);
此statemant。但事情并没有发生预期。我的意思是在应用程序退出之前,任何 getRandomNumber的
的要求完成。的code。在这里有些部分是从书里面说,这些家长的任务应该等待子任务来完成,这显然是没有发生复制。我失去了一些东西呢?
这是什么getRandomNumber的呢
公共静态INT getRandomNumber的(INT二)
{
随机兰特=新的随机();
的for(int i = 0; I< 10亿;我++){} //模仿一些工作
返回rand.Next(1000);
}
这code做同样的事情
任务< INT [] GT;父= Task.Run(()=>
{
变种结果=新INT [3];
的for(int i = 0; I< result.Length;我++)
{
INT J =;
新的任务(()=>结果[J] = getRandomNumber的(J),TaskCreationOptions.AttachedToParent)。开始();
}
返回结果;
});
此行为是由于您使用的 Task.Run
的方法,即禁止儿童工作被附加到父:
在
运行
法是一种简单的替代方式的<一个href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskfactory.startnew%28v=vs.110%29.aspx"><$c$c>StartNew$c$c>方法。它创建了一个任务[其] <一个href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.task.creationoptions%28v=vs.110%29.aspx"><$c$c>CreationOptions$c$c>属性值是<一个href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskcreationoptions%28v=vs.110%29.aspx"><$c$c>TaskCreationOptions.DenyChildAttach$c$c>.
要解决,只需从
改变你的第一线 任务&LT; INT [] GT;父= Task.Run(()=&GT;
到
任务&LT; INT [] GT;父= Task.Factory.StartNew(()=&GT;
So here is my code
Task<int[]> parent = Task.Run(() =>
{
var result = new int[3];
TaskFactory tf = new TaskFactory(TaskCreationOptions.AttachedToParent, TaskContinuationOptions.ExecuteSynchronously);
for (int i = 0; i < result.Length; i++)
{
int j = i;
tf.StartNew(() => result[j] = GetRandomNumber(j));
}
return result;
});
var finalTask = parent.ContinueWith(parentTask =>
{
foreach (var i in parentTask.Result)
{
Console.WriteLine(i);
}
});
finalTask.Wait();
Basically I create 3 Task
s which are children of Task
called parent (I guess). I excpect parent task to wait for all three child tasks to complete. After that the finalTask
would be waited to execute because of finalTask.Wait();
this statemant. But things do not happen as expected. I mean the application exits before the any of GetRandomNumber
calls finishes. Some part of the code in here is copied from book which stated that those parent task should wait for child task to complete which apparently is not happening. Am I missing something here ?
this is what GetRandomNumber does
public static int GetRandomNumber(int ii)
{
Random rand = new Random();
for (int i = 0; i < 1000000000; i++) { } // imitate some jobs
return rand.Next(1000);
}
This code does the same thing
Task<int[]> parent = Task.Run(() =>
{
var result = new int[3];
for (int i = 0; i < result.Length; i++)
{
int j = i;
new Task(() => result[j] = GetRandomNumber(j), TaskCreationOptions.AttachedToParent).Start();
}
return result;
});
This behaviour is due to your use of the Task.Run
method, which forbids child tasks from being attached to the parent:
The
Run
method is a simpler alternative to theStartNew
method. It creates a task [whose]CreationOptions
property value isTaskCreationOptions.DenyChildAttach
.
To resolve, simply change your first line from
Task<int[]> parent = Task.Run(() =>
to
Task<int[]> parent = Task.Factory.StartNew(() =>
这篇关于父任务不会等待子任务完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!