小编Joe*_*oeH的帖子

C#TaskFactory没有完成所有迭代

使用TaskFactory时遇到一些奇怪的事情:

Task<int[]> parent = Task.Run(() =>
{
    int length = 100;
    var results = new int[length];
    TaskFactory tf = new TaskFactory(TaskCreationOptions.AttachedToParent,
        TaskContinuationOptions.ExecuteSynchronously);

    // Create a list of tasks that we can wait for
    List<Task> taskList = new List<Task>();
    for (int i = 0; i < length - 1; i++) // have to set -1 on length else out of bounds exception, huh?
    {
        taskList.Add(tf.StartNew(() => results[i] = i));
    }
    // Now wait for all tasks to complete
    Task.WaitAll(taskList.ToArray());

    return results; …
Run Code Online (Sandbox Code Playgroud)

c# task taskfactory

2
推荐指数
1
解决办法
64
查看次数

标签 统计

c# ×1

task ×1

taskfactory ×1