2

I've spent some time researching and observing the best practices to avoid blocking the UI thread when attempting to process data on the UI thread. Specifically, I utilize the async/await where I can. However, when populating a DataGrid via Binding, I noticed my UI freezes up after command processing is completed and processing is passed back to the UI.

XAML

<DataGrid ItemsSource="{Binding EndpointModel.DataView}"
          AutoGenerateColumns="True" IsReadOnly="True">            
</DataGrid>

DataModel command execution:

public async void CommandExecute()
{
    ...

    JsonData = await accessor.GetDataAsync(new Endpoint.Params().Universe(universe)
                                                                .WithStart(start)
                                                                .WithEnd(end));

    // Creates a very large DataTable within my display (30 x 350)
    var grid = EndpointModel.CreateDataGrid(JsonData);      
    EndpointModel.DataView = grid.AsDataView();
}

I've stepped through the code to observe processing times, placed debugger messages and processing seems normal. The await statement takes about 1.5 seconds and the grid processing at the end is a few ms. Yet, when I return from the 'CommandExecute()', it takes about 3-5 seconds before the UI is responsive. The data populates fine - it just takes forever. I don't know if this is expected or whether I have control over this.

thanks.

zinc1oxide
  • 490
  • 3
  • 15
  • I am currently facing the same issue, but in my case it takes 6 minutes to populate the data!! How did you manage to fix it? – Raya Sep 29 '21 at 12:13

1 Answers1

2

WPF DataGrid is known for its performance issues. You can try to go through answers from this thread. Basically, most recommended option is: using Grid. Especially if rows counts thousands. Or, if you want to keep datagrid, there are such ways to decrease loading time as

EnableColumnVirtualization = true
EnableRowVirtualization = true

Also set fixed width and/or height, datagrid may try to recalculate required width and height every time there is data updated. Id check other threads to optimize your datagrid to your needs.

Nomad Developer
  • 473
  • 4
  • 14