Caliburn Micro 中的 Task.Run() 性能不佳

问题描述 投票:0回答:1

我正在使用 Task.Run() 在 ViewModel 中的另一个函数内运行 CPU 和 IO 绑定的同步方法(由 UI 中的按钮触发,使用 Caliburn Micro 的命令命名约定)。 UI 保持响应,但问题是这比同步运行 CPU/IO 密集型方法慢大约 10 倍。使用CM版本4.0.212。

在我的 XAML 中我设置了:

<Button x:Name="OpenXml" ToolTip="{x:Static resx:Strings.OpenXml}" IsEnabled="{Binding Ready}">

在我的 ViewModel 中,我有以下功能(此处使用 MVVMDialogs 用于打开文件对话框)

public async void OpenXml()
{
    if (this.Schema != null)
    {
        var settings = new OpenFileDialogSettings
        {
            Title = Resources.Strings.OpenXml,
            Filter = "XML files (*.xml)|*.xml"
        };
        bool? success = _dialogService.ShowOpenFileDialog(this, settings);
        if (success == true)
        {
            this.Ready = false;
            var progress = new Progress<int>(percent =>
            {
                 this.Progress = percent;
            });
            this.RootNode = await Task.Run(() => LoadXml(settings.FileName, progress));
            this.Progress = 0;
            this.Ready = true;
        }
    }
}

无需过多了解 LoadXml 函数的实现细节,其声明如下所示:(基本上,它将 XML 文件加载到树中,其中每个节点都知道它与哪个 XSD 架构元素关联)

public RootNode LoadXml(string path, IProgress<int>? progress = null)
{
     // Fairly intense CPU- and IO- bound work with XmlSchemaValidator and walking through an xDocument node by node
}

因此,当我同步运行 LoadXml 时,它的运行速度足够快,但使用 Task.Run() 时,我看到速度大约降低了 10 倍(同时 UI 保持响应且进度条更新)。

我尝试的是确保 LoadXml 在计算期间不会更新或访问任何 VM 属性/字段。此外,仅当百分比 (0-100) 增加时才会发生进度报告(因此,如果处理具有数千个节点的文件,则不会在每个节点上报告)。除此之外,我没有主意了 - 想知道是否有任何策略可以恢复 LoadXml 的原始性能?

任何想法都会有帮助,提前致谢!

c# wpf mvvm async-await caliburn.micro
1个回答
0
投票

尝试在LoadXml方法中放置秒表,并同步和异步测量各个部分的执行时间。这样您就可以定位慢速部分。 (抱歉,我无法评论这个问题)

© www.soinside.com 2019 - 2024. All rights reserved.