如何防止 Debug.Assert(...) 显示模态对话框 [英] How to prevent Debug.Assert(...) to show a modal dialog

查看:21
本文介绍了如何防止 Debug.Assert(...) 显示模态对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个使用 Debug.Assert(...) 的库.我认为 Debug.Assert(...) 很好,我仍然希望它们执行,但我不希望它们阻止我的应用程序的执行.理想情况下,我只希望它们被记录在某个地方.

I have a couple of libraries which use Debug.Assert(...). I think that the Debug.Assert(...) are fine and I still want them to execute, but I don't want them to block the execution of my application. Ideally, I would only like them to be logged somewhere.

鉴于我无法更改库的代码(并且我仍然想在调试中编译并运行断言),我该如何防止 Debug.Assert(...)显示模态对话框?

Given that I can't change the code of the libraries (and that I still want to compile in debug and run the assertion), how do I prevent Debug.Assert(...) to show a modal dialog?

此外,我想确保主程序在 Assert 发生时继续运行(与忽略按钮的行为相同).

In addition, I would like to make sure that the main program continues when an Assert occurs (same behavior as the Ignore button).

谢谢!

推荐答案

我不推荐它.问题是 Debug.Assert 应该只在您的代码中有错误时被触发.如果您只是忽略它们或不修复它们,那么您就是在伤害您的用户.另一方面,如果你为那些不是错误的事情触发Debug.Assert,那么你也会伤害你的用户(通过减少Debug.Assert 的影响).

I wouldn't recommend it. The problem is that Debug.Assert is only supposed to be fired when you have bugs in your code. If you just ignore them or don't fix them, then you are doing your users a disservice. If, on the other hand, you're firing Debug.Assert for things that aren't bugs, then you're also doing your users a disservice (by reducing the impact of Debug.Assert).

话虽如此,您可以禁用它.您需要做的第一件事是从 Debug.Listeners 集合:

Having said that, you can disable it. The first thing you need to do is remove the default listener from the Debug.Listeners collection:

Debug.Listeners.Clear();

然后,添加您自己的:

Debug.Listeners.Add(new MyTraceListener());

您需要创建一个继承自 跟踪监听器:

You need to create a class that inherits from TraceListener:

class MyTraceListener : TraceListener
{
    // ...

    public override void Fail(string msg, string detailedMsg)
    {
        // log the message (don't display a MessageBox)
    }
}

重要的方法是 TraceListener.Fail 方法,它在 DefaultTraceListener 的实现中是显示消息框的.

The important method is the TraceListener.Fail method, which in the implementation of DefaultTraceListener is what displays the message box.

这篇关于如何防止 Debug.Assert(...) 显示模态对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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