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?
Debug.Failwill by default use theDefaultTraceListener. Using ILSpy, I could see thatDefaultTraceListener.Failmakes a call toAssertWrapper.ShowAssert. To get around the problem, replace theDefaultTraceListenerwith your own that has Dispatcher guards around the call toAssertWrapper.ShowAssert.Voila! Now you can call
Debug.AssertandDebug.Failwithout bothering about dispatcher issues.