我有以下场景:
我的应用程序必须从另一个应用程序中导入多个数据集,时间非常关键。
因此,它为每次导入生成一个线程。
因此,假设我有1到10次导入,其中4和5只能在导入1之后运行,6和7在导入2之后运行,8、9和10在导入3之后运行。
我考虑创建一个导入/处理列表,并在循环中检查它们,但我不知道如何从那里触发.set()。
或者为每个父级导入创建一个线程并在其中实例化处理程序,该父线程为每个子级导入触发一个线程,但是处理线程的线程是否正确是我不确定的。
你有什么解决方法吗?
更新:
在Brian Gideon的回答之后,我想到了以下解决方法:
我的应用程序必须从另一个应用程序中导入多个数据集,时间非常关键。
因此,它为每次导入生成一个线程。
因此,假设我有1到10次导入,其中4和5只能在导入1之后运行,6和7在导入2之后运行,8、9和10在导入3之后运行。
- 导入1
- 导入4
- 导入5
- 导入2
- 导入6
- 导入7
- 导入3
- 导入8
- 导入9
- 导入10
我考虑创建一个导入/处理列表,并在循环中检查它们,但我不知道如何从那里触发.set()。
或者为每个父级导入创建一个线程并在其中实例化处理程序,该父线程为每个子级导入触发一个线程,但是处理线程的线程是否正确是我不确定的。
你有什么解决方法吗?
更新:
在Brian Gideon的回答之后,我想到了以下解决方法:
dateSince = Convert.ToDateTime(txtBoxDateSince.Text);
dateTo = Convert.ToDateTime(txtBoxDateTo.Text);
//循环时间间隔内的所有日期 while (DateTime.Compare(dateSince, dateTo) <= 0) {
foreach (ListItem father in checkBoxListFather.Items)
{
if (father.Selected == true)
{
processClass process = new processClass();
// This WaitHandle will be used to get the child tasks going.
var wh = new ManualResetEvent(false);
//Method to Import, wraped in a delegate
WaitCallback fatherMethod = new WaitCallback(process.importProcess);
//and its parameters
processClass.importParameters param = new processClass.importParameters(wh, father.Value, null, dateSince);
// Queue the parent task.
ThreadPool.QueueUserWorkItem(fundMethod, param);
// Register the child tasks.
foreach (ListItem child in checkBoxListChild.Items)
{
if (child.Selected == true)
{
processClass.importParameters parameters = new processClass.importParameters(null, child.Value, null, dateSince);
// Registers a callback for the child task that will execute once the
// parent task is complete.
WaitOrTimerCallback childMethod = new WaitOrTimerCallback(process.anotherImportProcess);
RegisteredWaitHandle rwh = ThreadPool.RegisterWaitForSingleObject(wh, childMethod, parameters, Timeout.Infinite, true);
}//End if (child.Selected == true)
}//End foreach (ListItem fund in checkBoxListChild.Items)
}//End if (father.Selected == true)
}//End foreach (ListItem fundProcess in checkBoxListFather.Items)
dateSince = dtSince.AddDays(1);
}//在(DateTime.Compare(since,to) < 0)结束while循环
基本上是相同的答案,只是使用了没有lambda表达式的方法,并在它们上使用了参数。
我仍然没有进行压力测试,但它的工作非常好。
谢谢Brian。