我試圖模仿VB Express 2013中的「賽馬」,這暴露了我對線程如何與Windows窗體一起工作的理解上的差距。這個想法是當按下表單上的一個按鈕時,一個藍色方塊和一個紅色方塊(馬)從左到右穿過一個表單(它有一個白色背景)。運動的每個增量應該是(僞)隨機生成的,所以獲勝者應該是不可預測的。在窗體上創建多個獨立的線程
這是我用過的代碼。廣場的移動確實是隨機的,但他們一起移動,而不是獨立的,所以比賽總是一條平行線。如果我只在其中一個線程程序中包含隨機化,它表現得更像是一場比賽,但勝者通常是可預測的。有人能告訴我我錯過了什麼嗎?感謝預期。
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim t1 As System.Threading.Thread
Dim t2 As System.Threading.Thread
t1 = New System.Threading.Thread(AddressOf RedHorse)
t2 = New System.Threading.Thread(AddressOf BlueHorse)
t1.Start()
t2.Start()
End Sub
Public Sub RedHorse()
Randomize()
Dim G As Graphics
G = Me.CreateGraphics
Dim bRed As Brush
bRed = New SolidBrush(Color.Red)
Dim bWhite As Brush
bWhite = New SolidBrush(Color.White)
Dim x1 As Integer
x1 = 100
G.FillRectangle(bWhite, x1, 100, 50, 50)
Do Until x1 >= 800
x1 = x1 + Rnd()
G.FillRectangle(bRed, x1, 100, 50, 50)
Threading.Thread.Sleep(1)
G.FillRectangle(bWhite, x1, 100, 50, 50)
Loop
End Sub
Public Sub BlueHorse()
Randomize()
Dim G As Graphics
G = Me.CreateGraphics
Dim bBlue As Brush
bBlue = New SolidBrush(Color.Blue)
Dim bWhite As Brush
bWhite = New SolidBrush(Color.White)
Dim x2 As Integer
x2 = 100
G.FillRectangle(bWhite, x2, 200, 50, 50)
Do Until x2 >= 800
x2 = x2 + Rnd()
G.FillRectangle(bBlue, x2, 200, 50, 50)
Threading.Thread.Sleep(1)
G.FillRectangle(bWhite, x2, 200, 50, 50)
Loop
End Sub
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
Dim G As Graphics
G = Me.CreateGraphics
Dim bRed As Brush
bRed = New SolidBrush(Color.Red)
Dim bBlue As Brush
bBlue = New SolidBrush(Color.Blue)
G.FillRectangle(bRed, 100, 100, 50, 50)
G.FillRectangle(bBlue, 100, 200, 50, 50)
End Sub
End Class
除了Randomize問題,我不認爲你可以從線程更新圖形。在線程中進行位置計算並使用計時器以計算位置更新HMI。 – Graffito