Tue, August 26, 2008, 12:28 AM under
dotNET
I only learnt about this featurette very recently (from
a reader's comment) and immediately found a use for it – read on.
If you tried to debug the
code from this exercise (still no correct answers to the quiz!), you probably found out that the variable corresponding to the Task that you want to examine is usually out of scope. You can look at the Threads window and try and map the Thread to the Task but there is another way: add once to the Watch window the expression
Task.Current
. When breaking in the debugger, switch between the threads in the Threads window to see the active/current Task for that Thread in the Watch window.
The only problem is that we have to tediously expand the variable each time to determine its Id, since the default display when the expression is evaluated is not useful ("{System.Threading.Tasks.Task}").
This is where the C#-only debugger feature comes in:
Make Object ID, accessible from the ContextMenu in the Watch window. Once you break in the debugger, Make Object ID for the Task.Current, then switch Threads and do the same for the other. Now you can distinguish between them easily due to the appended {1#}, {2#}, and you can add a Watch directly to the objects in memory:
This is a neat little feature and even more useful for other scenarios where there is a need to distinguish between 2 or more identical-looking objects in the debugger.