我的函数有三个部分。如何使用异步任务创建Reportviewer
第一部分参数弹出
第二部分执行的代码在一个新的线程与ApartmentState.STA打开。 3
部分 - 显示的ReportViewer
我目前收到此错误the calling thread cannot access this object because a different thread owns it.
public async void AnticipatedEntriesReport(bool fund)
{
var anticipatedReport = new AnticipatedReport(fund);
ReportPreviewForm report = new ReportPreviewForm();
anticipatedReport.InitializeParameters();
if (anticipatedReport.GetParameters() != null)
{
await RunAsyncTask(
() =>
{
report = anticipatedReport.GenerateReport(SelectedLoans);
});
report.Show();
}
}
我的代码处断裂report.Show()。
expectedReport.GenerateReport返回一个ReportPreviewForm。
我想知道我做错了什么?我认为这是基于我创建对象的位置。
public async Task RunAsyncTask(System.Action action)
{
try
{
await ThreadManager.StartSTATask(action);
}
catch (Exception ex)
{
}
}
public static Task StartSTATask(System.Action func)
{
var tcs = new TaskCompletionSource<object>();
var thread = new Thread(() =>
{
try
{
func();
tcs.SetResult(null);
}
catch (Exception e)
{
tcs.SetException(e);
}
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
return tcs.Task;
}
为什么能你是否将报告作为任务的结果返回? – shlatchz
什么是'RunAsyncTask'? –
@StephenCleary嗨,我更新它包括RunAsyncTask方法 – Master