“Step over” when debugging multithreaded programs in Visual Studio

前端 未结 6 1674
青春惊慌失措
青春惊慌失措 2020-12-01 04:46

One thing that annoys me when debugging programs in Visual Studio (2005 in my case) is that when I use \"step over\" (by pressing F10) to execute to the next line

6条回答
  •  -上瘾入骨i
    2020-12-01 05:10

    Apparently, Visual Studio 2010 only switches to other threads if you press F10 when the debugger had to break in that thread before or if a breakpoint is set which will be hit in this thread.

    I used the following code to test the behavior:

    class Program
    {
        static void Main(string[] args)
        {
            var t = new Thread(new ThreadStart(Work));
            t.Start();
    
            for (int i = 0; i < 20; i++)
            {
                Thread.Sleep(1000);
                Console.WriteLine("............");
            }
    
            t.Join();
        }
    
        static void Work()
        {
            for (int i = 0; i < 20; i++)
            {
                Thread.Sleep(1000);
                Console.WriteLine("ZZzzzzzzzzzzzzzz");
            }
        }
    }
    

    If you just stept to the program or add a breakpoint in the Main() method, hitting F10 only steps through the code from the main thread.

    If you add a breakpoint in the Work() method, the debugger steps through both threads.

    This behavior of Visual Studio makes sense but it all seems like an undocumented feature to me...

提交回复
热议问题