一个关于Parallel和action的问题
请教个问题,代码如下:
DataTable dt = GetPrice("SELECT distinct([StockCode]) FROM [MyStocks].[dbo].[StocksDayPrice] order by StockCode");
int datacount = dt.Rows.Count;
Stopwatch sw = new Stopwatch();
sw.Start();
int Arr_actionLength = 10;
int substep = datacount / Arr_actionLength;
int tail = datacount % Arr_actionLength;
if (tail != 0)
{
Arr_actionLength += 1;
}
Action[] actions = new Action[Arr_actionLength];
//****************问题就在这个for循环上************************//
for (int i = 0; i < Arr_actionLength; i++)
{
actions[i] = () =>
{
for (int j = (i * substep); j < ((i + 1) * substep); j++)
{
if (j >= datacount)
{
break;
}
updateTheAged("insert into test_parallel(stockcode) values('" + dt.Rows[j][0].ToString() + "')");
}
};
}
//**************************************//
Parallel.Invoke(actions);
这个for循环无法获取正常进行插入操作,但我将它换成
actions[0] = () =>
{
for (int j = 0 * substep; j < (0 + 1) * substep; j++)
{
if (j >= datacount)
{
break;
}
updateTheAged("insert into test_parallel(stockcode) values('" + dt.Rows[j][0].ToString() + "')");
}
};
.......
actions[10] = () =>
{
for (int j = 10 * substep; j < (10 + 1) * substep; j++)
{
if (j >= datacount)
{
break;
}
updateTheAged("insert into test_parallel(stockcode) values('" + dt.Rows[j][0].ToString() + "')");
}
};
又一切正常。运行结果也正常。搞不明白了,请假下大家。