C# 我可以在新线程上调用ReportExecutionService吗?
这是我第一次处理大型多线程项目,所以请容忍我。我们正在编写一个窗口服务,它将在数据库表上循环,并根据找到的记录生成SSRS报告。我试图把它写成一个多线程系统C# 我可以在新线程上调用ReportExecutionService吗?,c#,multithreading,task,backgroundworker,threadpool,C#,Multithreading,Task,Backgroundworker,Threadpool,这是我第一次处理大型多线程项目,所以请容忍我。我们正在编写一个窗口服务,它将在数据库表上循环,并根据找到的记录生成SSRS报告。我试图把它写成一个多线程系统 // THIS IS LONG RUNNING TASK void ProcessReport(Report report) { ReportExecutionService rs = new ReportExecutionService(); rs.Credentials = new NetworkCredenti
// THIS IS LONG RUNNING TASK
void ProcessReport(Report report)
{
ReportExecutionService rs = new ReportExecutionService();
rs.Credentials = new NetworkCredential(id,pass);
rs.Url = "url...asmx";
rs.ExecutionHeaderValue = new ExecutionHeader();
rs.Timeout = Timeout.Infinite;
string historyID = null;
ExecutionInfo info = rs.LoadReport("/../ReportName", historyID);
}
在ProcessReport()方法中,我调用ReportExecutionService并生成SSRS报告。这是一项长时间运行的任务,可能需要12小时。这里有一个选项1,用于为我要生成的每个报告打开新线程。我们应该限制我们打开的线的数量
foreach(Report report in reportList)
{
if (report.ReportID != null)
{
Thread thread = new Thread(() => ProcessReport(report));
thread.Start();
}
}
问题:
public class ReportExecutionService
{
private readonly HttpClient httpClient = new HttpClient();
public async Task<ExecutionInfo> LoadReportAsync(string reportName, string historyId)
{
// Lets assume you're sending the report as a JSON,
// and the return type is also a JSON
var content = new { ReportName = reportName, HistoryId = historyId };
var response = await httpClient.PostAsJsonAsync(
$"http://www.apiaddress.com/{reportName}", content);
var jsonResponse = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<ExecutionInfo>(jsonResponse);
}
}
public async Task ProcessAllReports()
{
var reportTasks = reportList.Select(report => ProcessReportAsync(report));
ExecutionInfo[] reports = await Task.WhenAll(reportTasks);
// Do stuff with the reports.
}
现在你可以这样称呼它:
public class ReportExecutionService
{
private readonly HttpClient httpClient = new HttpClient();
public async Task<ExecutionInfo> LoadReportAsync(string reportName, string historyId)
{
// Lets assume you're sending the report as a JSON,
// and the return type is also a JSON
var content = new { ReportName = reportName, HistoryId = historyId };
var response = await httpClient.PostAsJsonAsync(
$"http://www.apiaddress.com/{reportName}", content);
var jsonResponse = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<ExecutionInfo>(jsonResponse);
}
}
public async Task ProcessAllReports()
{
var reportTasks = reportList.Select(report => ProcessReportAsync(report));
ExecutionInfo[] reports = await Task.WhenAll(reportTasks);
// Do stuff with the reports.
}
您正在进行HTTP呼叫以处理报告吗?请查看Hi,谢谢您提供的详细信息。我打算多读一些关于任务的书。在您的示例中,我注意到您有Task.WhenAll()这行的意思是它将等待所有任务完成。在我的要求中,我不等待任何回调…基本上,我的服务应该只执行reporting service并在文件磁盘上生成PDF或Excel文件。您的示例非常简单,但我还不了解TASK如何运行所有不同的报告生成。它们会在不同的线程上运行吗?如果是,我在运行时是否有权访问这些线程以获取状态?再次感谢,它们不会在不同的线程上执行。这里的大部分工作都在等待IO,您不需要任何线程。您还可以在它们一个接一个地完成时对它们进行处理,以创建您需要的PDF文件。是的,
任务。所有的将等待所有任务完成。我明白了,我已经更新了我的实现,这是有意义的,但是我需要解决的另一件事是报告执行优先级。如果某些报告的优先级高于它们应该以更高的重要性运行。我打算用线程优先级机制来解决这个问题。对于任务和异步,我如何做到这一点?我建议您为此提出一个新问题。