【问题标题】:WPF populating DataGrid with large table blocking my UI threadWPF 用阻塞我的 UI 线程的大表填充 DataGrid
【发布时间】:2019-08-18 10:34:25
【问题描述】:

我花了一些时间研究和观察在尝试处理 UI 线程上的数据时避免阻塞 UI 线程的最佳做法。具体来说,我尽可能使用 async/await。但是,当通过 Binding 填充 DataGrid 时,我注意到我的 UI 在命令处理完成并将处理传递回 UI 后冻结。

XAML

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

DataModel 命令执行:

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

我已逐步检查代码以观察处理时间,放置调试器消息并且处理似乎正常。 await 语句大约需要 1.5 秒,最后的网格处理是几毫秒。然而,当我从“CommandExecute()”返回时,用户界面响应之前大约需要 3-5 秒。数据填充良好 - 它只需要永远。我不知道这是否是预期的,或者我是否可以控制。

谢谢。

【问题讨论】:

  • 我目前面临同样的问题,但就我而言,填充数据需要 6 分钟!你是怎么解决的?

标签: wpf user-interface async-await freeze blocking


【解决方案1】:

WPF DataGrid 以其性能问题而闻名。您可以尝试从这个thread 中查看答案。 基本上,最推荐的选项是:使用 Grid。特别是如果行数为数千。 或者,如果你想保留数据网格,有一些方法可以减少加载时间

EnableColumnVirtualization = true
EnableRowVirtualization = true

同样设置固定宽度和/或高度,datagrid 可能会在每次更新数据时尝试重新计算所需的宽度和高度。我会检查其他线程以根据您的需要优化您的数据网格。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多