任务无法正常启动
Tasks fail to launch properly
private static int[] Sort(int[] arr)
{
int temp;
int[] result = new int[arr.Length];
Array.Copy(arr, result, arr.Length);
for (int i = 0; i < result.Length - 1; i++)
{
for (int j = i + 1; j < result.Length; j++)
{
if (result[i] > result[j])
{
temp = result[i];
result[i] = result[j];
result[j] = temp;
}
}
}
return result;
}
public static List<List<T>> ChunkBy<T>(this List<T> source, int chunkSize)
{
return source
.Select((x, i) => new { Index = i, Value = x })
.GroupBy(x => x.Index / chunkSize)
.Select(x => x.Select(v => v.Value).ToList())
.ToList();
}
...
List<List<int>> unsortedLists = list.ChunkBy(chunkSize);
int count = unsortedLists.Count;
List<List<int>> lists = new List<List<int>>();
Task<List<int>>[] tasks = new Task<List<int>>[count];
//from 0 to 9
for (int i = 0; i < count; i++)
{
tasks[i] = new Task<List<int>>(() =>
{
lists[i] = Sort(unsortedLists[i].ToArray()).ToList(); //out of range exception i = 10
return lists[i];
});
}
for(int i = 0; i < count; i++)
{
tasks[i].Start();
}
for(int i = 0; i < count; i++)
{
lists[i] = await tasks[i]; //array out of range
}
我遇到了非常奇怪的错误。我稍微简化了我的代码并提供了重要的部分。
我正在创建一堆任务,启动它们,然后它在第 3 个循环时立即失败。出于某种原因,它使用以 9 结尾的旧迭代器,并将其增加到 10,从而产生异常。我不知道该怎么办,目前看来是环境错误。
你应该never use the task constructor。在这种情况下,请改用 Task.Run
。
你的另一个问题是 C# 中的 for
循环只定义了一个循环变量,并且 lambda 关闭 variables,而不是 values。这就是为什么您会看到 i
即 10
.
最简单的解决方案是完全取消 for
循环:
var tasks = Enumerable.Range(0, count)
.Select(i => Task.Run(() =>
{
lists[i] = Sort(unsortedLists[i].ToArray()).ToList();
return lists[i];
}))
.ToList();
var results = await Task.WhenAll(tasks);
private static int[] Sort(int[] arr)
{
int temp;
int[] result = new int[arr.Length];
Array.Copy(arr, result, arr.Length);
for (int i = 0; i < result.Length - 1; i++)
{
for (int j = i + 1; j < result.Length; j++)
{
if (result[i] > result[j])
{
temp = result[i];
result[i] = result[j];
result[j] = temp;
}
}
}
return result;
}
public static List<List<T>> ChunkBy<T>(this List<T> source, int chunkSize)
{
return source
.Select((x, i) => new { Index = i, Value = x })
.GroupBy(x => x.Index / chunkSize)
.Select(x => x.Select(v => v.Value).ToList())
.ToList();
}
...
List<List<int>> unsortedLists = list.ChunkBy(chunkSize);
int count = unsortedLists.Count;
List<List<int>> lists = new List<List<int>>();
Task<List<int>>[] tasks = new Task<List<int>>[count];
//from 0 to 9
for (int i = 0; i < count; i++)
{
tasks[i] = new Task<List<int>>(() =>
{
lists[i] = Sort(unsortedLists[i].ToArray()).ToList(); //out of range exception i = 10
return lists[i];
});
}
for(int i = 0; i < count; i++)
{
tasks[i].Start();
}
for(int i = 0; i < count; i++)
{
lists[i] = await tasks[i]; //array out of range
}
我遇到了非常奇怪的错误。我稍微简化了我的代码并提供了重要的部分。
我正在创建一堆任务,启动它们,然后它在第 3 个循环时立即失败。出于某种原因,它使用以 9 结尾的旧迭代器,并将其增加到 10,从而产生异常。我不知道该怎么办,目前看来是环境错误。
你应该never use the task constructor。在这种情况下,请改用 Task.Run
。
你的另一个问题是 C# 中的 for
循环只定义了一个循环变量,并且 lambda 关闭 variables,而不是 values。这就是为什么您会看到 i
即 10
.
最简单的解决方案是完全取消 for
循环:
var tasks = Enumerable.Range(0, count)
.Select(i => Task.Run(() =>
{
lists[i] = Sort(unsortedLists[i].ToArray()).ToList();
return lists[i];
}))
.ToList();
var results = await Task.WhenAll(tasks);