3

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);
          }
       }
Ganesh R.
  • 4,337
  • 3
  • 30
  • 46
  • how does sDisplayNumber and ReverseNumbers look like? – AndersK Aug 03 '09 at 07:30
  • one problem is that there is no guarantee exactly when the thread starts, if you step down to t2.Start(), then t1 may haven't started yet but since you are pausing on that line nothing will happen. Maybe if you put a line inbetween t1.Start() and t2.Start() checking if thread started, then start t2 it would work better. – AndersK Aug 03 '09 at 07:34
  • If a put Console.ReadKey() between t1.Start() & t2.Start(0 then t1 executes. – Ganesh R. Aug 03 '09 at 07:46
  • Please look at - [http://stackoverflow.com/questions/1147387/how-to-debug-a-deadlock](http://stackoverflow.com/questions/1147387/how-to-debug-a-deadlock) – KV Prajapati Aug 03 '09 at 07:25

4 Answers4

6

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
  • 2,369
  • 16
  • 26
  • 4
    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 Aug 03 '09 at 07:39
  • If I go to threads Window -> Freeze the main thread & Press f5, then thread t1 executes but then I have no way to resume main thread as the thread window goes out of focus (blanks out since the break point is stepped out of). Hence I think its the above is the answer. That VBSStudio by default pauses all threads; which as Andrew said is good for debugging purpose. – Ganesh R. Aug 03 '09 at 07:52
  • this restriction is for good i guess, if you suddenly want to break and see situation of each thread. suppose i want to break on a condition in any thread. now if other threads keep running, then how do i get a snapshot view of the program at that time. anyhow, you can freeze/thaw any threads at your will (once after you break) and then continue. it is good for natural debugging flow. – Munish Goyal May 23 '11 at 07:55
2

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
  • 2,729
  • 2
  • 21
  • 28
1

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
  • 44,254
  • 30
  • 139
  • 205
1

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
  • 114,645
  • 34
  • 221
  • 317