简体   繁体   中英

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? Will the debugger deadlock until I continue executing after breakpoint (or kill visual studio/debugging)? Or will the debugger simply not show any data for the property (grabbing data in background thread from 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!

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".

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()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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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