Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 任务并行库异常_C#_.net_Task Parallel Library - Fatal编程技术网

C# 任务并行库异常

C# 任务并行库异常,c#,.net,task-parallel-library,C#,.net,Task Parallel Library,我正在尝试使用任务并行库逐个单元构建矩阵单元 我有以下代码可以执行此操作: List<Campaign> campaigns = GetHomeCampaigns(); Dictionary<int, string> sellers = GetHomeSellers(); int numTasks = campaigns.Count*sellers.Count; Task<MatrixCell<string>>[] statusTasks = n

我正在尝试使用任务并行库逐个单元构建矩阵单元

我有以下代码可以执行此操作:

List<Campaign> campaigns = GetHomeCampaigns();
Dictionary<int, string> sellers = GetHomeSellers();

int numTasks = campaigns.Count*sellers.Count;
Task<MatrixCell<string>>[] statusTasks = new Task<MatrixCell<string>>[numTasks];

int count = 0;                   
for(int i = 0; i < campaigns.Count -1;i++)
{
    for(int j = 0; j < sellers.Count -1;j++)
    {
        Func<MatrixCell<string>> getStatus = () => GetStatus(campaigns[i].CampaignID, sellers.ElementAt(j).Key);
        statusTasks[count] = Task.Factory.StartNew(getStatus);
        count++;
    }
}
Task.WaitAll(statusTasks);
我得到了以下例外

The tasks array included at least one null element.
Parameter name: tasks
我已经检查了数组,它显示所有项目都存在于statusTasks中

不太清楚还能去哪里看


谢谢,

当您在基于0的索引语言中使用
for
循环时,您不需要执行
<.Count-1
。这应该是:

for (int i = 0; i < campaigns.Count; i++)
for(int i=0;i

由于在基于0的索引语言中使用
for
循环时是
,因此不需要执行
<.Count-1
。这应该是:

for (int i = 0; i < campaigns.Count; i++)
for(int i=0;i

因为它是<代码> 如果您真的想使用TPL,请考虑使用并行类:

Parallel.For(0, campaigns.Count, i =>  // the outer loop is most important
{
    Parallel.For(0, sellers.Count, j =>  
    {
        GetStatus(campaigns[i].CampaignID, sellers.ElementAt(j).Key);
    }
}));
// no Waiting here

这将使用一个分区器,它可能决定不为每个<代码> j/c>使用一个任务,但是要构建段。

如果您真的想使用TPL,请考虑使用并行类:

Parallel.For(0, campaigns.Count, i =>  // the outer loop is most important
{
    Parallel.For(0, sellers.Count, j =>  
    {
        GetStatus(campaigns[i].CampaignID, sellers.ElementAt(j).Key);
    }
}));
// no Waiting here

这将使用一个分区器,它可能会决定不为每个
j
使用任务,而是构建段。

这意味着数组中有
null
s。@svick:没错。OP没有迭代所有的条目,因此任务数组结尾处没有满。这意味着数组中有
null
s。@svick:没错。OP并没有迭代所有的条目,因此任务数组的末尾并没有满。