0

我有一个问题,当涉及到可能使用其他线程的属性时,watch 表达式会立即中断。我不想收到消息“函数评估需要所有线程都运行”,但我仍然想使用不执行潜在“不安全”和持久属性的调试器功能。

我看到在即时窗口中输入表达式时抛出了 ThreadAbortException,因此我尝试处理此异常并在评估时继续执行 Watch Window 线程执行。但它没有用。

我有下一个情况:

public partial class DummyClassWithDebuglessProperties
{
    public static int countPrompt = 0;
    public static int countSuccess = 0;
    public static int countThreadAbortException = 0;

    #region Properties

    public string StringPropertyDebugFail
    {
        get
        {
            countPrompt++;
            Debugger.NotifyOfCrossThreadDependency();
            countSuccess++;
            return "complex";
        }
    }

    public string StringProperty
    {
        get
        {
            return "simple";
        }
    }

    public string AllProperties
    {
        get
        {
            string stringPropertyDebugFail = "";
            try
            {
                stringPropertyDebugFail = StringPropertyDebugFail;
            }
            catch (ThreadAbortException)
            {
                countThreadAbortException++;
                stringPropertyDebugFail = "failedReading";
                Thread.ResetAbort();
            }

            //expecting for watchWindow: simple, failedReading
            //expecting in regular execution: simple, complex
            return String.Format("{0}, {1}", StringProperty, stringPropertyDebugFail);
        }
    }

    #endregion Properties
}

在测试方法中调试:

    [TestMethod]
    public void ReadProperties()
    {
        DummyClassWithDebuglessProperties dummyClassWithDebuglessProperties = new DummyClassWithDebuglessProperties();

        //Create watch for next
        //dummyClassWithDebuglessProperties.AllProperties;

        //breakpoint at the end.
    }

似乎处理了 ThreadAbortException ,但是Thread.ResetAbort()没有成功恢复应该在监视窗口中初始化表达式的线程,并且当离开 Catch 块时,它被重新抛出,就像 ResetAbort 没有效果一样。在到达断点时对手表的第一次评估中,我有下一个情况:
图片 - 最初尝试读取“不安全”属性

我们看到提示读取该属性并且发生了 ThreadAbortException。单击标记的图标后,将执行“不安全”读取。
图片 - 单击按钮后的常规阅读

我对初步评估感兴趣,是否可以进行评估,例如:
简单,失败阅读

也许 Thread.ResetAbort() 被 Watch Window 线程忽略。有任何想法吗?

作为参考的相关主题:
调试期间的 Visual Studio:函数评估需要所有线程运行
评估需要一个线程临时运行。使用 Watch 窗口执行评估

4

0 回答 0