多线程类无法正常工作

时间:2014-03-15 15:48:25

标签: vb.net multithreading class

我正在尝试制作一个程序,当用户按住左箭头键或右箭头键以及空格键时,它会拍摄一些向上移动的红色图片框直到被删除,当我创建一个它工作正常但是当我创建两个或更多时,第一个图片框停止移动,第二个图片框开始移动,如果有三个图片框,那么第一个和第二个将停止,第三个将开始,一旦第三个完成,然后第二个再次启动然后第一个会开始。我想要它所以它们都在同一时间不是一个接一个地移动。我有一个主表单和一个类,这里​​是表单代码:

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

这是类代码:

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

我认为应用程序需要更快地更新或类似的东西,我不太确定。

0 个答案:

没有答案
相关问题