这是一段代码,我在长时间运行的任务中遇到了 UI 阻塞问题。我见过其他似乎做同样事情的例子,但这不起作用。我认为这很简单。
我错过了什么?
public class ClipManager
{
async public Task<int> AddNewClipsAsync()
{
int rc = AddNewClips(); // does the work
return rc;
}
}
public partial class MainWindow : Window
{
private async void UpdateClick(object sender, RoutedEventArgs e)
{
// Check event to update library from WindowsMain.xml.cs
private async void UpdateClick(object sender, RoutedEventArgs e)
{
UpdateBtn.IsEnabled = false;
Console.WriteLine("Start of Update");
var cm = ClipManagement.Create();
int rc = await cm.AddNewClipsAsync(); // long running method
// UI unfreezes at this point
Console.WriteLine("After Update of Update");
await Task.Delay(30000); // just debugging code
Console.WriteLine("After delay");
LibraryDG.Items.Refresh();
UpdateBtn.IsEnabled = true;
}
}
}