Receiving cannot wait for emptiness, according to the method I want to expect

I am on a team writing a WPF application. We need to make the user hide / show different columns so that he reflects this in the ReportViewer control on one of the views. During testing, we found that it takes a long time to add data to ReportViewer data sources; sometimes from a few seconds to a minute. Too long I think for users. So I try to use asynchronous C # and wait. However, when I applied to wait for the line, which is the hog process, and then compile it, I get an error with the C # compiler: “Can't wait for void.” In this case, I cannot change what the .NET platform returns, its emptiness. So, how do I deal with this situation? Here is the code:

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();
}
+4
2

, , Task, . Task.Run:

await Task.Run(() => 
{
   rptViewer.LocalReport.DataSources.Add(new ReportDataSource("MyData", CoreUtils.ToDataTable(itemsSource)));
});

, , , . , , . , , Task.

+6

, , , CoreUtils.ToDataTable(itemsSource) , , .

ToDataTable, , , , async , .

var data = await CoreUtils.ToDataTableAsync(itemsSource)
rptViewer.LocalReport.DataSources.Add(new ReportDataSource("MyData", data));

, - Task.Run, .

var data = await Task.Run(() => CoreUtils.ToDataTable(itemsSource));
rptViewer.LocalReport.DataSources.Add(new ReportDataSource("MyData", data));
+4

Source: https://habr.com/ru/post/1669730/


All Articles