VB.Net线程挂起和恢复

VB.Net Threading suspending and resuming

好的,我们需要为学校制作一个线程程序来暂停和恢复。它应该从 0 数到 6,然后倒数到零,然后再倒数。

然而,它不是在计算时打印值,而是只打印一次值 1。代码如下:

Module Module1
Public A As System.Threading.Thread = New Threading.Thread(AddressOf MethodA)
Public B As System.Threading.Thread = New Threading.Thread(AddressOf MethodB)


Sub MethodA()
    Dim C As Integer
    C = 0
    C = C + 1
    If C = 6 Then
        B.Start()
        If A.ThreadState = Threading.ThreadState.Running Then
            A.Suspend()
        End If
    End If
    Console.Write(C)
End Sub

Sub MethodB()
    Dim C As Integer
    C = 6
    C = 6 - 1
    For C > 0
        Console.Write(C)
    Next

    If A.ThreadState = Threading.ThreadState.Suspended Then
        A.Resume()
    End If
End Sub

Sub Main()
    A.Start()
    Console.ReadLine()
End Sub

End Module

查看您的代码,我猜您的控制台只输出 1,然后等待用户退出。请参阅下面我的评论。

Sub MethodA()
    Dim C As Integer
    C = 0
    C = C + 1 'set C equal to 0 + 1
    If C = 6 Then 'C equals 1, not 6
        B.Start()
        If A.ThreadState = Threading.ThreadState.Running Then
            A.Suspend()
        End If
    End If
    Console.Write(C) 'Write C, which is 1
End Sub

此外,您还有:

For C > 0
    Console.Write(C)
Next

但我很确定正确的语法是:

For C = 6 To 0 Step -1
    Console.Write(C)
Next

这会让你重新开始。

编辑: 基本上正是@Capellan 在他的评论中所说的。