Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在调度程序上调用时,WPF应用程序处于死锁状态_Wpf_Deadlock_Dispatcher - Fatal编程技术网

在调度程序上调用时,WPF应用程序处于死锁状态

在调度程序上调用时,WPF应用程序处于死锁状态,wpf,deadlock,dispatcher,Wpf,Deadlock,Dispatcher,我们偶尔会遇到应用程序死锁的情况,并且似乎调度程序与试图在调度程序上调用的后台线程死锁。我看不出这两个线程都有任何被锁定的共享资源。后台线程遇到异常,并在应用程序域未处理异常委托处结束,因为没有人拾取此异常。这将调用异常处理程序,该异常处理程序的任务是确保将异常对话框放在调度程序上 有人能提出一些方法让我找出是什么导致了僵局吗 dispatcher堆栈如下所示,看起来并不与众不同: *0. System.Windows.Threading.DispatcherSynchronizationCon

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

有人能提出一些方法让我找出是什么导致了僵局吗

dispatcher堆栈如下所示,看起来并不与众不同:

*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)
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);
        }
    }
调用似乎正在按预期等待,但调度程序线程本身似乎已被阻止。在这种情况下,我们已经等待了很多分钟,应用程序再也没有回来。如有任何帮助或见解,将不胜感激。我知道我可以切换到BeginInvoke,但基于这里的上下文,我担心我的后台线程会继续,UI会因为同样的原因被阻止,或者异常对话框不会出现

当域未处理异常处理程序中出现异常时,我们的后台线程执行以下代码流:

*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)
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
,这在以前的这种情况下对我很有帮助


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

我发布了从应用程序域未处理异常处理程序开始执行的代码。在死锁处于活动状态时,查看UI线程的堆栈跟踪会更有趣。UI线程可能正在等待某些东西,因此调度程序被阻塞。下次发生死锁时中断调试器,并检查UI线程的堆栈跟踪。可能是您的UI线程位于thread.Join(或类似的东西)中,而您的后台线程试图在dispatcher上调用?由于死锁,我很久以前就停止使用Invoke了。。我的ui线程发布在上面。它是显示的第一个堆栈。对于Invoke和BeginInvoke,我的理解是Invoke最终会调用Begin,但也会有处理等待。这不是真的吗?Invoke是否试图在BeginInvoke发送到队列时直接中断线程?@Ben。你有没有解决过这个问题?我也得到了它,使用Invoke或BeginInvoke也没什么区别。@Jim-老实说,我不记得了。从那以后发生了很多事情。不过有两点。1.正在触发未处理的异常,因为第三方组件正在生成自己的线程,并且该线程正在引发异常。我们无法捕获它,因此它最终位于应用程序域。2.我们已经更改了代码,现在只调用Application.Current.invokeIQuered。大概其中一个解决了这个问题。我希望我有更具体的信息。从
Invoke
切换到
BeginInvoke
并不总是可能的。前者是同步的,后者是异步的。如果被调用方依赖于被调用调用的调用的效果,那么就很容易了。