4

Out of curiosity. I'd like to know if its possible to step into (F11) a background worker during debugging. It usually just skips over to the next line of code. Is it a VS setting that needs to be changed? Is it just how its meant to be?

Any clarification would be much appreciated. Thanks.

Dumisani
  • 2,988
  • 1
  • 29
  • 40
  • 1
    There should be no problem doing this - just put a breakpoint in the thread function and step through. At a breakpoint all threads are paused. [See here for more info](http://msdn.microsoft.com/en-us/library/bb157786.aspx). – Roger Rowland Apr 25 '14 at 07:25
  • @RogerRowland Yes, that's what I normally do. I'm just wondering why it never steps inside like it does with normal functions. – Dumisani Apr 25 '14 at 07:28
  • 1
    Debug/Windows/Threads gives you a list of running threads. Double click one e.g. while the program is suspended at a breakpoint and you see where the thread is stopped. Cf. http://msdn.microsoft.com/en-us/library/w15yf86f.aspx. As Roger said, from there debugging behaves pretty much normally. – Peter - Reinstate Monica Apr 25 '14 at 07:32
  • @PeterSchneider Mind adding your comment as an answer? :) – Dumisani Apr 25 '14 at 07:37
  • Rik beat me to it :-). He also suggested to set the breakpoint in the BG worker code which is kindof the key to stopping there. – Peter - Reinstate Monica Apr 25 '14 at 07:47

4 Answers4

6

As the comment says, you just have to set a breakpoint for the first line of code in your background task. (Or: Debugger.Break().)

Also, I recommend you to get a look for Debug > Windows > Threads window, it's quite useful in multithread debugging cases.

2

It usually just skips over to the next line of code.

That's because the BackgroundWorker is being executed on a different thread, which has to be created and started first, and this takes some time. If you keep stepping, you will enter the worker at some point, but there's no telling when exactly.

Just put a breakpoint at the start of the worker code if you want to debug it.

Rik
  • 28,507
  • 14
  • 48
  • 67
2

Once the debugging is started, open the thread window mentioned in Sebestyén answer. When the desired thread is started, keep an eye in the column "Location" of the new row that appeared in the Thread window. Once it have the name of the thread you want, right-click it and select "Switch to Thread". This solved the problem of the debugger cursor keep going to other points of the code (other threads running, I suppose) while I was trying to debug a specific thread.

jcs
  • 611
  • 2
  • 9
  • 22
2

From Haggisatonal answer at Visual Studio 2015 Debug doesn't work in multithread application

disable the VS hosting process (Project -> Properties -> Debug -> Enable the Visual Studio hosting process)

General Grievance
  • 4,555
  • 31
  • 31
  • 45