4

我有一个 WPF 应用程序,它需要解析一堆包含产品的大型 XML 文件(大约 40MB),并保存有关所有实际上是书籍的产品的信息。对于进度报告,我有一个数据网格,它显示文件名、状态(“等待”、“解析”、“已完成”等)、找到的产品数量、解析的产品数量和找到的书籍数量,比如这个:

        <DataGrid Grid.ColumnSpan="2" Grid.Row="1" ItemsSource="{Binding OnixFiles}" AutoGenerateColumns="False" 
              CanUserAddRows="False"
              CanUserDeleteRows="False"
              CanUserReorderColumns="False"
              CanUserResizeColumns="False"
              CanUserResizeRows="False"
              CanUserSortColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Bestand" IsReadOnly="True" Binding="{Binding FileName}" SortMemberPath="FileName" />
            <DataGridTextColumn Header="Status" IsReadOnly="True" Binding="{Binding Status}" />
            <DataGridTextColumn Header="Aantal producten" IsReadOnly="True" Binding="{Binding NumTotalProducts}" />
            <DataGridTextColumn Header="Verwerkte producten" IsReadOnly="True" Binding="{Binding NumParsedProducts}" />
            <DataGridTextColumn Header="Aantal geschikte boeken" IsReadOnly="True" Binding="{Binding NumSuitableBooks}" />                
        </DataGrid.Columns>
    </DataGrid>

当我点击“解析”按钮时,我想遍历文件名列表并解析每个文件,报告产品的数量、解析的产品和沿途找到的书籍。显然我希望我的 UI 保持响应,所以我想使用 Task.Run() 在不同的线程上进行解析。

当用户点击标有“解析”的按钮时,应用程序需要开始解析文件。如果我在按钮命令的 command_executed 方法中调用 TaskRun 一切正常:

    private async void ParseFilesCommand_Executed(object sender, ExecutedRoutedEventArgs e)
    {
        foreach (var f in OnixFiles)
        {
            await Task.Run(() => f.Parse());
        }
    }

    // In the OnixFileViewModel
    public void Parse()
    {
        var progressIndicator = new Progress<ParsingProgress>(ReportProgress);
        var books = Parser.ParseFile(this.fileName, progressIndicator);
    }

    private void ReportProgress(ParsingProgress progress)
    {
        // These are properties that notify the ui of changes
        NumTotalProducts = progress.NumTotalProducs;
        NumParsedProducts = progress.NumParsedProducts;
        NumSuitableBooks = progress.NumSuitableBooks;
    }

    // In the class Parser
public static IEnumerable<Book> ParseFile(string filePath, IProgress<ParsingProgress> progress)
    {
        List<Book> books = new List<Book>();

        var root = XElement.Load(filePath);
        var fileInfo = new FileInfo(filePath);
        XNamespace defaultNamespace = "http://www.editeur.org/onix/3.0/reference";

        var products = (from p in XElement.Load(filePath).Elements(defaultNamespace + "Product")
                        select p).ToList();

        var parsingProgress = new ParsingProgress()
        {
            NumParsedProducts = 0,
            NumSuitableBooks = 0,
            NumTotalProducs = products.Count
        };

        progress.Report(parsingProgress);

        foreach (var product in products)
        {
            // Complex XML parsing goes here
            parsingProgress.NumParsedProducts++;

            if (...) // If parsed product is actual book
            {  
                parsingProgress.NumSuitableBooks++;                 
            }

            progress.Report(parsingProgress);
        }

        return books;
    }

这一切都执行得非常快,用户界面快速更新并保持响应。但是,如果我将对 Task.Run() 的调用移到 ParseFile 方法中,如下所示:

    private async void ParseFilesCommand_Executed(object sender, ExecutedRoutedEventArgs e)
    {
        foreach (var f in OnixFiles)
        {
            await f.ParseAsync();
        }
    }

    // In the OnixFileViewModel
    public async Task ParseAsync()
    {
        var progressIndicator = new Progress<ParsingProgress>(ReportProgress);
        var books = await Parser.ParseFileAsync(this.fileName, progressIndicator);
    }

    private void ReportProgress(ParsingProgress progress)
    {
        // These are properties that notify the ui of changes
        NumTotalProducts = progress.NumTotalProducs;
        NumParsedProducts = progress.NumParsedProducts;
        NumSuitableBooks = progress.NumSuitableBooks;
    }

    // In the class Parser
public static async Task<IEnumerable<Book>> ParseFileAsync(string filePath, IProgress<ParsingProgress> progress)
    {
        List<Book> books = new List<Book>();

        await Task.Run(() =>
        {

        var root = XElement.Load(filePath);
        var fileInfo = new FileInfo(filePath);
        XNamespace defaultNamespace = "http://www.editeur.org/onix/3.0/reference";

        var products = (from p in XElement.Load(filePath).Elements(defaultNamespace + "Product")
                        select p).ToList();

        var parsingProgress = new ParsingProgress()
        {
            NumParsedProducts = 0,
            NumSuitableBooks = 0,
            NumTotalProducs = products.Count
        };

        progress.Report(parsingProgress);

        foreach (var product in products)
        {
            // Complex XML parsing goes here
            parsingProgress.NumParsedProducts++;

            if (...) // If parsed product is actual book
            {  
                parsingProgress.NumSuitableBooks++;                 
            }

            progress.Report(parsingProgress);
        }
        });

        return books;
    }

UI 锁定,直到文件完成解析后才更新,并且一切看起来都慢得多。

我错过了什么?如果您在 command_executed 处理程序中调用 Task.Run() ,为什么它会按预期工作,但如果您在该方法调用的异步方法中调用它则不会?

编辑:根据 Shaamaan 的要求,这是我正在做的一个更简单的示例(仅使用 thread.sleep 来模拟工作负载),但令人沮丧的是,该示例的工作方式与我最初预期的一样,未能突出我遇到的问题. 尽管如此,为了完整性添加它:

MainWindow.xaml:

<Window x:Class="ThreadingSample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel>

        <DataGrid Grid.ColumnSpan="2" Grid.Row="1" ItemsSource="{Binding Things}" AutoGenerateColumns="False" 
                  Height="250"
                  CanUserAddRows="False"
                  CanUserDeleteRows="False"
                  CanUserReorderColumns="False"
                  CanUserResizeColumns="False"
                  CanUserResizeRows="False"
                  CanUserSortColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Name" IsReadOnly="True" Binding="{Binding Name}" />
                <DataGridTextColumn Header="Value" IsReadOnly="True" Binding="{Binding Value}" />                
            </DataGrid.Columns>
        </DataGrid>

        <Button Click="RightButton_Click">Right</Button>
        <Button Click="WrongButton_Click">Wrong</Button>
    </StackPanel>
</Window>

MainWindow.xaml.cs:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ThreadingSample
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public ObservableCollection<Thing> Things { get; private set; }

        public MainWindow()
        {
            InitializeComponent();

            this.DataContext = this;

            Things = new ObservableCollection<Thing>();

            for (int i = 0; i < 200; i++)
            {
                Things.Add(new Thing(i));
            }
        }

        private async void RightButton_Click(object sender, RoutedEventArgs e)
        {
            foreach (var t in Things)
            {
                await Task.Run(() => t.Parse());
            }
        }

        private async void WrongButton_Click(object sender, RoutedEventArgs e)
        {
            foreach (var t in Things)
            {
                await t.ParseAsync();
            }            
        }
    }
}

东西.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ThreadingSample
{
    public class Thing : INotifyPropertyChanged
    {
        private string _name;

        public string Name
        {
            get { return _name; }
            set
            {
                _name = value;
                RaisePropertyChanged("Name");
            }
        }

        private int _value;

        public int Value
        {
            get { return _value; }
            set
            {
                _value = value;
                RaisePropertyChanged("Value");
            }
        }

        public Thing(int number)
        {
            Name = "Thing nr. " + number;
            Value = 0;
        }

        public void Parse()
        {
            var progressReporter = new Progress<int>(ReportProgress);
            HeavyParseMethod(progressReporter);
        }

        public async Task ParseAsync()
        {
            var progressReporter = new Progress<int>(ReportProgress);
            await HeavyParseMethodAsync(progressReporter);
        }

        private void HeavyParseMethod(IProgress<int> progressReporter)
        {
            for (int i = 0; i < 1000; i++)
            {
                Thread.Sleep(10);
                progressReporter.Report(i);
            }
        }

        private async Task HeavyParseMethodAsync(IProgress<int> progressReporter)
        {
            await Task.Run(() =>
                {
                    for (int i = 0; i < 1000; i++)
                    {
                        Thread.Sleep(100);
                        progressReporter.Report(i);
                    }
                });
        }

        private void ReportProgress(int progressValue)
        {
            this.Value = progressValue;
        }

        private void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}

我可以说,这个示例和我的实际代码之间的唯一区别是,我的实际代码使用 LINQ to XML 解析一堆 40mb xml 文件,而这个示例只调用 Thread.Sleep()。

编辑2:我找到了一个可怕的解决方法。如果我使用第二种方法并在解析每个产品之后调用 Thread.Sleep(1) 并在调用 IProgress.Report() 之前,一切正常。我可以看到“NumParsedProducts”计数器增加和一切。这是一个可怕的黑客虽然。这意味着什么?

4

2 回答 2

2

每次调用时,progress.Report(...)您实际上是在向 UI 线程发布消息以更新 UI,并且因为您在紧密循环中调用它,所以您只是在 UI 线程中充斥着它需要处理的报告消息,因此没有时间来处理做任何其他事情(并因此锁定)。这就是您Thread.Sleep(1)的“hack”起作用的原因,因为您正在给 UI 线程时间赶上。

您需要重新考虑回报告的方式,或者至少重新考虑回帖的频率。您可以使用许多缓冲回发的技术。我会使用Reactive Extensions的解决方案

于 2013-05-22T15:13:23.177 回答
-2

从事件处理程序调用异步方法时,您正在使用 await。这会导致事件处理程序线程等待(不执行任何操作)直到异步方法完成。
来自http://msdn.microsoft.com/en-us/library/vstudio/hh156528.aspx
await 运算符应用于异步方法中的任务,以暂停该方法的执行,直到等待的任务完成。

于 2013-05-21T08:35:13.687 回答