时 通过多个线程将表添加到数据集是线程安全的吗?
List<Action> TestActions = new List<Action>();
Action action1 = new Action(() => Method1(dsDataset));
TestActions.Add(action1);
Action action2 = new Action(() => Method2(dsDataset));
TestActions.Add(action2);
Action action3 = new Action(() => Method3(dsDataset));
TestActions.Add(action3);
try
{
Parallel.ForEach(TestActions, (a) => a());
}
Method1(DataSet ds) {
//excecute a db call and returns datatable
Datatable db ={database query}
ds.Tables.Add(db)
}
Method2(DataSet ds) {
//excecute a db call and returns datatable
Datatable db ={database query}
ds.Tables.Add(db)
}
Method3(DataSet ds) {
//excecute a db call and returns datatable
Datatable db ={database query}
ds.Tables.Add(db)
}
当每个线程尝试添加时,上面的代码是否有效或者我需要锁定锁定数据集? 我有什么更好的办法吗?