27

I would like to be able to follow only one thread's execution while debugging. I use a threadpool so the debugger keeps switching between threads and this makes debugging very uncomfortable.

Reading:

I get one solution which is conditional breakpoints (based on the name of the thread). However, I can't tell "the thread #3" will always be the one treating the interesting case, so I would have to change the condition for each execution. Too much work.

Another solution is to use the freeze/thaw feature to make only my interesting thread run. However, this make some information unavailable because all threads are paused.

What I am using now is to put make the program run until I get to a breakpoint where I am sure to be in the good thread. Then I pause all other threads of threadpool and try to resume the execution. If the programs seems to be stuck, I pause, and thaw the current thread.

The ideal solution would to find the correct thread, flag it and then say to Visual Studio: "break only if the current thread is flagged".

Is this even possible ?

Community
  • 1
  • 1
kamaradclimber
  • 2,479
  • 1
  • 26
  • 45
  • So what is the criteria for thread to break in? – abatishchev Nov 18 '11 at 14:05
  • the criteria would be stop is the thread is flagged – kamaradclimber Nov 18 '11 at 14:41
  • Test your code in isolation. With unit tests for example. Then you only have to debug the thread interaction, the kind of debugging where you *don't* want to freeze threads. – Hans Passant Nov 18 '11 at 15:41
  • 1
    this is not what I want, my problem is not a concurrency issue but I just don't want to loose my time switching from the thread that interest me to another one. – kamaradclimber Nov 18 '11 at 15:45
  • Related [“Step over” when debugging multithreaded programs in Visual Studio](http://stackoverflow.com/questions/336628/step-over-when-debugging-multithreaded-programs-in-visual-studio) – Liam May 12 '17 at 12:41

3 Answers3

7

While debugging you can freeze all the threads in the Threads window and resume only the one you're interested in.

Iman
  • 17,932
  • 6
  • 80
  • 90
Bode
  • 87
  • 1
  • 1
  • 4
    thanks but it does not answer the question: I want the application to run normally and only stopping on break points in one thread – kamaradclimber Feb 06 '14 at 07:16
2

A bit late but this cropped up as the first answer in a search.

I use the following in VS 2015...

var thread = System.Threading.Thread.CurrentThread;
if (thread.Name == null)
            thread.Name = "Main";

Then in the breakpoint...

System.Threading.Thread.CurrentThread.Name == "Main"

To make it more flexible you can embed Thread in a custom class.

FYI: You cannot use static variables in a conditional breakpoint as they are not in context. Never really understood why statics are not always in context.

Paulustrious
  • 609
  • 1
  • 11
  • 17
  • thanks for your answer (even late) where would you add the first piece of code to name the thread? (I've mentioned adding this into the code would require to know in advance the thread I want to follow) – kamaradclimber Jan 24 '17 at 16:53
  • @kamaradclimber. As soon as possible. You can only set a thread name once or there is an exception. You can set thread.Name manually in debug. You can also say if thread.name == xxx System.Diagnostics.Debugger.Break() – Paulustrious Jan 25 '17 at 21:39
  • Sorry .. 5 minute timeout. @kamaradclimber. As soon as possible. You can only set a thread name once or there is an exception. You can set thread.Name manually in debug or just System.Threading.Thread.CurrentThread.Name = "xyz". You can also say ... if (thread.name == xxx) { System.Diagnostics.Debugger.Break();}. You can give multiple threads the same name if you want. In the past I have added code along the lines of bool stop = thread.Name == "someName" and then in the debugging breakpoint you can just put the condition `stop`. Makes life easier – Paulustrious Jan 25 '17 at 21:50
2

I would use the conditional breakpoints you mentioned, but instead of comparing to some fixed string compare to some semi-global variable (maybe a static property on your main class?).

When you identify which thread becomes interesting you can use the immediate window to set the variable name and allow your conditional breakpoints to be hit.

MBirchmeier
  • 134
  • 7
  • that would be a possible solution. I see 2 drabacks though : I need to modify my codeto debug it and I need to go through a (complicated) breakpoint procedure (1 click vs click,copy-past,1click) – kamaradclimber Nov 18 '11 at 22:51
  • For those who tune in lately (like me) I suggest reading this [answer which elaborates this workaround](http://stackoverflow.com/a/38926833/1348138) a little further. I also recommend voting up [the suggestion mentioned here](http://stackoverflow.com/a/31191208/1348138). – robert4 Aug 12 '16 at 22:02