我正在嘗試使用Parallel.for循環來加速我的過程。我無法完成這項工作,因爲我使用的索引超出了界限。在研究這個網站之後,我想我知道我在做什麼doing wrong,並且我認爲我也以臨時變量的形式找到了解決方案,這些臨時變量在進入代碼中的動作之前存儲了循環變量。然而,這在我的例子中不起作用。我發現有人提供給System.Collections.Concurrent的鏈接,它可以爲像這樣的情況提供安全線程,但我不知道如何使用該集合。我如何去做這件事?在Parallel.for循環中超出範圍
我試圖創建一個複製粘貼代碼爲你們跑,但我做錯事的時候有這可能表明我的經驗不足:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text;
namespace empty
{
class Program
{
double[, , ,] info = new double[100, 100, 10, 5];
void calculate()
{
int row;
int layer, tex_class;
double result;
try
{
for (row = 0; row < 100; row++)
{
Parallel.For(0, 99, col =>
{
int tempcol = col;
for (layer = 0; layer < 10; layer++)
{
int templayer = layer;
for (tex_class = 0; tex_class < 5; tex_class++)
{
int tempclass = tex_class;
result = info[row, tempcol, templayer, tempclass];
}
//other code
}
});
}
}
catch { }
}
static void Main(string[] args)
{
calculate();
}
}
}
哦,我的4維陣列。 –
代碼應該做什麼?我根本不理解它。也許如果你發佈一些預期的輸出...... –