Hidden Breakpoint Feature: Stepping in just one thread
So I was giving this demo the other day of some of our new concurrency features in the Orcas CTP and I found this "new" feature for breakpoints. At least it was new for me. Sorry I'm a noob :)
The code was something like so:
451 private void ComputePrimes_ThreadProc(object obj)
452 {
453 ThreadParams threadParams = (ThreadParams)obj; <- Breakpoint here
454
455 // Go through our range, determining if a number is a prime
456 for (int number = threadParams.lower; number <= threadParams.upper; number++)
457 {
458 if (IsAPrime(number))
459 {
460 // Then add to our list
461 _primes.Add(number);
462 }
463 }
464
465 }
The problem I was trying to demonstrate, while stepping through some code, was a race condition occurring on a collection (one of our new features is supposed to help point this out). However, whenever I stepped, the other threads kept hitting the breakpoint, moving my ip indicator back to the breakpoint, and generally screwing stuff up.
Here's a screenshot of it in action (notice our new other thread indicators)
Yeah this is a pain. I want to just step in this first thread. I guess I could wait until the right thread gets to the breakpoint, then freeze everybody else. This doesn't seem to work though (mostly because of the internal state of the other threads)
But wait, there is a way to step in just this thread!
Here's how:
- Right click on the breakpoint and select "Filter" from the list of options
- When the filter dialog appears, type in a filter condition. I used ThreadName=<my thread name>
- Click OK
Now when the breakpoint gets hit, only this thread will be in stepping mode. (Something else I learned, threads are in one of 3 states - stepping, running, or suspended)
So as you step, the IP indicator won't jump around.
Of course this then made me want more, but I'll leave that for another post.
Comments
- Anonymous
November 05, 2007
You can also write a macro to step only a single thread. Just suspend all other threads, do a step, and then unsuspend the threads. You can add another button to the toolbar (and/or assign a keystroke), and it will be just like a built in feature. I did this a while back but I don't have the macro code now... You could also create a macro to set a breakpoint in a single thread when in Break mode. Should be pretty easy to automate setting a breakpoint with a filter where ThreadId is the current thread id.