简体   繁体   English

打破单线程

[英]Breaking single thread

Is it possible to break a single thread in Visual Studio, while other threads will continue their execution? 是否有可能在Visual Studio中破坏单个线程,而其他线程将继续执行?

I have one background thread that does simple data sending/receiving, which I would like to happen, while stepping through my code in some other thread. 我有一个后台线程执行简单的数据发送/接收,我希望在其他线程中逐步执行我的代码。

open the thread view (Debug->Windows->Threads), right-click the thread you want to suspend, select 'Freeze'. 打开线程视图(Debug-> Windows-> Threads),右键单击要挂起的线程,选择“Freeze”。 Select 'Thaw' to put it back in a running state. 选择“解冻”将其重新置于运行状态。

Generally it's impossible, but there are some things that might work for specific scenarios. 一般情况下这是不可能的,但有一些事情可能适用于特定的场景。

Basic solution As mentioned elsewhere, repeating the sequence: Freeze, Resume, (wait), Pause, Thaw, Step should result in the behavior you describe, giving other threads the possibility of running in the background while your target thread is halted. 基本解决方案如其他地方所述,重复序列: 冻结,恢复,(等待),暂停,解冻,步骤应该导致您描述的行为,让其他线程在目标线程暂停时在后台运行。

This approach has at least two issues: 这种方法至少有两个问题:

  • It's quite tedious 这很乏味
  • Your background threads will be suspended anytime the debugger is paused. 您的后台线程将在调试器暂停时暂停。

Improvements 改进

The first issue may be tackled using a different procedure: Issue a Thread.Sleep(10000) in the Immediate Window, effectively keeping the focused thread occupied while the other threads execute normally. 可以使用不同的过程解决第一个问题:在立即窗口中发出Thread.Sleep(10000) ,有效地保持聚焦线程占用而其他线程正常执行。 You could even bind that command to a macro. 您甚至可以将该命令绑定到宏。

The second issue can only be tackled by an approach that does not need to pause the debugger. 第二个问题只能通过不需要暂停调试器的方法来解决。 But how would we examine state when the session isn't paused? 但是,当会话没有暂停时,我们如何检查状态? That's where IntelliTrace comes in, but you may find you need to create custom IntelliTrace events . 这就是IntelliTrace的用武之地,但您可能会发现需要创建自定义IntelliTrace事件 Drawback of this approach is that you can not manually modify state mid-flight. 这种方法的缺点是你不能手动修改飞行中的状态。

Set a counter that does a one up for each thread created and then set your break point to break on a condition and pick a value for that counter. 设置一个计数器,为每个创建的线程执行一个计数器,然后将断点设置为在条件上中断并为该计数器选择一个值。 I don't think this will work in all cases, especially PLINQ, but should be doable in a lot of situations. 我认为这不会适用于所有情况,特别是PLINQ,但在很多情况下都应该可行。

You can always put a conditional break point based on a property of your current thread (like the name or id). 您始终可以根据当前线程的属性(如名称或ID)设置条件断点。

You may also find this usefull : http://www.codeproject.com/Tips/396617/Conditional-Breakpoint-using-Make-Object-Id-featur 你也可以找到这个有用的: http//www.codeproject.com/Tips/396617/Conditional-Breakpoint-using-Make-Object-Id-featur

This worked for me in VS2008 and should work in a similar way in 2010 at the least 这在VS2008中对我有用,至少应该在2010年以类似的方式工作

All i can find to this, is that you can change the behaviour on a process level by the setting 我可以找到的是,您可以通过设置更改进程级别的行为

  • Tools - Options - Debugging - General - Break all processes when one process breaks 工具 - 选项 - 调试 - 常规 - 在一个进程中断时中断所有进程

but not on a Thread base. 但不是在Thread基础上。

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

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