两个后台线程中使用同一个Dispatcher invoke进行dialog展示,其中dialog中设置定时关闭窗口,这时会导致进入死锁状态
调用dialog代码:
Dispatcher dispatcher = Application.Current.Dispatcher;
Task.Run(() =>
{
dispatcher.Invoke(() =>
{
_dialogService.ShowDialog("VerifyDialog", new DialogParameters(), null);
});
});
Task.Run(() =>
{
dispatcher.Invoke(() =>
{
_dialogService.ShowDialog("VerifyDialog", new DialogParameters(), null);
});
});
dialog定时关闭代码:
public async void AutoCloseDialog()
{
await Task.Delay(1000);
RequestClose?.Invoke(new DialogResult(ButtonResult.None));
}
Dispatcher 改成InvokeAsync并等待无效果,同样会进入死锁
Dispatcher dispatcher = Application.Current.Dispatcher;
Task.Run(() =>
{
dispatcher.InvokeAsync(() =>
{
_dialogService.ShowDialog("VerifyDialog", new DialogParameters(), null);
}).Wait();
});
Task.Run(() =>
{
dispatcher.InvokeAsync(() =>
{
_dialogService.ShowDialog("VerifyDialog", new DialogParameters(), null);
}).Wait();
});
目前没有找到很好的解决方法,只能设置一个全局变量对dialog是否关闭进行监控和阻塞
Dispatcher dispatcher = Application.Current.Dispatcher;
Task.Run(() =>
{
dispatcher.InvokeAsync(() =>
{
_dialogService.ShowDialog("VerifyDialog", new DialogParameters(), null);
});
while(!flag)
{
Thread.Sleep(10);
}
});
Task.Run(() =>
{
dispatcher.InvokeAsync(() =>
{
_dialogService.ShowDialog("VerifyDialog", new DialogParameters(), null);
}).Wait();
while (!flag)
{
Thread.Sleep(10);
}
});
public async void AutoCloseDialog()
{
await Task.Delay(1000);
flag = true;
RequestClose?.Invoke(new DialogResult(ButtonResult.None));
}