VB.NET并行与分布式编程(4)-线程栈[3]

3、运用stacktrace类实现线程栈回溯跟踪

VB.NET并行与分布式编程(4)-线程栈[3]

 代码如下:

Imports System
Imports System.Threading
Imports System.Diagnostics.StackTrace



Module Module1

    Sub Main()
        Dim main_x As Integer
        main_x = 5
        Call sub1(main_x)
    End Sub
    Private Sub sub1(sub1_x As Integer)
        Dim jg As Integer
        jg = sub1_x * sub1_x
        Call sub2(jg)
    End Sub
    Private Sub sub2(sub2_x As Integer)
        Dim jg As Integer
        jg = sub2_x * 2
        jg = jg * jg
        Dim st As New StackTrace(True)
        Console.WriteLine(" 栈跟踪: {0}", _
  st.ToString())
        Dim i As Integer

        For i = 0 To st.FrameCount - 1

            Dim sf As StackFrame = st.GetFrame(i)
            Console.WriteLine()
            Console.WriteLine("回溯调用栈, 方法: {0}", _
                sf.GetMethod())

            Console.WriteLine("回溯调用栈, 行号 : {0}", _
                sf.GetFileLineNumber())
        Next i





    End Sub

End Module

下面这句定义了StackTrace 类的新实例,实现线程栈跟踪

StackTrace 是使用调用方的当前线程创建的,参数含义如下:

如果为 true,则捕获文件名、行号和列号;否则为 false

 Dim st As New StackTrace(True)

你可能感兴趣的:(VB.NET)