views:

238

answers:

5

I have created a console test application which creates, an object & calls 2 functions on 2 separate threads. One thread prints numbers form 1- 20 the other in the reverse order.

The problem is while debugging the 1st worker thread does not get fired till I don't stop debugging the main thread (i.e. I press f5). Any answers?

class Program
 {
  static void Main(string[] args)
   {
     DisplayData dd = new DisplayData();

     ThreadStart ts1 = new ThreadStart(dd.DisplayNumber);
     Thread t1 = new Thread(ts1);
     t1.Name = "Display Numbers";

     ThreadStart ts2 = new ThreadStart(dd.ReverseNumbers);
     Thread t2 = new Thread(ts2);
     t2.Name = "Reverse Numbers";

     t1.Start(); //Keep 1st break point at this location. Then press F10.
     t2.Start(); //Keep break point at this location permanently
    }




public class DisplayData
   {
       public void DisplayNumber()
       {
          int length = 20;
          Console.WriteLine("\nNumbers in correct order.\n");


          for (int i = 0; i < length; i++)
          {

             Console.WriteLine("DN: The value of i is: " + (i+1) + " " + Thread.CurrentThread.ManagedThreadId + " " + Thread.CurrentThread.Name);
             //Thread.Sleep(1000);

          }
       }

       public void ReverseNumbers()
       {
          int length = 20;
          Console.WriteLine("\nNumbers in reverse order.\n");
          for (int i = 0; i < length; i++)
          {

             Console.WriteLine("RN: The value of i is: " + (length - i) + " " + Thread.CurrentThread.ManagedThreadId + " " + Thread.CurrentThread.Name);
             //Thread.Sleep(1000);
          }
       }
+4  A: 

I'm not sure if this is a limitation in Visual Studio or the CLR but setting breakpoint on one thread will stop all threads.

If you want to pause a thread you can do this through the Debug..Threads window, you can pause and resume threads there.

Kevin Jones
Personally, I don't consider it a limitation that setting a breakpoint breaks all threads. When I debug multithreaded code, I want to know exactly what the order of steps happen to be in that debugging session.
Andrew Shepherd
If I go to threads Window -> Freeze the main thread which as Andrew said is good for debugging purpose.
Ganesh R.
A: 

Work on another threat does not start until a context switch happens. It could easy get through all the work in your main routine before the CPU decides to start on something else.

Use t1.Join() to wait for a worker thread to finish before continuing.

Nick Whaley
A: 

You'll find that if you keep stepping through the main thread code it will eventually switch to one of your worker threads.

But the simplest thing to do in your situation is to place a break point on the first line of DisplayNumber and ReverseNumbers, if that's what you specifically want to debug.

Andrew Shepherd
The thing is I was trying to understand how threading works. Hence the simple example.
Ganesh R.
+1  A: 

Well it does "execute", but it doesn't necessarily get any time slice. When you single step through your code all threads effectively "execute". However, the actual number of running threads depend on available CPUs. So depending on the circumstances your other threads may or may not run.

Since the .NET framework synchronizes access to standard out, only one of your threads will hold that given lock at any time, which may give you the impression that the other threads do not run.

Brian Rasmussen
Another Possibility.
Ganesh R.