调用 Debugger.Break() 之前和之后的 C# 事件?
我正在调试一个人工智能机器人,它被输入“重播”输入数据,以使其重播之前进行的比赛。每个回合都限制一定的时间,机器人用该时间来计算当前回合的剩余时间;这让它决定如何使用剩余的周转时间。现在,我想做的是跟踪调试所花费的时间(该回合在断点上花费的总时间),因此我可以将其添加到剩余时间中,以使执行看起来从未暂停。
最初,我尝试使用这样的东西:
private DateTime turnTimeStarted;
public TimeSpan TurnTimeRemaining
{
get { return (turnTimeStarted + TurnTime + TimeSpentInDebugger) -
DateTime.Now; }
}
private DateTime debugStartTime;
private bool inDebugger = false;
private TimeSpan timeSpentInDebugger = new TimeSpan();
public TimeSpan TimeSpentInDebugger
{
get
{
if (!inDebugger)
return timeSpentInDebugger;
return timeSpentInDebugger + (DateTime.Now - debugStartTime);
}
}
public void Break()
{
if (Debugger.IsAttached)
{
debugStartTime = DateTime.Now;
inDebugger = true;
Debugger.Break();
inDebugger = false;
timeSpentInDebugger += DateTime.Now - debugStartTime;
}
}
这样做的问题是它在实际的 Debugger.Break() 语句而不是 Break() 方法调用上中断,所以我最终超出了我想要调试的代码的上下文。
我正在寻找类似在调用 Debugger.Break 之前和之后触发的一组事件,以检测进入和离开断点的时间。有什么办法可以做到这一点吗?
I'm debugging an AI bot that's being fed 'replay' input data to make it replay a previously played match. Each turn is limited to a certain amount of time, which the bot uses to calculate its remaining time for the current turn; this lets it determine how to use the remaining turn time. Now, what I'd like to do, is keep track of the amount of time spent debugging (the total amount time spent in breakpoints for that turn), so I can add that to the remaining time to make it seem like execution never paused.
Originally, I tried using something like this:
private DateTime turnTimeStarted;
public TimeSpan TurnTimeRemaining
{
get { return (turnTimeStarted + TurnTime + TimeSpentInDebugger) -
DateTime.Now; }
}
private DateTime debugStartTime;
private bool inDebugger = false;
private TimeSpan timeSpentInDebugger = new TimeSpan();
public TimeSpan TimeSpentInDebugger
{
get
{
if (!inDebugger)
return timeSpentInDebugger;
return timeSpentInDebugger + (DateTime.Now - debugStartTime);
}
}
public void Break()
{
if (Debugger.IsAttached)
{
debugStartTime = DateTime.Now;
inDebugger = true;
Debugger.Break();
inDebugger = false;
timeSpentInDebugger += DateTime.Now - debugStartTime;
}
}
The problem with this is that it breaks on the actual Debugger.Break()
statement instead of the Break()
method call, so I end up outside the context of the code I want to debug.
What I'm looking for is something like a set of events that is triggered right before and after calling Debugger.Break
to detect the times for entering and leaving breakpoints. Is there any way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用秒表 ( http://msdn.microsoft.com/en -us/library/system.diagnostics.stopwatch.aspx ) 类并在中断之前开始,在之后停止并根据需要添加时间。
Use the Stopwatch ( http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx ) class and start just before breaking, stop just after and add times as required.
我建议看一下 SlimTune:http://code.google.com/p/slimtune/ 以及 Matt Pietrek 撰写的有关 DNProfiler 的文章 http://msdn.microsoft.com/en-us/magazine/cc301725.aspx
I'd suggest taking a look at SlimTune: http://code.google.com/p/slimtune/ and this article about the DNProfiler by Matt Pietrek http://msdn.microsoft.com/en-us/magazine/cc301725.aspx
我知道这个主题很旧,但我仍然没有找到更好的解决方案。
关于在错误的上下文中中断,您可以通过公开
Action
属性而不是方法来轻松解决此问题:然后调试器将停止在
MyDebugger.Break()
调用而不是>调试器.Break
。您可以在返回操作之前存储时间,但主要问题是您无法判断何时实际恢复执行,因为
Break()
仅请求停止,并不实际执行调试过程。为了解决这个问题,我编写了一个监视方法,用于测量其执行时间,并在访问属性时在单独的线程上启动它。当执行时间很慢时,将其视为时间花费评估断点,否则,当在一定时间内检测到正常执行速度时,线程退出。
我在这里分享
这样你就可以计算你的像这样的时间:
问题是您必须利用整个线程才能获得相当准确的测量结果。当您实际单步执行代码时,这并不是什么大问题,但在恢复应用程序后,线程将被阻塞几秒钟。
更重要的是,如果监视器没有持续打开,您将无法检测调试器何时命中 IDE 设置的断点。
为了解决这个问题,我编写了额外的惰性监视器,它以较慢的时间间隔在任务上运行,但它不是那么准确,并且有它自己的问题......
令人遗憾的是
Debugger
界面如此糟糕。I know the topic is old but I still didn't found better solution for this.
Regarding breaking in wrong context, you can solve this easily by exposing an
Action
property instead of a method:Then debugger will stop at
MyDebugger.Break()
call instead ofDebugger.Break
.You can store the time before returning the action but the main issue is you can't tell when the execution is actually resumed since the
Break()
only requests the stop and is not actually performing the debugging process.To work around this I wrote a monitoring method that measures it's execution time and start it on a sperate thread when the property is accessed. When execution time is slow it considers it as time spend evaluation breakpoint, otherwise, when normal execution speed is detected for certain amount of time, the thread exits.
I share it here
With this you could calculate your time like this:
The problem with that is that you have to utilize the whole thread in order to get reasonably accurate measurements. It's not a big deal when you are actually stepping through the code but you will have the thread blocked for few seconds after resuming the application.
More importantly, you are not able to detect when debugger hits breakpoint set from the IDE if monitor is not turned on constantly.
To deal with that I wrote additional lazy monitor that runs on a task in slower interval but it is not that accurate and has it's own issues...
It's a shame the
Debugger
interface is so poor.