来自 Async Worker 的 UWP 更新 UI

Mic*_*hem 2 c# user-interface multithreading async-await uwp

我正在尝试实施一个长期运行的后台进程,定期报告其进度,以更新 UWP 应用程序中的 UI。我怎样才能做到这一点?我看过几个有用的主题,但没有一个包含所有部分,而且我无法将它们全部放在一起。

例如,考虑一个用户选择了一个非常大的文件,并且应用程序正在读取和/或操作文件中的数据。用户单击一个按钮,该按钮将使用用户选择的文件中的数据填充存储在页面上的列表。

第1部分

页面和按钮的点击事件处理程序如下所示:

public sealed partial class MyPage : Page
{
    public List<DataRecord> DataRecords { get; set; }

    private DateTime LastUpdate;

    public MyPage()
    {
        this.InitializeComponent();

        this.DataRecords = new List<DataRecord>();
        this.LastUpdate = DateTime.Now;

        // Subscribe to the event handler for updates.
        MyStorageWrapper.MyEvent += this.UpdateUI;
    }

    private async void LoadButton_Click(object sender, RoutedEventArgs e)
    {
        StorageFile pickedFile = // … obtained from FileOpenPicker.

        if (pickedFile != null)
        {
            this.DataRecords = await MyStorageWrapper.GetDataAsync(pickedFile);
        }
    }

    private void UpdateUI(long lineCount)
    {
        // This time check prevents the UI from updating so frequently
        //    that it becomes unresponsive as a result.
        DateTime now = DateTime.Now;
        if ((now - this.LastUpdate).Milliseconds > 3000)
        {
            // This updates a textblock to display the count, but could also
            //    update a progress bar or progress ring in here.
            this.MessageTextBlock.Text = "Count: " + lineCount;

            this.LastUpdate = now;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

MyStorageWrapper班级内部:

public static class MyStorageWrapper
{
    public delegate void MyEventHandler(long lineCount);
    public static event MyEventHandler MyEvent;

    private static void RaiseMyEvent(long lineCount)
    {
        // Ensure that something is listening to the event.
        if (MyStorageWrapper.MyEvent!= null)
        {
            // Call the listening event handlers.
            MyStorageWrapper.MyEvent(lineCount);
        }
    }

    public static async Task<List<DataRecord>> GetDataAsync(StorageFile file)
    {
        List<DataRecord> recordsList = new List<DataRecord>();

        using (Stream stream = await file.OpenStreamForReadAsync())
        {
            using (StreamReader reader = new StreamReader(stream))
            {
                while (!reader.EndOfStream)
                {
                    string line = reader.ReadLine();

                    // Does its parsing here, and constructs a single DataRecord …

                    recordsList.Add(dataRecord);

                    // Raises an event.
                    MyStorageWrapper.RaiseMyEvent(recordsList.Count);
                }
            }
        }

        return recordsList;
    }
}
Run Code Online (Sandbox Code Playgroud)

对于时间的代码检查我从下面得到这个

正如所写,此代码使应用程序对大文件无响应(我在大约 850 万行的文本文件上进行了测试)。我认为在通话中添加async和会阻止这种情况?这不是在 UI 线程之外的线程上工作吗?通过 Visual Studio 中的调试模式,我已验证程序按预期进行……它只是占用了 UI 线程,使应用程序无响应(请参阅Microsoft 的有关 UI 线程和异步编程的此页面)。awaitGetDataAsync()

第2部分

我已经在一个异步的、长时间运行的进程之前成功实现,该进程在一个单独的线程上运行并且仍然定期更新 UI……但是这个解决方案不允许返回值 - 特别是第 1 部分中的那一行:

this.DataRecords = await MyStorageWrapper.GetDataAsync(pickedFile);
Run Code Online (Sandbox Code Playgroud)

我之前的成功实施如下(为了简洁起见,大多数机构都被删掉了)。有没有办法调整它以允许返回值?

在一个Page班级中:

public sealed partial class MyPage : Page
{
    public Generator MyGenerator { get; set; }

    public MyPage()
    {
        this.InitializeComponent();

        this.MyGenerator = new Generator();
    }

    private void StartButton_Click(object sender, RoutedEventArgs e)
    {
        this.MyGenerator.ProgressUpdate += async (s, f) => await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, delegate ()
        {
            // Updates UI elements on the page from here.
        }

        this.MyGenerator.Start();
    }

    private void StopButton_Click(object sender, RoutedEventArgs e)
    {
        this.MyGenerator.Stop();
    }
}
Run Code Online (Sandbox Code Playgroud)

Generator课堂上:

public class Generator
{
    private CancellationTokenSource cancellationTokenSource;

    public event EventHandler<GeneratorStatus> ProgressUpdate;

    public Generator()
    {
        this.cancellationTokenSource = new CancellationTokenSource();
    }

    public void Start()
    {
        Task task = Task.Run(() =>
        {
            while(true)
            {
                // Throw an Operation Cancelled exception if the task is cancelled.
                this.cancellationTokenSource.Token.ThrowIfCancellationRequested();

                // Does stuff here.

                // Finally raise the event (assume that 'args' is the correct args and datatypes).
                this.ProgressUpdate.Raise(this, new GeneratorStatus(args));
            }
        }, this.cancellationTokenSource.Token);
    }

    public void Stop()
    {
        this.cancellationTokenSource.Cancel();
    }
}
Run Code Online (Sandbox Code Playgroud)

最后,该ProgressUpdate事件有两个支持类:

public class GeneratorStatus : EventArgs
{
    // This class can contain a handful of properties; only one shown.
    public int number { get; private set; }

    public GeneratorStatus(int n)
    {
        this.number = n;
    }
}

static class EventExtensions
{
    public static void Raise(this EventHandler<GeneratorStatus> theEvent, object sender, GeneratorStatus args)
    {
        theEvent?.Invoke(sender, args);
    }
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*und 5

关键是要理解这async/await并没有直接说等待的代码将在不同的线程上运行。当您执行时await GetDataAsync(pickedFile);,执行GetDataAsync仍会在 UI 线程上进入该方法,并在那里继续直到await file.OpenStreamForReadAsync()到达 - 这是实际在不同线程上异步运行的唯一操作(file.OpenStreamForReadAsync实际上是通过这种方式实现的)。

但是,一旦OpenStreamForReadAsync完成(这将非常快),请await确保执行返回到它开始的同一个线程 - 这意味着UI 线程。所以你的代码的实际昂贵部分(读取 中的文件while)在 UI 线程上运行。

您可以通过使用稍微改进这一点reader.ReadLineAsync,但是,您仍然会在每个await.

ConfigureAwait(false)

您要介绍的解决此问题的第一个技巧是ConfigureAwait(false).

在异步调用上调用它告诉运行时执行不必返回到最初调用异步方法的线程 - 因此这可以避免将执行返回到 UI 线程。把它放在你的情况下的好地方是OpenStreamForReadAsyncReadLineAsync调用:

public static async Task<List<DataRecord>> GetDataAsync(StorageFile file)
{
    List<DataRecord> recordsList = new List<DataRecord>();

    using (Stream stream = await file.OpenStreamForReadAsync().ConfigureAwait(false))
    {
        using (StreamReader reader = new StreamReader(stream))
        {
            while (!reader.EndOfStream)
            {
                string line = await reader.ReadLineAsync().ConfigureAwait(false);

                // Does its parsing here, and constructs a single DataRecord …

                recordsList.Add(dataRecord);

                // Raises an event.
                MyStorageWrapper.RaiseMyEvent(recordsList.Count);
            }
        }
    }

    return recordsList;
}
Run Code Online (Sandbox Code Playgroud)

调度员

现在您释放了 UI 线程,但又引入了进度报告的另一个问题。因为现在MyStorageWrapper.RaiseMyEvent(recordsList.Count)在不同的线程上运行,你不能更新UIUpdateUI直接方法,从非UI线程访问UI元素抛出同步异常。相反,您必须使用 UI 线程Dispatcher来确保代码在正确的线程上运行。

在构造函数中获取对 UI 线程的引用Dispatcher

private CoreDispatcher _dispatcher;

public MyPage()
{
    this.InitializeComponent();
    _dispatcher = Window.Current.Dispatcher;

    ...
}
Run Code Online (Sandbox Code Playgroud)

提前这样做的原因是它Window.Current再次只能从 UI 线程访问,但页面构造函数肯定会在那里运行,因此它是理想的使用场所。

现在改写UpdateUI如下

private async void UpdateUI(long lineCount)
{
    await _dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
    {
       // This time check prevents the UI from updating so frequently
       //    that it becomes unresponsive as a result.
       DateTime now = DateTime.Now;
       if ((now - this.LastUpdate).Milliseconds > 3000)
       {
           // This updates a textblock to display the count, but could also
           //    update a progress bar or progress ring in here.
           this.MessageTextBlock.Text = "Count: " + lineCount;

           this.LastUpdate = now;
       }
    });
}
Run Code Online (Sandbox Code Playgroud)

  • 你是对的,我的坏:-)。我主要是从记忆中写了那部分 :-D 。很高兴它有所帮助,快乐编码! (2认同)