The Art of Debugging – A Developer’s Best Friend – Lesson 10 – Debugging Threads – Repost
Download the sample project – debugging.zip |
This next section is fairly brief and will help you work with the debugging of threads more effectively.
The the feature all I am about to show you is off by default.
It is available from the toolbar so as a first step we need to bring up the debug toolbar.
Right mouse click at the toolbar area and select debug.
The next step is to enable the option “show threads in source.” Once again that is available from the debug toolbar. It is not enabled by default, therefore we must switch it on.
To test this option we will create our threads and then enter break mode. Select the “create threads” button below.
Next, we will select “Break All” from the menu.
Because we have enabled source code debugging, we will end up in some .net system source code.
We need to switch windows back to our own source code. We can do this with a simple control tab. Notice the squiggly lines in the gutter. If we hover our mouse over the squiggly lines we can see exactly which threads are awaiting to be signaled.
Notice we can see that five threads are awaiting on the method call WaitOne().
As the next step,let’sbringupthethreads debugger window. You can find this in the Visual Studio menu Debug | Windows | Threads.
Notice that we can hover the mouse over one of the thread’s location columns and get the call stack for that thread. For example, below we are looking at thread 3’s call stack.
You also have the option of renaming the thread the right from the debugger using a simple right mouse click.
Big challenge for developers – single stepping through a thread’s execution
Because windows is a preemptive operating system, the context switch to another thread may be forced upon you buy Windows. This can interfere greatly with your debugging session. The solution to this problem is to suspend the other threads so that the thread you are debugging does not get interrupted by the operating system.
That means you can control click and select all the threads that you wish to suspend, right mouse click and choose the freeze option as you see above. Freeze is the same thing as suspend.
You can also thaw your threads
Notice that in the second column of the threads window, four of our threads were suspended. But I can re-select act the suspended threads and get them to resume by right mouse clicking and selecting thaw.