How to debug a single thread in Visual Studio?

后端 未结 10 1573
执笔经年
执笔经年 2020-11-28 17:59

I have a solution with some projects. There are several break-points in different projects. I want to trace the first thread hit one of these break-points and continue traci

相关标签:
10条回答
  • 2020-11-28 18:28

    A slightly different approach which I've used:

    1. Create a normal breakpoint and let it get hit
    2. Look in your threads window for the managed thread ID that you're current debugging
    3. Right click your breakpoint in the breakpoints window and selecter filter
    4. Enter ThreadId=xxx where xxx is the thread ID from 2
    5. You can now debug without stopping other threads and without them hitting your breakpoint

    This assumes you have time to do the above before a second thread hits your breakpoint. If not and other threads hit your breakpoint before you've done the above you can right click them in the threads window and choose freeze.

    0 讨论(0)
  • 2020-11-28 18:30

    In VS 2019:

    1. Set a breakpoint somewhere.
    2. Hit F5 (Continue) until your thread comes.
    3. Click on breakpoint to remove it.
    4. You can step the thread with F10 or F11.
    0 讨论(0)
  • 2020-11-28 18:32

    I would suggest adding another instance of the application on the live server, either on the same hardware or a new machine (cluster it) and then debug only that instance. I wouldn't add a breakpoint in code users are triggering. If that's not an option, I'd add more tracing.

    However, if this is absolutely necessary and you need a solution stat, I'm sure you could add a breakpoint that breaks only if the request is coming from your IP address. You would do this by adding a conditional breakpoint that inspects HttpContext.Request.UserHostAddress. Note however that this slows down your application considerably.

    0 讨论(0)
  • 2020-11-28 18:32

    If you don't want to stop all other threads (maybe you are attaching Visual Studio debugger to a running application that needs to answer to requests), you can use a macro that create and remove breakpoints automatically.

    This is suggested in an answer to Stack Overflow question "Step over" when debugging multithreaded programs in Visual Studio.

    However, the link only explain how to debug line by line. I suggest you modify the macro (if you're comfortable with it) to make it modify all breakpoints (in a given range of line for instance) to stop only on the current thread.

    0 讨论(0)
提交回复
热议问题