为什么带有 ContinueWith 选项的第二个线程不等待第一个线程的结束?
Why second thread with ContinueWith option is not waiting for first thread's end?
为什么第二个 thead 由行开始
var finalTask = parent.ContinueWith( .. )
不是在等待第一个线程的结束吗?这是证明新线程没有等待第一个结束的打印输出:
Starting main thread 1
Starting ContinueWith thread 2
0 0 0
Ending ContinueWith thread 2
ContinueWith thread is
finished!
Starting child thread 3
Ends child thread 3
Starting child thread 2
Ends child thread 2
Starting child thread 1
Ends child thread 1
代码:
public static void AttachTasksToMain()
{
Task<Int32[]> parent = Task.Run(() =>
{
Console.WriteLine("Starting main thread 1");
var results = new Int32[3];
new Task(() => {
Console.WriteLine("Starting child thread 1");
results[0] = 0;
Console.WriteLine("Ends child thread 1");
}, TaskCreationOptions.AttachedToParent).Start();
new Task(() => {
Console.WriteLine("Starting child thread 2");
results[1] = 1;
Console.WriteLine("Ends child thread 2");
}, TaskCreationOptions.AttachedToParent).Start();
new Task(() => {
Console.WriteLine("Starting child thread 3");
results[2] = 2;
Console.WriteLine("Ends child thread 3");
}, TaskCreationOptions.AttachedToParent).Start();
//this thread finishes when all child-thread are finished!
return results;
});
//parent.Wait();
var finalTask = parent.ContinueWith(parentTask =>
{
Console.WriteLine("Starting ContinueWith thread 2");
foreach (int i in parentTask.Result)
Console.WriteLine(i);
Console.WriteLine("Ending ContinueWith thread 2");
});
finalTask.Wait();
Console.WriteLine("ContinueWith thread is finished!");
}
问题是Task.Run
。这将以 TaskCreationOptions.DenyChildAttach
.
开始任务
使用 Task.Factory.StartNew
的简单更改,问题就解决了。那是因为这里默认的是TaskCreationOptions.None
.
See here 深入讨论差异。
为什么第二个 thead 由行开始
var finalTask = parent.ContinueWith( .. )
不是在等待第一个线程的结束吗?这是证明新线程没有等待第一个结束的打印输出:
Starting main thread 1
Starting ContinueWith thread 2
0 0 0
Ending ContinueWith thread 2
ContinueWith thread is finished!
Starting child thread 3
Ends child thread 3
Starting child thread 2
Ends child thread 2
Starting child thread 1
Ends child thread 1
代码:
public static void AttachTasksToMain()
{
Task<Int32[]> parent = Task.Run(() =>
{
Console.WriteLine("Starting main thread 1");
var results = new Int32[3];
new Task(() => {
Console.WriteLine("Starting child thread 1");
results[0] = 0;
Console.WriteLine("Ends child thread 1");
}, TaskCreationOptions.AttachedToParent).Start();
new Task(() => {
Console.WriteLine("Starting child thread 2");
results[1] = 1;
Console.WriteLine("Ends child thread 2");
}, TaskCreationOptions.AttachedToParent).Start();
new Task(() => {
Console.WriteLine("Starting child thread 3");
results[2] = 2;
Console.WriteLine("Ends child thread 3");
}, TaskCreationOptions.AttachedToParent).Start();
//this thread finishes when all child-thread are finished!
return results;
});
//parent.Wait();
var finalTask = parent.ContinueWith(parentTask =>
{
Console.WriteLine("Starting ContinueWith thread 2");
foreach (int i in parentTask.Result)
Console.WriteLine(i);
Console.WriteLine("Ending ContinueWith thread 2");
});
finalTask.Wait();
Console.WriteLine("ContinueWith thread is finished!");
}
问题是Task.Run
。这将以 TaskCreationOptions.DenyChildAttach
.
使用 Task.Factory.StartNew
的简单更改,问题就解决了。那是因为这里默认的是TaskCreationOptions.None
.
See here 深入讨论差异。