vb.netmultithreadingsynclock

Preventing two Threads from running at the same time in vb.net


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?


Solution

  • 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.