本文介绍了“过渡”在Visual Studio中调试多线程程序时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 在Visual Studio(2005)中调试程序时,有一件事情是,当我使用逐步(通过按)执行下一行代码时,我经常最终以与我所看到的完全不同的线程达到那条特定的代码行。这意味着我所做的一切事情都丢失了。 如何解决这个问题? 如果这可以在Visual Studio的更高版本中进行,我也想听听。 在下一行代码中设置断点有条件,只有打破这个线程不是我正在寻找的答案,因为这是太多的工作,对我有用:)解决方案我认为你的问题只有一个答案,你被打折为太多的工作,但是我相信这是因为你错了。让我介绍在Thread ID上添加条件断点的步骤,这是非常容易的,但不明显,直到你知道。 在你想要继续调试的正确线程中停止调试器(我猜这通常是第一个获取那个线程) 将 $ TID 输入观察窗口。 添加具有条件 $ TID ==< 的价值来自Watch Window的$ TID > , 示例: $ TID == 0x000016a0 继续执行 $ TID Microsoft编辑器的魔术变量(至少Visual Visual Studio 2003)具有当前线程ID的值。它使得它比查看更容易(FS + 0x18)[0x24]。 = D 就是说,你可以通过一些简单的宏来获得与调试器的一次性断点相同的行为。当您过渡时,调试器在幕后设置断点,运行到该断点,然后将其删除。如果任何断点被击中,一致用户界面的关键是删除这些断点。 以下两个宏提供 Step Over 和运行到光标。这是以与调试器相同的方式完成的,断点在执行后被删除,而不考虑哪个断点。 您将要分配一个组合键 : :一个警告 - 步骤超过宏只能在您要跳过的行上光标正常工作。这是因为它通过光标位置确定当前位置,并且只需将一个添加到行号。虽然我无法从Macro IDE找到该信息,但您可能可以使用当前执行点的信息替换位置计算。 One thing that annoys me when debugging programs in Visual Studio (2005 in my case) is that when I use "step over" (by pressing ) to execute to the next line of code, I often end up reaching that particular line of code in a totally different thread than the one I was looking at. This means that all the context of what I was doing was lost.How do I work around this?If this is possible to do in later versions of Visual Studio, I'd like to hear about it as well.Setting a breakpoint on the next line of code which has a conditional to only break for this thread is not the answer I'm looking for since it is way too much work to be useful for me :) 解决方案 I think there is only one answer to your question, which you have discounted as being 'way too much work.' However, I believe that is because you are going about it the wrong way. Let me present steps for adding a conditional breakpoint on Thread ID, which are extremely easy, but not obvious until you know them.Stop the debugger at a point where you are in the correct thread you want to continue debugging in (which I would guess is usually the first thread that gets there).Enter $TID into the watch window.Add a break point with the condition $TID == <value of $TID from Watch Window>, Example: $TID == 0x000016a0Continue Execution.$TID is a magic variable for Microsoft compilers (since at least Visual Studio 2003) that has the value of the current Thread ID. It makes it much easier than looking at (FS+0x18)[0x24]. =DThat being said, you can get the same behavior as the debugger's One-Shot breakpoints with some simple macros. When you step over, the debugger, behind the scenes, sets a breakpoint, runs to that breakpoint and then removes it. The key to a consistent user interface is removing those breakpoints if ANY breakpoint is hit.The following two macros provide Step Over and Run To Cursor for the current thread. This is accomplished in the same manner as the debugger, with the breakpoints being removed after execution, regardless of which breakpoint is hit.You will want to assign a key combination to run them.Here they are and good luck bug hunting!!Imports SystemImports EnvDTEImports EnvDTE80Imports System.DiagnosticsPublic Module DebugHelperFunctions Sub RunToCursorInMyThread() Dim textSelection As EnvDTE.TextSelection Dim myThread As EnvDTE.Thread Dim bp As EnvDTE.Breakpoint Dim bps As EnvDTE.Breakpoints ' For Breakpoints.Add() Dim FileName As String Dim LineNumber As Integer Dim ThreadID As String ' Get local references for ease of use myThread = DTE.Debugger.CurrentThread textSelection = DTE.ActiveDocument.Selection LineNumber = textSelection.ActivePoint.Line FileName = textSelection.DTE.ActiveDocument.FullName ThreadID = myThread.ID ' Add a "One-Shot" Breakpoint in current file on current line for current thread bps = DTE.Debugger.Breakpoints.Add("", FileName, LineNumber, 1, "$TID == " & ThreadID) ' Run to the next stop DTE.Debugger.Go(True) ' Remove our "One-Shot" Breakpoint For Each bp In bps bp.Delete() Next End Sub Sub StepOverInMyThread() Dim textSelection As EnvDTE.TextSelection Dim myThread As EnvDTE.Thread Dim bp As EnvDTE.Breakpoint Dim bps As EnvDTE.Breakpoints ' For Breakpoints.Add() Dim FileName As String Dim LineNumber As Integer Dim ThreadID As String ' Get local references for ease of use myThread = DTE.Debugger.CurrentThread textSelection = DTE.ActiveDocument.Selection LineNumber = textSelection.ActivePoint.Line FileName = textSelection.DTE.ActiveDocument.FullName ThreadID = myThread.ID LineNumber = LineNumber + 1 ' Add a "One-Shot" Breakpoint in current file on current line for current thread bps = DTE.Debugger.Breakpoints.Add("", FileName, LineNumber, 1, "$TID == " & ThreadID) ' Run to the next stop DTE.Debugger.Go(True) ' Remove our "One-Shot" Breakpoint For Each bp In bps bp.Delete() Next End SubEnd Module 这篇关于“过渡”在Visual Studio中调试多线程程序时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-13 01:45