Original Post
I'm having a problem with getting multithreading to work in C#. Specifically, my calls to Thread.Sleep() don't yield their time to the other threads. This is true even when I copy code verbatim out of help. Here's a piece of code copied directly from the help docs:
Here's what the output is SUPPOSED to look like, according to the help docs:
|
Quote:Here's the output I actually get when I run my program:
Main thread: Start a second thread. Main thread: Do some work. ThreadProc: 0 Main thread: Do some work. ThreadProc: 1 Main thread: Do some work. ThreadProc: 2 Main thread: Do some work. ThreadProc: 3 Main thread: Call Join(), to wait until ThreadProc ends. ThreadProc: 4 ThreadProc: 5 ThreadProc: 6 ThreadProc: 7 ThreadProc: 8 ThreadProc: 9 Main thread: ThreadProc.Join has returned. Press Enter to end program.
Quote:As you can see, any calls to Thread.Sleep() do NOT yield control to the other thread. But it's supposed to. Anyone have any ideas why this is happening? Thanks.
Main thread: Start a second thread. Main thread: Do some work. Main thread: Do some work. Main thread: Do some work. Main thread: Do some work. Main thread: Call Join(), to wait until ThreadProc ends. ThreadProc: 0 ThreadProc: 1 ThreadProc: 2 ThreadProc: 3 ThreadProc: 4 ThreadProc: 5 ThreadProc: 6 ThreadProc: 7 ThreadProc: 8 ThreadProc: 9 Main thread: ThreadProc.Join has returned. Press Enter to end program.