Debug.Fail 给我一个“调度程序处理已暂停”的消息错误
我的代码出现问题,导致 Debug.Fail 被调用。我经常使用 Debug.Assert 和 Debug.Fail,因为它不仅可以立即通知我存在问题,还可以提供有关问题所在的内容和位置的信息。
但这一次 Debug.Fail 本身导致了一个更大的问题,因为 UI (WPF) 由于以下错误而冻结:
调度程序处理已暂停,但消息仍在处理中。
我能做些什么来使 Debug.Fail 成功吗? 我绝对不想让 Debug.Fail 周围的代码来检查是否要 Invoke 或 BeginInvoke。
编辑:我想替换 DefaultTraceListener 就可以解决问题吗?
I had a problem in code that caused a Debug.Fail to be called. I use Debug.Assert and Debug.Fail quite a lot, since it not only gives me instant notification that a problem exists, but also information about what and where the problem is located.
But this time the Debug.Fail itself caused an even bigger problem, since the UI (WPF) froze becuse of the following error:
Dispatcher processing has been suspended, but messages are still being processed.
Is there anything I can do to make Debug.Fail succeed? I most definitely do not want to have code around by Debug.Fail to check whether to Invoke or BeginInvoke.
EDIT: I suppose replacing the DefaultTraceListener would do the trick?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来您正在从 DependencyProperty 更改回调中进行 Debug.Fail。
您可以在这里找到完整的答案:调用 ShowDialog 时出现“调度程序处理已暂停”InvalidOperationException
Looks like you're Debug.Fail from DependencyProperty change callback.
Full answer you could find here: 'Dispatcher processing has been suspended' InvalidOperationException when calling ShowDialog
Debug.Fail
默认情况下将使用DefaultTraceListener
。使用 ILSpy,我可以看到DefaultTraceListener.Fail
调用了AssertWrapper.ShowAssert
。要解决此问题,请将DefaultTraceListener
替换为您自己的,在对AssertWrapper.ShowAssert
的调用周围有 Dispatcher 防护。瞧!现在,您可以调用
Debug.Assert
和Debug.Fail
,而无需担心调度程序问题。Debug.Fail
will by default use theDefaultTraceListener
. Using ILSpy, I could see thatDefaultTraceListener.Fail
makes a call toAssertWrapper.ShowAssert
. To get around the problem, replace theDefaultTraceListener
with your own that has Dispatcher guards around the call toAssertWrapper.ShowAssert
.Voila! Now you can call
Debug.Assert
andDebug.Fail
without bothering about dispatcher issues.