5

Currently I have a background thread that does some periodic heavy data management. Because it only needs to run its process every few hundred milliseconds I call Thread.Sleep() at the end of the process and it then goes back to the top of the loop and repeats.

This is all working perfectly and doesn't cause any grief or performance issues for the rest of the software. The only thing that does bug me though is that when I break the debugger instead of going to my main threads current location, it gets stolen by the Thread.Sleep() and takes me there instead.

Is there any way that I can disable the debugger from stopping on that line, or is there an alternative to putting the Thread to sleep?

Thanks in advance!

Craig White
  • 13,492
  • 4
  • 23
  • 36

1 Answers1

3

One thing you could try is to put the Thread.Sleep() in a method and add the DebuggerStepThrough on it.

You can also just "freeze" the you don't want to "steal focus" from Threads window (Debug -> Windows -> Threads). Also remember to unfreeze them when you're done

As an aside using a Timer (there are two) or a AutoReset Event is preferred to using Thread.Sleep() See Is Sleep() evil?

Community
  • 1
  • 1
Conrad Frix
  • 51,984
  • 12
  • 96
  • 155
  • Thanks for the `[DebuggerStepThrough]`, I'd previously thought about it but had thought it was only for Properties. Maybe I should reconsider my standing with the `Thread.Sleep()`. I don't feel that a Timer is the best idea for the full way it works. Is there a .NET 2.0 way to schedule a method? – Craig White Dec 19 '11 at 06:36
  • 1
    Ok, I now have my BgWorker finish and in completed I use: `TimerCallback tc = new TimerCallback(bw.RunWorkerAsync); System.Threading.Timer t = new System.Threading.Timer(tc, null, Interval, Timeout.Infinite);` – Craig White Dec 19 '11 at 06:39