Wpf 当使用TaskScheduler.FromCurrentSynchronizationContext在任务连续中的ShowDialog中打开表单时,应用程序get冻结
在我的WPF应用程序中,我使用任务并行性来增强多任务处理和UI响应能力,但我面临应用程序冻结的问题 以下是我正在使用的代码: MainForm.csWpf 当使用TaskScheduler.FromCurrentSynchronizationContext在任务连续中的ShowDialog中打开表单时,应用程序get冻结,wpf,task-parallel-library,showdialog,Wpf,Task Parallel Library,Showdialog,在我的WPF应用程序中,我使用任务并行性来增强多任务处理和UI响应能力,但我面临应用程序冻结的问题 以下是我正在使用的代码: MainForm.cs Storyboard board = (Storyboard)this.FindResource("StoryboardLoadingAnimation"); board.Begin(this, true); this.Cursor = Cursors.Wait; Task.Factory.StartNew(() => {
Storyboard board = (Storyboard)this.FindResource("StoryboardLoadingAnimation");
board.Begin(this, true);
this.Cursor = Cursors.Wait;
Task.Factory.StartNew(() =>
{
//Long Running Task
}).ContinueWith((prev) =>
{
if (board != null)
{
board.Stop(this);
}
this.Cursor = Cursors.Arrow;
this.popupOverlayHelper.Visibility = Visibility.Visible; // Adding overlay on main form by giving opacity and background color to popupOverlayHelper element
NotificationPopup notification = new NotificationPopup(this, NotificationsType.Info); // Another WPF Window form
notification.Owner = this;
notification.ShowDialog();
this.popupOverlayHelper.Visibility = Visibility.Collapsed;
}, TaskScheduler.FromCurrentSynchronizationContext());
NotificationPopup.xaml
<Window x:Class="Application.TrackOFF_NotificationPopup"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Application"
mc:Ignorable="d" Title="NotificationPopup"
WindowStyle="None" AllowsTransparency="True" ResizeMode="NoResize"
WindowStartupLocation="CenterOwner"
FontFamily="/TrackOFFApplication;component/Fonts/#Segoe UI"
Width="1024px" Height="640px" Background="{x:Null}"
MouseLeftButtonDown="NotificationPopup_MouseLeftButtonDown">
<Grid Margin="240px,68px,0,0">
<!--Other xaml code to show notification -->
</Grid>
</Window>
此NotificationPopup中没有任何复杂的功能,它只是向用户显示通知消息,单击右上角的X,“CloseMe”函数将获得调用,从而关闭弹出窗口并再次呈现主窗体
这里的问题是,,
当通知弹出窗口保持打开状态超过几秒钟时,应用程序就会冻结,或者我玩弹出窗口(比如将弹出窗口屏幕拖到此处)
有趣的是,如果我从代码中删除任务,那么应用程序就不会冻结并按预期工作。
但我真的不想从代码中删除任务,因为它有长时间运行的任务要执行,我想释放UI线程并向用户显示动画,直到长时间运行的任务完成他的工作
您的宝贵反馈将不胜感激
谢谢 这里不需要使用
Task.Factory.StartNew
wait
长时间运行的任务和方法的其余部分将在捕获的上下文上运行
我假设您的代码来自加载的窗口
或类似的事件处理程序:
private async void Window_Loaded(object sender, RoutedEventArgs e) {
Storyboard board = (Storyboard)this.FindResource("StoryboardLoadingAnimation");
board.Begin(this, true);
this.Cursor = Cursors.Wait;
//offload work from UI
await Task.Run(() => {
//Long Running Task
});
//work is complete, execution returns to UI context
if (board != null) {
board.Stop(this);
}
this.Cursor = Cursors.Arrow;
this.popupOverlayHelper.Visibility = Visibility.Visible; // Adding overlay on main form by giving opacity and background color to popupOverlayHelper element
NotificationPopup notification = new NotificationPopup(this, NotificationsType.Info); // Another WPF Window form
notification.Owner = this;
notification.ShowDialog();
this.popupOverlayHelper.Visibility = Visibility.Collapsed;
}
对于此类操作,您可能应该使用后台工作人员,因为您必须先阅读本文。对于大型计算任务,使用Task.Factory.StartNew,然后在UI线程上使用ContinueWith来处理响应和更新的UI元素,是否有任何问题。是的,正确使用
StarNew
请参阅。当async/await
通常使代码更易于维护、更易于推理和阅读时,没有什么好的理由使用旧的基于延续的技术。
private async void Window_Loaded(object sender, RoutedEventArgs e) {
Storyboard board = (Storyboard)this.FindResource("StoryboardLoadingAnimation");
board.Begin(this, true);
this.Cursor = Cursors.Wait;
//offload work from UI
await Task.Run(() => {
//Long Running Task
});
//work is complete, execution returns to UI context
if (board != null) {
board.Stop(this);
}
this.Cursor = Cursors.Arrow;
this.popupOverlayHelper.Visibility = Visibility.Visible; // Adding overlay on main form by giving opacity and background color to popupOverlayHelper element
NotificationPopup notification = new NotificationPopup(this, NotificationsType.Info); // Another WPF Window form
notification.Owner = this;
notification.ShowDialog();
this.popupOverlayHelper.Visibility = Visibility.Collapsed;
}