Multi threading classes not working correctly

2019-08-30 21:01发布

问题:

I'm trying to make a program that when the user holds down the left or right arrow key along with the space bar it shoots some red picture boxes that move upwards until the get removed, when I just create one it works fine but when I create two or more the first picture box stops moving and the second one starts moving and if there were three picture boxes then the first and second would stop and the third would start, once the third has finished then the second starts again and then the first would start. I want it so they all move at the same time not one after another. I have a main form and a class, here's the form code:

Public Class Form1
Public bool As Boolean
Public Pressed As New HashSet(Of Keys)
Public shots As New Shots
Public counter As Integer = 0

Private Sub Main_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
    Pressed.Add(e.KeyCode)
    If PictureBox1.Location.X <> 0 Then
        If Pressed.Contains(Keys.Left) Then
            PictureBox1.Location = New Point(PictureBox1.Location.X - 10, PictureBox1.Location.Y)
            If Pressed.Contains(Keys.Left) AndAlso Pressed.Contains(Keys.Space) Then
                shots = New Shots
                shots.main(Me.PictureBox1.Location.X, Me.PictureBox1.Location.Y, counter)
                counter += 1
            End If
            Application.DoEvents()
        End If
    Else
        If Pressed.Contains(Keys.Right) Then
            bool = True
            PictureBox1.Location = New Point(PictureBox1.Location.X + 10, PictureBox1.Location.Y)
            If Pressed.Contains(Keys.Right) AndAlso Pressed.Contains(Keys.Space) Then
                shots = New Shots
                shots.main(Me.PictureBox1.Location.X, Me.PictureBox1.Location.Y, counter)
                counter += 1
            End If
            Application.DoEvents()
        End If
    End If

    If PictureBox1.Location.X <> 540 Then
        If bool <> True Then
            If Pressed.Contains(Keys.Right) Then
                PictureBox1.Location = New Point(PictureBox1.Location.X + 10, PictureBox1.Location.Y)
                If Pressed.Contains(Keys.Right) AndAlso Pressed.Contains(Keys.Space) Then
                    shots = New Shots
                    shots.main(Me.PictureBox1.Location.X, Me.PictureBox1.Location.Y, counter)
                    counter += 1
                End If
                Application.DoEvents()
            End If
        Else
            bool = False
        End If
    Else
        If Pressed.Contains(Keys.Left) Then
            PictureBox1.Location = New Point(PictureBox1.Location.X - 10, PictureBox1.Location.Y)
            If Pressed.Contains(Keys.Left) AndAlso Pressed.Contains(Keys.Space) Then
                shots = New Shots
                shots.main(Me.PictureBox1.Location.X, Me.PictureBox1.Location.Y, counter)
                counter += 1
            End If
            Application.DoEvents()
        End If
    End If
End Sub
Private Sub Main_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
    Pressed.Remove(e.KeyCode)
End Sub
End Class

and here's the class code:

Imports System.Threading.Thread
Public Class Shots
Public Const count As Integer = 100
Public PictureBoxes(count - 1) As PictureBox
Public Sub main(x, y, v)
    PictureBoxes(v) = New PictureBox
    PictureBoxes(v).BackColor = Color.Red
    PictureBoxes(v).Visible = True
    PictureBoxes(v).Location = New System.Drawing.Point(x + 15, y)
    PictureBoxes(v).Size = New System.Drawing.Size(10, 50)
    Form1.Controls.Add(PictureBoxes(v))
    For z = 0 To 10
        PictureBoxes(v).Location = New Point(PictureBoxes(v).Location.X, PictureBoxes(v).Location.Y - 10)
        Application.DoEvents()
        Threading.Thread.Sleep(10)
    Next
    Form1.Controls.Remove(PictureBoxes(v))
End Sub
End Class

I think the application needs to update faster or something like that, I'm not too sure.