在我的WPF应用程序中,我使用Task Parallelism来增强多任务处理和UI响应,但我面临着应用程序冻结的问题。
以下是我正在使用的代码:
MainForm.cs
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.xaml.cs
void NotificationPopup_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
try
{
DragMove();
}
catch (Exception ex)
{
Helper.WriteLogEntries(ex, "NotificationPopup => NotificationPopup_MouseLeftButtonDown");
}
}
private void CloseMe(object sender, RoutedEventArgs e)
{
try
{
this.Close();
if (IsParentToShowAgain)
(ParentPopup as Window).Show();
if (IsOverlayToKill)
{
(ParentPopup as MainForm).popupOverlayHelper.Visibility = Visibility.Collapsed;
}
}
catch (Exception ex)
{
Helper.WriteLogEntries(ex, "NotificationPopup => CloseMe");
}
}
此NotificationPopup中没有任何复杂的功能,只是向用户显示通知消息,点击右上角的X“CloseMe”函数进行调用,关闭弹出窗口并再次渲染MainForm。
这里的问题是, 当通知弹出保持打开超过几秒钟时,应用程序会冻结,或者我弹出窗口(比如将拖动弹出屏幕拖到那里)。
有趣的是,如果我从代码中删除任务,那么应用程序不会冻结并按预期工作。 但我真的不想从代码中删除Task,因为它有执行Long Running任务,我想释放UI线程并向用户显示动画,直到Long Running任务完成他的工作。
您的宝贵意见将不胜感激。
谢谢!
答案 0 :(得分:0)
此处无需使用Task.Factory.StartNew
。 await
您的长时间运行任务,您方法的其余部分将在捕获的上下文中运行。
我假设您的代码来自Window_Loaded
或类似的事件处理程序:
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;
}