【发布时间】:2020-02-25 10:59:59
【问题描述】:
我正在使用 Windows 服务生成报告,比如 2K 左右的报告。我也在使用 TPL 来提高性能。我观察到在本地机器上它对 40 报告工作正常,但在 2K 的 UAT 上它不能正常工作,它异常停止工作并启动 Windows 服务状态。系统事件查看器中没有事件日志条目(我假设我没有收到事件日志条目,因为服务状态已启动并且它没有异常终止)
这是我的代码块。
foreach (pt_report report in reportCollection)
{
using(dbContent context=new dbContext())
{
//Execute 1 sp which takes around 2 minutes.
//do some db operation to update status of sp execution.
//This step is mandatory for all 2K plan before running Async task.
}
if(report.Status != Completed) //check for completion of report generation
{
Task generateReport = new Task(delegate
{
ScheduleMassReportGeneration(report); // This takes around 2-3 min more to generate pdf.
});
generateReport.Start();
//Problem #1 occurs (see below description).
}
}
问题 #1:
我知道当异步任务开始时,我需要写一些东西来连接线程,以便我可以检查执行状态。基于上面的代码,我无法控制它,现在就像孤儿一样。现在我想控制它,以便我可以检查该线程/任务发生了什么并将适当的状态记录到数据库并重新运行这些任务。
PS:在ScheduleMassReportGeneration()method() 中编写了正确的日志记录,以跟踪生成报告时发生的情况。但我没有看到任何错误或可疑的迹象。
就这样溜走了,不留痕迹。
【问题讨论】:
标签: c# .net multithreading thread-safety task-parallel-library