我正在编写一个WPF应用程序的团队。我们必须这样做,以便当用户隐藏/显示不同的列时,它将反映在其中一个视图的ReportViewer控件中。在测试中,我们发现将数据添加到ReportViewer的数据源需要很长时间;有时大约几秒到可能一分钟。太长时间我想为用户。所以我试图使用C#的异步并等待。但是,当我申请等待进程管理线然后编译它时,我从C#编译器中得到一个错误,“无法等待'void'”。在这种情况下,我无法改变.NET框架返回的结果,它是无效的。那么我该如何处理这种情况呢?下面的代码:获取一个无法等待无效,在我想要等待的方法上
private async Task GenerateReportAsync()
{
DataSet ds = new DataSet();
DataTable dt = new DataTable();
dt.Clear();
int iCols = 0;
//Get the column names
if (Columns.Count == 0) //Make sure it isn't populated twice
{
foreach (DataGridColumn col in dataGrid.Columns)
{
if (col.Visibility == Visibility.Visible)
{
Columns.Add(col.Header.ToString()); //Get the column heading
iCols++;
}
}
}
//Create a DataTable from the rows
var itemsSource = dataGrid.ItemsSource as IEnumerable<Grievance>;
if (this.rptViewer != null)
{
rptViewer.Reset();
}
rptViewer.LocalReport.DataSources.Clear();
if (m_rdl != null)
{
m_rdl.Dispose();
}
Columns = GetFieldOrder();
m_rdl = CoreUtils.GenerateRdl(Columns, Columns);
rptViewer.LocalReport.LoadReportDefinition(m_rdl);
//the next line is what takes a long time
await rptViewer.LocalReport.DataSources.Add(new ReportDataSource("MyData", CoreUtils.ToDataTable(itemsSource)));
rptViewer.RefreshReport();
}
如果您认为等待需要很长时间的事情是解决您的问题的方法,那么您必须对“await”的做法产生误解。你能描述一下你的信念是关于“等待”吗?只有等待已经*异步的东西才有意义;你相信等待*使*不是异步操作的异步操作?它不是。 –
我的理解是,使用async/await可以将控制返回到UI线程,而不是挂起它,等待一些长时间的工作过程完成。但是谢谢你的提问。 – Rod
我的观点是,如果函数无效返回*和*已经异步然后*它不应该花费太多时间*。如果它无效返回,同步和高延迟,那么没有什么可以“等待”。等待管理现有的异步操作。如果你有一个你想要异步的高延迟操作,'await'不会帮助你。你将不得不弄清楚如何通过其他机制来实现异步。 –