-1

I'm new to threading in .net and its hard to find good examples that I can use. I'm reading articles here. Managed Threading Best Practices But I'm still not clear. Right now I have a program where I'm trying to have two threads running at the same time but doing different things. I have one thread working doing what it is supposed to, but when I start my second thread (I name it PrintThread) I have PrintThread and Main Thread running the same lines of code, Should I be looking into the syncLock statement to avoid this?

Brandon
  • 915
  • 4
  • 23
  • 44
  • 1
    The whole point of threads is that they can run at the same time. If you don't want them to execute at the same time, don't use threads. – Oded Nov 09 '12 at 14:37
  • Please explain what the issue is with the two threads running the same code. Why is that an issue? A lock will not help with this. – Oded Nov 09 '12 at 14:38
  • SETTINGS_FILE_PATH = Cannot evaluate expression because we are stopped in a place where garbage collection is impossible, possibly because the code of the current method may be optimized. - This is where my Main Thread is stopping, can any of the experts on here point me in the right direction? – Brandon Nov 09 '12 at 14:39
  • Right, well I want them running at the same time, but i don't want them doing the same thing or rather running the same lines of code – Brandon Nov 09 '12 at 14:40
  • @BrandonJ: if you don't want your threads to run the same lines of code, point them to different procedures. :) – Victor Zakharov Nov 09 '12 at 14:43
  • 1
    If they are running the same code (which may be OK), then that's how you defined your second thread (that's what you told it to run). – Oded Nov 09 '12 at 14:44
  • I think I see the problem, thanks for the ideas – Brandon Nov 09 '12 at 14:56

1 Answers1

1

Cannot evaluate expression because we are stopped in a place where garbage collection is impossible

This is just a debugger diagnostic, telling you that it can't display the watch expression. You are apt to get this problem when you use Debug + Break All to break into the debugger.

In general, yes it can be quite difficult to debug code when two or more threads are running the same code. Breakpoints stop being effective since every thread will hit them. One possible solution for that, beyond using unit tests (recommended!), is to use Debug + Windows + Threads. You'll see the active threads listed in this window. You can right click them and select "Freeze". That freezes the thread until you "Thaw" it again. Which lets you focus on debugging only one thread.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536