多线程程序的调试是一件比较麻烦的事,在我的这篇博文里介绍了两个方便多线程调试的特性(一个是VS特性,一个是John Robbin的InterestingThread宏),今天我再来介绍John Robbin的另一个宏。如果你希望在调试多线程程序时只运行一个线程,而将其它的线程全部暂时Freeze掉,请尝试使用FreezeThawThreads宏。John Robbin原文帖出来的宏代码有一些排版的错误,导致不能编译通过,我已经做了修改,下面是可以编译通过的代码版本:
FreezeThawThreads
1 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
2 '' Wintellect Debugging Code
3 '' Copyright © 1997-2009 John Robbins
4 ' -- All rights reserved.
5 '' Freeze and thaw threads in bulk.
6 ''
7 '' Version 1.0 - July 17, 2009
8 '' - Initial version.
9 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
10 Imports System
11 Imports EnvDTE
12 Imports EnvDTE80
13 Imports EnvDTE90
14 Imports System.Diagnostics
15 Imports System.Text
16 Imports System.Collections.Generic
17 Imports System.Runtime.InteropServices
18 Imports System.Windows.Forms
19 Public Module FreezeThawThreads
20 Public Sub FreezeAllButActiveThread()
21 Dim dbg As EnvDTE90.Debugger3 = CType(DTE.Debugger, Debugger3)
22 If dbg.CurrentMode = dbgDebugMode.dbgBreakMode Then
23 Dim currProg As Program = dbg.CurrentProgram
24 For Each t As Thread2 In currProg.Threads
25 If (t.ID <> dbg.CurrentThread.ID) Then
26 If t.IsFrozen = False Then
27 t.Freeze()
28 End If
29 End If
30 Next
31 Else
32 NotInBreakMode()
33 End If
34 End Sub
35 Public Sub ThawAllFrozenThreads()
36 Dim dbg As EnvDTE90.Debugger3 = CType(DTE.Debugger, Debugger3)
37 If dbg.CurrentMode = dbgDebugMode.dbgBreakMode Then
38 Dim currProg As Program = dbg.CurrentProgram
39 For Each t As Thread2 In currProg.Threads
40 If t.IsFrozen = True Then
41 t.Thaw()
42 End If
43 Next
44 Else
45 NotInBreakMode()
46 End If
47 End Sub
48 Private Sub NotInBreakMode()
49 MessageBox.Show(New MainWindow(), "You mustbe stopped in the debugger for this macro to work", "Wintellect Thread Freeze/Thaw Macros", MessageBoxButtons.OK, MessageBoxIcon.Error)
50 End Sub
51 ' A helper class so I can parent message boxes correctly on the IDE.
52 Class MainWindow
53 Implements IWin32Window
54 Public ReadOnly Property Handle() _
55 As System.IntPtr Implements IWin32Window.Handle
56 Get
57 ' The HWnd property is undocumented.
58 Dim ret As IntPtr = CType(DTE.MainWindow.HWnd, IntPtr)
59 Return (ret)
60 End Get
61 End Property
62 End Class
63 End Module
64