简体   繁体   English

带锁的C#Visual Studio调试器UI行为

[英]C# Visual Studio Debugger UI behavior with lock

I have a block of code in a lock: 我在锁中有一段代码:

lock (obj)
{
  //...
}

I also have a property that locks on that same object. 我还有一个锁定在同一对象上的属性。 Simple enough scenario. 足够简单的场景。 My question is, if I put a breakpoint inside my locked block of code, and then examine the property in the Visual Studio debugger, what will happen? 我的问题是,如果我在锁定的代码块中放置一个断点,然后在Visual Studio调试器中检查该属性,会发生什么? Will the debugger deadlock until I continue executing after breakpoint (or kill visual studio/debugging)? 在断点之后继续执行之前,调试器是否会死锁(或终止Visual Studio /调试)? Or will the debugger simply not show any data for the property (grabbing data in background thread from UI?) 还是调试器不会简单地不显示该属性的任何数据(从UI抓取后台线程中的数据?)

The reason I ask is I've got a property specifically (and only) for debugging purposes; 我问的原因是我有一个(专门)用于调试的属性; I'm OK with it occasionally not showing data when this scenario happens, but having crashed the debugger (and visual studio) many a time with bad debugger attributes, I'd rather avoid code that could at some point hamper my debugging efforts when that's what I'm trying to aid to begin with! 我可以接受这种情况下偶尔不显示数据的情况,但是由于调试器属性不好而使调试器(和Visual Studio)多次崩溃,我宁愿避免某些时候会妨碍我的调试工作的代码首先我要帮助的是!

I plan on testing this at some point when I've got some more time, but was hoping for a quicker answer from someone who might know better. 我计划在有更多时间的时候进行测试,但希望有更多了解的人提供更快的答案。

Yes, the debugger executes watch expressions on a separate worker thread that's running inside the process. 是的,调试器在进程内部运行的单独工作线程上执行监视表达式。 Which will hit the lock in your property getter and block. 它将锁定您的属性获取器并阻止它。 The debugger puts up with that for 5 seconds, then declares the watch expression unusable and displays "Function evaluation timed out". 调试器将其忍受5秒钟,然后声明watch表达式不可用并显示“功能评估超时”。

The debugger then gets grumpy, not much it can do with that blocked thread, you'll commonly see "Function evaluation disabled because a previous function evaluation timed out. You must continue execution to reenable function evaluation." 然后,调试器会变得脾气暴躁,对被阻塞的线程无能为力,您通常会看到“由于先前的函数评估超时而禁用了函数评估。您必须继续执行才能重新启用函数评估。” Which is good advice. 这是个好建议。

No the debugger will show you the properties of the object even if it is within a lock() block. 即使对象位于lock()块中,调试器也不会向您显示该对象的属性。

Lock()ing the object does not actually prevent any access to the object - it simply creates a semaphore that will block any other code that attempts to lock the same object until the lock is released. 对对象进行Lock()实际上并不能阻止对该对象的任何访问-它只是创建一个信号量,该信号量将阻止尝试锁定同一对象的任何其他代码,直到释放锁定为止。

我的经验是VS.NET调试器确实会不时冻结,但是它必须具有一些死锁检测和调试优化功能,以避免出现此类问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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