0

我正在尝试为事件制作一个每秒更改一次的标签。我目前拥有的内容使线程从“Rock”一直到“Shoot”并跳过“Paper”和“Scissors”。有没有人知道我应该怎么做才能正确显示标签,以便它们都以相等的时间增量显示?任何帮助将非常感激。

lblCountdown.Text = "Rock"
System.Threading.Thread.Sleep(1000)
lblCountdown.Text = "Paper"
System.Threading.Thread.Sleep(1000)
lblCountdown.Text = "Scissors"
System.Threading.Thread.Sleep(1000)
lblCountdown.Text = "Shoot!"
4

2 回答 2

1

如果您从 UI 线程调用它,渲染将锁定直到代码完成,因此您不会看到 Paper 或 Scissors。一种解决方案(不是最好的解决方案,而是一种解决方案)是在每个. 这将告诉 UI 重绘自己。Me.Refresh()Application.DoEvents()Sleep

更好的解决方案是使用某种以 1 秒为间隔计时的计时器,该计时器将在另一个线程中运行,从而为更重要的事情释放 UI 线程。

于 2012-04-11T21:00:34.550 回答
0

计时器和调用将是处理此问题的好方法:

Private WithEvents m_tmrGame As New System.Timers.Timer(1000)
Private m_intShouldBeAnEnum As Integer = 1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    m_tmrGame.Start()

End Sub

Private Sub m_tmrGame_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles m_tmrGame.Elapsed

    Dim strCurrentObject As String = String.Empty

    Select Case m_intShouldBeAnEnum
        Case 1
            strCurrentObject = "Rock"
            m_intShouldBeAnEnum = 2
        Case 2
            strCurrentObject = "Paper"
            m_intShouldBeAnEnum = 3
        Case 3
            strCurrentObject = "Scissors"
            m_intShouldBeAnEnum = 4
        Case 4
            strCurrentObject = "Shoot!"
            m_intShouldBeAnEnum = 1
            m_tmrGame.Stop()
    End Select

    lblCountdown.Invoke(Sub(lblTarget As Label) lblTarget.Text = strCurrentObject, New Object() {lblCountdown})

End Sub
于 2012-04-11T21:57:48.873 回答