I have many methods filling different DataTables in the same DataSet:
public override void FillMethod1(MyDataSet ds)
{
try
{
using (SqlDataAdapter adapter = new SqlDataAdapter(query, connectionObj))
{
using (SqlCommandBuilder adapterSCB = new SqlCommandBuilder(adapter))
{
adapter.Fill(ds.MyTable);
}
}
}
catch (Exception e)
{
//Exception handling
}
}
public override void FillMethod2(MyDataSet ds)
{
try
{
using (SqlDataAdapter adapter = new SqlDataAdapter(query, connectionObj))
{
using (SqlCommandBuilder adapterSCB = new SqlCommandBuilder(adapter))
{
adapter.Fill(ds.MyTable2);
}
}
}
catch (Exception e)
{
//Exception handling
}
}
[....]
using(MyDataSet ds = new MyDataSet())
{
FillMethod1(ds);
FillMethod2(ds);
[...]
}
And I'd like to parallelize these operation using Task:
using(MyDataSet ds = new MyDataSet())
{
Task fill1 = Task.Run(() => FillMethod1(ds));
Task fill2 = Task.Run(() => FillMethod2(ds));
[...]
Task.WaitAll(fill1, fill2, [...]);
}
After some research I found that DataSet is not thread-safe, but is it safe when working on different DataTable?