简体   繁体   English

调试主线程时为什么工作线程不执行?

[英]Why does a worker thread not execute when debugging the main thread?

I have created a console test application which creates, an object & calls 2 functions on 2 separate threads. 我创建了一个控制台测试应用程序,它创建一个对象并在2个独立的线程上调用2个函数。 One thread prints numbers form 1- 20 the other in the reverse order. 一个线程以相反的顺序打印1-20形式的数字。

The problem is while debugging the 1st worker thread does not get fired till I don't stop debugging the main thread (ie I press f5). 问题是,在调试第一个工作线程之前,我没有停止调试主线程(即我按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);
          }
       }

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. 我不确定这是否是Visual Studio或CLR中的限制,但在一个线程上设置断点将停止所有线程。

If you want to pause a thread you can do this through the Debug..Threads window, you can pause and resume threads there. 如果你想暂停一个线程你可以通过Debug..Threads窗口执行此操作,你可以在那里暂停和恢复线程。

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. 在CPU决定开始其他操作之前,它可以轻松完成主程序中的所有工作。

Use t1.Join() to wait for a worker thread to finish before continuing. 使用t1.Join()等待工作线程完成后再继续。

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. 但在你的情况下最简单的做法是在DisplayNumberReverseNumbers的第一行放置一个断点,如果那是你特别想要调试的。

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. 但是,实际运行的线程数取决于可用的CPU。 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. 由于.NET框架同步对标准输出的访问,因此任何时候只有一个线程会持有给定的锁定,这可能会让您觉得其他线程不会运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM