我是使用多任务处理的新手。运行以下代码时,有一次例外。为什么会引发异常。
我给出了(对象引用未设置为对象实例的)异常。我正在寻找哪个对象抛出。但是我从来没有在使用多任务之前给出过例外。
代码:
System.Threading.Tasks.Task[] tasks = new System.Threading.Tasks.Task[vw.Count];
System.Collections.Concurrent.ConcurrentQueue<DataSet> dsCollection = new System.Collections.Concurrent.ConcurrentQueue<DataSet>();
for (int i = 0; i < tasks.Length; i++)
{
tasks[i] = System.Threading.Tasks.Task.Factory.StartNew((Object obj) =>
{
DataRowView dr = obj as DataRowView;
if ((dr["MagicMasterView"].ToString() == "1") || (dr["MagicMasterView"] == System.DBNull.Value))
{
DataSet dsTmp = new DataSet();
List<SqlParameter> sqlParameters3 = new List<SqlParameter>();
sqlParameters3.Add(new SqlParameter("@MasterID", nMasterID));
sqlParameters3.Add(new SqlParameter("@UserID", nUserID));
sqlParameters3.Add(new SqlParameter("@FirmID", nFirmID));
SqlProcedureBuilder sqlProcedure3 = new SqlProcedureBuilder()
{
IsTransaction = false,
NTimeout = 180000,
Procedure = sProcedure + "_Fill",
UserID = nUserID,
SrcTable = sProcedure,
SqlParams = sqlParameters3
};
using (SqlExecutor sql = new SqlExecutor())
{
sql.Execute(sqlProcedure3, ref dsTmp);
}
sqlProcedure3.Dispose();
dsCollection.Enqueue(dsTmp);
}
}, vw[i]);
}
System.Threading.Tasks.Task.WaitAll(tasks);
foreach (var item in dsCollection)
{
ds.Merge(item as DataSet, false);
}
注意:SqlExecutor,SqlProcedureBuilders类已在db中执行了过程。