VB.Net线程暂停和恢复

时间:2015-12-17 18:51:12

标签: vb.net multithreading resume suspend

好的,对于学校我们需要制作一个暂停和恢复的线程程序。它应该从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 个答案:

答案 0 :(得分:1)

查看你的代码,我猜你的控制台只输出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在评论中所说的。