这是因为
DataTable
有很多要创建的对象(行、值)。您应该在不同的线程中执行适配器和填充数据表,并在继续之前同步等待每个操作完成。
下面的代码是用记事本编写的,可能还没有编译,但希望你能理解…
// Setup state as a parameter object containing a table and adapter to use to populate that table here
void DoWork()
{
List<AutoResetEvent> signals = GetNumberOfWaitHandles(2);
var params1 = new DataWorkerParameters
{
Command = GetCommand1();
Table = new DataTable();
}
var params2 = new DataWorkerParameters
{
Command = GetCommand2();
Table = new DataTable();
}
ThreadPool.QueueUserWorkItem(state =>
{
var input = (DataWorkerParameters)state;
PopulateTable(input);
input.AutoResetEvent.Set(); // You can use AutoResetEvent.WaitAll() in the caller to wait for all threads to complete
},
params1
);
ThreadPool.QueueUserWorkItem(state =>
{
var input = (DataWorkerParameters)state;
PopulateTable(input);
input.AutoResetEvent.Set(); // You can use AutoResetEvent.WaitAll() in the caller to wait for all threads to complete
},
params2
);
WaitHandle.WaitAll(signals.ToArray());
}
void PopulateTable(DataWorkerParameters parameters)
{
input.Command.ExecuteReader();
input.Table.Load(input.Command);
}