I have helper static method to “automatize” generating SSRS reports
public static WindowsFormsHost CreateSSRSReport(string reportServerUrl, string reportSystemName, string reportPath,
List<Parameter> parameters, int? idKey = null)
{
WindowsFormsHost windowsFormsHost = new WindowsFormsHost();
var reportViewer = new ReportViewer();
reportViewer.ProcessingMode = ProcessingMode.Remote;
windowsFormsHost.Child = reportViewer;
ServerReport serverReport = reportViewer.ServerReport;
serverReport.ReportServerUrl = new Uri(reportServerUrl);
serverReport.ReportPath = String.Concat("/", reportSystemName, "/", reportPath);
if(idKey != null)
{
var reportParameterInfos = serverReport.GetParameters();
var reportParameterInfo = reportParameterInfos.FirstOrDefault(p => Equals(p.Name, "IdKey"));
if (reportParameterInfo != null)
{
parameters.Add(new Parameter("IdKey", idKey));
}
}
parameters = parameters ?? new List<Parameter>();
serverReport.SetParameters(parameters.Select(p => new ReportParameter(p.Name, p.Value.ToString())));
reportViewer.RefreshReport();
return windowsFormsHost;
}
I would like to await that operation as the loading time is a bit long and displaying ProgessBar
till everything is done and ready would improve user experience.
private async Task GenerateReport()
{
SetWorkingStatus(true);
Report = await Task.Run(() => ReportingHelper.CreateSSRSReport(_pluginConfiguration.ReportServerPath,
_pluginConfiguration.ReportSystemName, SelectedReportDetails.SourceName, null));
SetWorkingStatus(false);
}
But that resulted in System.InvalidOperationException: 'The calling thread must be STA, because many UI components require this.'
The main application Main
method has [STAThread]
attribute specified. My next idea was to use Dispatcher
SetWorkingStatus(true);
await Application.Current.Dispatcher.Invoke(async () =>
{
Report = await Task.Run(() => ReportingHelper.CreateSSRSReport(_pluginConfiguration.ReportServerPath,
_pluginConfiguration.ReportSystemName, SelectedReportDetails.SourceName, null));
});
SetWorkingStatus(false);
But I keep getting the mentioned exception.
The Exception
is thrown at WindowsFormsHost windowsFormsHost = new WindowsFormsHost();
line in the helper method.