简体   繁体   中英

Asserts not working in Visual Studio 2010 in a C# WPF application

I don't know if it's a setting that I've turned on or off, or if there's code that I'm somehow linking in that's turning it off, but Debug.Assert isn't stopping execution anymore. All I get is a stack trace in the Output window that looks like:

The thread '<No Name>' (0x155c) has exited with code 0 (0x0).
The thread '<No Name>' (0x24f4) has exited with code 0 (0x0).
---- DEBUG ASSERTION FAILED ----
---- Assert Short Message ----

---- Assert Long Message ----


    at ...
    at CommandBinding.OnExecuted(Object sender, ExecutedRoutedEventArgs e)  
    at CommandManager.ExecuteCommandBinding(Object sender, ExecutedRoutedEventArgs e, CommandBinding commandBinding)  
    at CommandManager.FindCommandBinding(CommandBindingCollection commandBindings, Object sender, RoutedEventArgs e, ICommand command, Boolean execute)  
    at CommandManager.FindCommandBinding(Object sender, RoutedEventArgs e, ICommand command, Boolean execute)  
    at CommandManager.OnExecuted(Object sender, ExecutedRoutedEventArgs e)  
    at RoutedEventArgs.InvokeHandler(Delegate handler, Object target)  
    at EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)  
    at UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)  
    at RoutedCommand.ExecuteImpl(Object parameter, IInputElement target, Boolean userInitiated)  
    at MenuItem.InvokeClickAfterRender(Object arg)  
    at ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)  
    at ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)  
    at Dispatcher.WrappedInvoke(Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)  
    at DispatcherOperation.InvokeImpl()  
    at ExecutionContext.runTryCode(Object userData)  
    at RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)  
    at ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)  
    at ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)  
    at DispatcherOperation.Invoke()  
    at Dispatcher.ProcessQueue()  
    at Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)  
    at HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)  
    at HwndSubclass.DispatcherCallbackOperation(Object o)  
    at ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)  
    at ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)  
    at Dispatcher.WrappedInvoke(Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)  
    at Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)  
    at HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)  
    at UnsafeNativeMethods.DispatchMessage(MSG& msg)  
    at Dispatcher.PushFrameImpl(DispatcherFrame frame)  
    at Application.RunInternal(Window window)  
    at Application.Run()  
    at App.Main()
    at AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)  
    at HostProc.RunUsersAssembly()  
    at ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)  
    at ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)  
    at ThreadHelper.ThreadStart()  

Stats:

  • Visual Studio 2010
  • WPF application
  • C#
  • .Net 4.0

Does anyone know why no "Assertion failed" messagebox popped up?

From the MSDN page on Debug.Assert

The display of the message box depends on the presence of the DefaultTraceListener. If the DefaultTraceListener is not in the Listeners collection, the message box is not displayed. The DefaultTraceListener can be removed by the Element for for , the Element for for , or by calling the Clear method on the Listeners property (System.Diagnostics.Trace.Listeners.Clear()).

That said, the implementation of a message box that can be pumping messages when there's an assert has always bothered me. The .Net one I think tends to work pretty well, but I've lost unrecoverable state because of that kind of thing in other contexts. For my own .Net projects I tend to use my own static Assert class that doesn't do that. Instead I call Debugger.Break() when my Assertions fail.

eg

internal static class Assert 
{
    [Conditional("DEBUG")]
    public static void IsNotNull<T>(T obj) where T : class
    {
        if (obj == null)
            System.Diagnostics.Debugger.Break();
    }
}

Exceptions inside a Binding are treated differently. You will see Binding errors (exceptions) int the output window only too. This is a variation.

You can throw a test Exception somewhere else to check your settings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM