Mohamed Elmalah Mohamed Elmalah - 5 months ago 28
Vb.net Question

VB.Net Threading suspending and resuming

Okay so for school we need to make a threading program that suspends and resumes. It is supposed to count from 0 to 6 then back down to zero then back up again.

However instead of printing the values as it counts, it instead just prints the value of 1 a single time. The code is as follows:

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

Answer

Looking at your code, I'm guessing what is happening is your console only outputs 1, and then waits for the user to exit. See my comments below.

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

Also, you have:

For C > 0
    Console.Write(C)
Next

But I'm pretty sure the proper syntax would be:

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

That should get you going again.

Edit: Basically exactly what @Capellan said in his comment.

Comments