在 foreach 循环中创建的同一个线程正在执行多次
Same thread created inside foreach loop is executing multiple times
我正在循环中的 foreach 报告列表中创建线程。创建的每个线程都将调用其 DLL,其中报告数据被插入到数据库中。问题是创建的每个线程都执行多次插入重复数据。
如何避免在 foreach 循环中多次执行相同线程?
下面是我的代码:
每个报表都通过线程调用DLL方法
foreach (ReportGeneric.ExportReportSchedulerModel report in reportList)
{
log.Debug(report.ReportName + " Export():");
var parametersArray = new object[1];
parametersArray[0] = report;
log.Debug("Thread started in ExportReport() for " + report.ReportName);
Thread exporttoexcel = new Thread(() =>
{
_isStarted = false;
//invoking DLL method
var ret = ReportInvokeFunction(moduleName: report.ReportName.Split('_')[0], className: report.ReportName, functionName: "SelectData", parameters: parametersArray);
if (ret > 0)
{
log.Debug("ExportReport() successfull " + report.ReportName);
_isStarted = true;
}
else
{
log.Error("ExportReport() failed " + report.ReportName);
_isStarted = false;
}
});
exporttoexcel.Start();
}
下面是为其中一份报告编写的日志:如下所示,同一个线程 [45] 在 2018-07-27 13:35:05,781 同时执行了两次。这会在数据库中创建重复数据。
2018-07-27 13:35:05,781 [45] DEBUG ReportGeneric.GenericReportBase - ---- Started reading : IvrHostTransactionReport from 20180727 133005 to 20180727 133505:
2018-07-27 13:35:05,781 [45] DEBUG IvrHostTransactionReport.IvrHostTransactionReport - ---- Started fetching data from SP : IvrHostTransactionReport from 20180727 133005 to 20180727 133505:
2018-07-27 13:35:05,781 [42] DEBUG ReportGeneric.GenericReportBase - ---- Started reading : ChatInteractionReport from 20180727 133005 to 20180727 133505:
2018-07-27 13:35:05,781 [42] DEBUG ChatInteractionReport.ChatInteractionReport - ---- Started fetching data from SP : ChatInteractionReport from 20180727 133005 to 20180727 133505:
线程之间应该是内存独立的,不能在线程内部使用"report"变量
foreach(report){
ReportGeneric.ExportReportSchedulerModel tempReport = report; // independent memory address
Thread exporttoexcel = new Thread(() => {
.....
tempReport ....
.....
});
exporttoexcel.Start();
}
我正在循环中的 foreach 报告列表中创建线程。创建的每个线程都将调用其 DLL,其中报告数据被插入到数据库中。问题是创建的每个线程都执行多次插入重复数据。 如何避免在 foreach 循环中多次执行相同线程?
下面是我的代码: 每个报表都通过线程调用DLL方法
foreach (ReportGeneric.ExportReportSchedulerModel report in reportList)
{
log.Debug(report.ReportName + " Export():");
var parametersArray = new object[1];
parametersArray[0] = report;
log.Debug("Thread started in ExportReport() for " + report.ReportName);
Thread exporttoexcel = new Thread(() =>
{
_isStarted = false;
//invoking DLL method
var ret = ReportInvokeFunction(moduleName: report.ReportName.Split('_')[0], className: report.ReportName, functionName: "SelectData", parameters: parametersArray);
if (ret > 0)
{
log.Debug("ExportReport() successfull " + report.ReportName);
_isStarted = true;
}
else
{
log.Error("ExportReport() failed " + report.ReportName);
_isStarted = false;
}
});
exporttoexcel.Start();
}
下面是为其中一份报告编写的日志:如下所示,同一个线程 [45] 在 2018-07-27 13:35:05,781 同时执行了两次。这会在数据库中创建重复数据。
2018-07-27 13:35:05,781 [45] DEBUG ReportGeneric.GenericReportBase - ---- Started reading : IvrHostTransactionReport from 20180727 133005 to 20180727 133505:
2018-07-27 13:35:05,781 [45] DEBUG IvrHostTransactionReport.IvrHostTransactionReport - ---- Started fetching data from SP : IvrHostTransactionReport from 20180727 133005 to 20180727 133505:
2018-07-27 13:35:05,781 [42] DEBUG ReportGeneric.GenericReportBase - ---- Started reading : ChatInteractionReport from 20180727 133005 to 20180727 133505:
2018-07-27 13:35:05,781 [42] DEBUG ChatInteractionReport.ChatInteractionReport - ---- Started fetching data from SP : ChatInteractionReport from 20180727 133005 to 20180727 133505:
线程之间应该是内存独立的,不能在线程内部使用"report"变量
foreach(report){
ReportGeneric.ExportReportSchedulerModel tempReport = report; // independent memory address
Thread exporttoexcel = new Thread(() => {
.....
tempReport ....
.....
});
exporttoexcel.Start();
}