gpt4 book ai didi

WPF 应用程序在 Dispatcher 上调用时死锁

转载 作者:行者123 更新时间:2023-12-03 22:45:34 25 4
gpt4 key购买 nike

我们偶尔会遇到应用程序死锁的情况,并且调度程序似乎因试图在调度程序上调用的后台线程而死锁。我没有看到任何一个线程都有任何被锁定的共享资源。后台线程遇到了一个异常,它在应用程序域未处理异常委托(delegate)处结束,因为没有人收到这个异常。这将调用我们的异常处理程序,该处理程序的任务是确保将我们的异常对话框放到调度程序上。

有人可以建议我找出导致僵局的原因的方法吗?

调度程序堆栈紧随其后,看起来并没有什么特别之处:

*0. System.Windows.Threading.DispatcherSynchronizationContext.Wait (source line information unavailable)

1. System.Threading.SynchronizationContext.InvokeWaitMethodHelper (source line information unavailable)
2. Xceed.Wpf.DataGrid.DeferredOperationManager.Process (source line information unavailable)
3. Xceed.Wpf.DataGrid.DeferredOperationManager.Dispatched_Process (source line information unavailable)
4. System.Windows.Threading.ExceptionWrapper.InternalRealCall (source line information unavailable)
5. System.Windows.Threading.ExceptionWrapper.TryCatchWhen (source line information unavailable)
6. System.Windows.Threading.Dispatcher.WrappedInvoke (source line information unavailable)
7. System.Windows.Threading.DispatcherOperation.InvokeImpl (source line information unavailable)
8. System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext (source line information unavailable)
9. System.Threading.ExecutionContext.runTryCode (source line information unavailable)
10. System.Threading.ExecutionContext.RunInternal (source line information unavailable)
11. System.Threading.ExecutionContext.Run (source line information unavailable)
12. System.Windows.Threading.DispatcherOperation.Invoke (source line information unavailable)
13. System.Windows.Threading.Dispatcher.ProcessQueue (source line information unavailable
14. System.Windows.Threading.Dispatcher.WndProcHook (source line information unavailable)
15. MS.Win32.HwndWrapper.WndProc (source line information unavailable)
16. MS.Win32.HwndSubclass.DispatcherCallbackOperation (source line information unavailable)
17. System.Windows.Threading.ExceptionWrapper.InternalRealCall (source line information unavailable)
18. System.Windows.Threading.ExceptionWrapper.TryCatchWhen (source line information unavailable)
19. System.Windows.Threading.Dispatcher.WrappedInvoke (source line information unavailable)
20. System.Windows.Threading.Dispatcher.InvokeImpl (source line information unavailable)
21. System.Windows.Threading.Dispatcher.Invoke (source line information unavailable)
22. MS.Win32.HwndSubclass.SubclassWndProc (source line information unavailable)
[Internal Frame, 'M-->U']
23. System.Windows.Threading.Dispatcher.PushFrameImpl (source line information unavailable)
24. System.Windows.Threading.Dispatcher.PushFrame (source line information unavailable)
25. System.Windows.Threading.Dispatcher.Run (source line information unavailable)
26. System.Windows.Application.RunDispatcher (source line information unavailable)
27. System.Windows.Application.RunInternal (source line information unavailable)
28. System.Windows.Application.Run (source line information unavailable)
29. System.Windows.Application.Run (source line information unavailable)
30. Wmc.Gtseq.Client.Desktop.App.Main (source line information unavailable)

第二个线程堆栈基本上从应用程序域未处理的异常处理程序开始:
*0. System.Threading.WaitHandle.WaitOne (source line information unavailable)

1. System.Threading.WaitHandle.WaitOne (source line information unavailable)
2. System.Windows.Threading.DispatcherOperation+DispatcherOperationEvent.WaitOne (source line information unavailable)
3. System.Windows.Threading.DispatcherOperation.Wait (source line information unavailable)
4. System.Windows.Threading.Dispatcher.InvokeImpl (source line information unavailable)
5. System.Windows.Threading.Dispatcher.Invoke (source line information unavailable)
6. Wmc.Gtseq.Core.ForwardPort.Extensions.DispatcherExtension.InvokeIfRequired (source line information unavailable)
7. Wmc.Gtseq.Core.ForwardPort.Utilities.DispatcherHelper.InvokeOnMainThread (source line information unavailable)
8. Wmc.Gtseq.Core.ForwardPort.Handlers.ExceptionHandler.ThreadSafeDialogHandler (source line information unavailable)
9. Wmc.Gtseq.Core.ForwardPort.Handlers.ExceptionHandler.ShowErrorDialog (source line information unavailable)
10. Wmc.Gtseq.Core.ForwardPort.Handlers.ExceptionHandler.HandleException (source line information unavailable)
11. Wmc.Gtseq.Client.Desktop.App.AppDomainUnhandledException (source line information unavailable)

看起来 Invoke 正在按预期等待,但调度程序线程本身似乎也被阻塞了。在这些情况下,我们已经等了很多分钟,应用程序再也没有回来。任何帮助或见解将不胜感激。我知道我可以切换到 BeginInvoke,但根据这里的上下文,我担心我的后台线程会继续,并且 UI 会因为同样的原因被阻止,或者不会出现异常对话框。

当异常出现在域未处理的异常处理程序中时,我们的后台线程执行以下代码流:
protected override void AppDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
ExceptionHandler.HandleException(e.ExceptionObject as Exception, false);
}

public static void HandleException(Exception ex, bool closeApp)
{
ThreadSafeDialogHandler((Action)delegate { ErrorDialog.ShowDialog(ex, closeApp); });
}

private static void ThreadSafeDialogHandler(Action methodCall)
{
DispatcherHelper.InvokeOnMainThread(() => { methodCall(); });
}

public static void InvokeOnMainThread(Action method)
{
Application.Current.InvokeIfRequired(method, DispatcherPriority.Normal);
}

public static void InvokeIfRequired(this DispatcherObject control, Action methodcall, DispatcherPriority priorityForCall)
{
// see if we need to Invoke call to Dispatcher thread
if (control.Dispatcher.CheckAccess())
{
methodcall();
}
else
{
control.Dispatcher.Invoke(priorityForCall, methodcall);
}
}

最佳答案

而不是 control.Dispatcher.Invoke试试control.Dispatcher.BeginInvoke ,在这种极端情况下,这对我有所帮助。

另外,您的代码对我来说似乎有点奇怪。在我的应用程序中,我设置了 AppDomain.CurrentDomain.UnhandledException主窗口中的事件处理程序 Loaded()事件。该事件附加到主窗口的本地方法。因此,我不需要执行任何 Dispatcher 调用,因为该事件已经在主调度程序线程上引发,即使错误确实发生在另一个线程上。

关于WPF 应用程序在 Dispatcher 上调用时死锁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9282235/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com