c# - 来自异步工作器的 UWP 更新 UI
问题描述
我正在尝试实现一个长期运行的后台进程,它会定期报告其进度,以更新 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;
}
}
}
课堂内部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;
}
}
我从以下得到的时间检查代码。
如所写,此代码使应用程序对大文件无响应(我在大约 850 万行的文本文件上进行了测试)。我认为在通话中添加async
and可以防止这种情况发生?这不是在 UI 线程之外的线程上工作吗?通过 Visual Studio 中的调试模式,我已验证程序正在按预期进行......它只是占用了 UI 线程,使应用程序无响应(请参阅Microsoft 的此页面关于 UI 线程和异步编程)。await
GetDataAsync()
第2部分
我已经在一个单独的线程上运行并且仍然定期更新 UI 的异步、长时间运行的进程之前成功实现了......但是这个解决方案不允许返回值 - 特别是第 1 部分中的行:
this.DataRecords = await MyStorageWrapper.GetDataAsync(pickedFile);
接下来是我之前的成功实施(为简洁起见,大部分正文都被删掉了)。有没有办法调整它以允许返回值?
在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();
}
}
在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();
}
}
最后,该事件有两个支持类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);
}
}
解决方案
关键是要理解,async/await
并不直接说等待的代码将在不同的线程上运行。当您执行时,仍然在 UI 线程上await GetDataAsync(pickedFile);
进入该方法并继续在那里直到到达 - 这是唯一将实际在不同线程上异步运行的操作(实际上是以这种方式实现的)。GetDataAsync
await file.OpenStreamForReadAsync()
file.OpenStreamForReadAsync
但是,一旦OpenStreamForReadAsync
完成(这将非常快),await
请确保执行返回到它开始的同一线程 - 这意味着UI 线程。因此,代码中实际昂贵的部分(在 中读取文件while
)在 UI 线程上运行。
您可以通过使用来稍微改善这一点reader.ReadLineAsync
,但仍然会在每个await
.
ConfigureAwait(false)
要解决此问题,您要介绍的第一个技巧是ConfigureAwait(false)
.
在异步调用上调用它会告诉运行时执行不必返回到最初调用异步方法的线程——因此这可以避免将执行返回到 UI 线程。将它放在您的案例中的好地方是OpenStreamForReadAsync
并ReadLineAsync
调用:
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;
}
调度员
现在您释放了您的 UI 线程,但在进度报告中引入了另一个问题。因为现在MyStorageWrapper.RaiseMyEvent(recordsList.Count)
在不同的线程上运行,所以不能直接在UpdateUI
方法中更新 UI,因为从非 UI 线程访问 UI 元素会引发同步异常。相反,您必须使用 UI 线程Dispatcher
来确保代码在正确的线程上运行。
在构造函数中获取对 UI 线程的引用Dispatcher
:
private CoreDispatcher _dispatcher;
public MyPage()
{
this.InitializeComponent();
_dispatcher = Window.Current.Dispatcher;
...
}
提前这样做的原因是它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;
}
});
}
推荐阅读
- wordpress - 如何将计算字段绑定到条带提要作为付款金额?
- amazon-web-services - 在“provisioner”块上的 AWS EC2 实例上运行 Hashicorp Vault 服务器
- html - 在角度 4 中单击时将活动类添加到子菜单
- javascript - Jquery 搜索可以在我的桌面设备和 chrome 移动模拟器上工作,但不能在真正的移动设备(IOS/Android)上工作
- r - 相关矩阵忽略 R 中每一对的特定 NA
- android - 如何在 react-native 中实现文本环绕视图
- python - 比较同一列表的 XY 坐标
- node.js - 如何实现“npm run build”,更改“config.js”,然后运行“npm run build”
- xml - 使用批处理代码批量格式化 xml?
- android - 将媒体控制器绑定到某个视图