如何防止调试器在暂停时调用某些源代码? [英] How to prevent the debugger from calling some source code when paused?

查看:38
本文介绍了如何防止调试器在暂停时调用某些源代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Visual Studio 中,当应用程序在调试模式下停止时,您可以将鼠标悬停在对象/属性上以查看其中的内容.

In visual studio, when an application is stopped during debug mode you can hover over an object/property to see whats inside of it.

当您使用调试器打开一个对象时,就像我对上图所做的那样,该属性会调用 Get 方法,作为调试器检索属性值以向用户显示的一种方式.

When you open up an object using the debugger like I did with the above picture, the property has it's Get method called as a way for the debugger to retrieve the value of the property to show the user.

class Program
{
        static void Main(string[] args)
    {
        Foo foo = new Foo();

        foo.MyLock.EnterWriteLock();
        foo.Bar = 5.1;
        foo.MyLock.ExitWriteLock();

        // "I stop here and attempt to see what the value of Bar is via the debugger."
        foo.MyLock.EnterReadLock();
        Console.WriteLine(foo.Bar);
        foo.MyLock.ExitReadLock();
    }
}

class Foo
{
    private double bar;
    public double Bar 
    {
        get
        {
            Console.WriteLine(MyLock.IsReadLockHeld);
            Debug.Assert(MyLock.IsReadLockHeld, "Please enter the read lock before attempting to read this property.");
            return bar;
        }
        set
        {
            Debug.Assert(MyLock.IsWriteLockHeld, "Please enter the write lock before attempting to write this property.");
            bar = value;
        }
    }

    public ReaderWriterLockSlim MyLock { get; set; }

    public Foo()
    {
        MyLock = new ReaderWriterLockSlim();
    }
}

在我的应用程序中,我将 Debug.Assert() 调用添加到我的 Get 访问器中,以确保此示例中的 Foo 已被锁定.每当我的代码调用 Bar 时,都应该按照设计锁定 foo,但是当调试器尝试查看 bar 时,foo 不会被锁定,这意味着断言应该失败.

In my application I add in a Debug.Assert() call into my Get accessors to make sure that Foo in this example has been locked. Whenever my code called Bar, foo should be locked as per design, however when the debugger attempts to view bar, foo won't be locked which means the assert should fail.

当调试器遇到失败的断言时,它有时会抑制断言弹出窗口,有时它会按照正常的失败断言行为显示断言弹出窗口.尽我所能告诉 Assert 弹出窗口似乎在前 1-2 次调试器查看 Bar 的值时被抑制,但每次在前 1-2 次之后都允许显示弹出窗口.虽然这是断言抑制的最常见行为,但情况并非总是如此,因为在应用程序的其他运行中,无论调试器查看 Bar 多少次,调试器都不会停止抑制.

When the debugger encounters an assert that fails it will sometimes suppress the assert popup and other times it will show the assert pop-up as per normal failed assert behavior. As best as I can tell the Assert popup appears to be suppressed for the first 1-2 times the debugger views the value of Bar but every time after those first 1-2 the popup is allowed to show. While this is the most common behavior of the assert suppressing this is not always the case because on other runs of the application the debugger never stopped suppressing no matter how many times the debugger viewed Bar.

问题:对于我的应用程序,期望的行为是断言在 100% 的时间内被抑制.我如何实现这一目标?

Question: For my application the desired behavior would be for the asserts to be suppressed 100% of the time. How do I achieve this?

此外,如果它在调试器命中其中一个断言时有帮助并且它使以下消息失败,则它会写入调试输出.无论断言是否被抑制,这都是完全相同的消息.

Also, if it helps when the debugger hits one of the asserts and it fails the below message it written to the Debug output. This is the exact same message regardless of whether or not the assert is suppressed.

---- DEBUG ASSERTION FAILED ----
---- Assert Short Message ----
Please enter the read lock before attempting to read this property.
---- Assert Long Message ----

   at TestApp_Debugging.Foo.get_Bar() in c:\Users\Adrian.vanBerkhout\Documents\Visual Studio 2013\Projects\TestApp_Debugging\TestApp_Debugging\Program.cs:line 37
   at TestApp_Debugging.Program.Main(String[] args) in c:\Users\Adrian.vanBerkhout\Documents\Visual Studio 2013\Projects\TestApp_Debugging\TestApp_Debugging\Program.cs:line 17
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

推荐答案

我找到了解决方案 此处.使用 DebuggerBrowsable 装饰您的问题属性 属性.

I found a solution here. Decorate your problem properties with the DebuggerBrowsable attribute.

[DebuggerBrowsable(DebuggerBrowsableState.Never)]

理想情况下,您永远不需要这样做.但是,我们有一些属性在评估时(在调试中)会触发代码契约异常,这会导致 Visual Studio 变得非常困惑.您可能需要修饰类的属性和它实现的任何接口.

Ideally you'd never need to do this. However, we have some properties that trigger a code contract exception when evaluated (in debug), which causes Visual Studio to get very confused. You probably need to decorate both the property on the class and any interface it implements.

这篇关于如何防止调试器在暂停时调用某些源代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆