4

我正在使用 VB.NET,我有:

  • 一个Start按钮
  • 一个Stop按钮
  • 一个While循环

Start按下按钮时,While循环开始。我想在Stop按下按钮时停止循环。

我曾尝试使用,但按两次按钮Applications.DoEvents时循环停止。Stop

下面是我使用的代码Applications.DoEvents

Dim stopclick As Boolean = False

Private Sub btnPlay_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPlay.Click
    btnForward.Enabled = False
    btnStop.Enabled = True
    While 
        ' Perform the while statements
        If stopclick = True Then
            stopclick = False
            Application.DoEvents()
            Exit While
        End If
    End While
End Sub

Private Sub btnStop_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnStop.Click
    stopClick = True
End Sub
  1. 我用Application.DoEvents错了吗?
  2. 除此以外还有哪些选择Application.DoEvents?如何通过Stop单击按钮停止循环?
4

4 回答 4

5

您需要将调用放在语句Application.DoEvents之外,如下所示:If

While True
    Application.DoEvents()
    ' Perform the while statements
    If stopclick Then
        stopclick = False
        Exit While
    End If
End While

Stop_Click您调用之前,不会有机会被处理DoEvents,因此,按照您的方式,stopClick永远不会设置为True.

但是,这里更大的问题是,调用DoEvents根本是非常糟糕的做法,如果您不小心,可能会导致一些非常难以修复的错误。如果循环在后台线程中执行会好得多。查看该BackgroundWorker组件,了解用于 WinForm 项目的易于实现的线程机制。您可以在表单设计器的工具箱中找到它。

以下是如何使用BackgroundWorker组件执行此操作的示例:

Dim stopclick As Boolean = False

Private Sub btnPlay_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPlay.Click
    btnForward.Enabled = False
    btnStop.Enabled = True
    BackgroundWorker1.RunWorkerAsync()
End Sub

Private Sub btnStop_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnStop.Click
    stopClick = True
End Sub

Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    stopClick = False
    While Not stopClick
        ' Perform the while statements
    End While
End Sub

Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
    btnForward.Enabled = True
    btnStop.Enabled = False
End Sub
于 2013-08-20T15:05:13.303 回答
0

您可以使用 stopClick 作为条件显着简化您的 while 循环:

While Not stopClick
  Application.DoEvents()
End While
于 2013-08-20T15:09:08.860 回答
0

我知道这是一个旧线程,但我找到了一个简单的解决方法。在表单中添加一个 CheckBox(如果有意义的话,它甚至可以“在表单的一侧”)。

 Private Sub btnStopLoop_Click(sender As System.Object, e As System.EventArgs) Handles btnStopLoop.Click

    chkEndLoop.CheckState = CheckState.Checked

 End Sub

然后,在您希望退出的循环中:

 Do Until chkEndLoop.CheckState = CheckState.Checked

   'Do Stuff Here

 Loop

只要CheckBox.CheckState = CheckState.Unchecked,循环就会继续,但是当 CheckBox 变为 Checked 时,循环会退出

于 2014-07-17T00:26:09.003 回答
-1

You have to use btnstop.focus() at the start of the loop. This will be sure to solve your problem.

Example:

Dim stopclick As Boolean = False

Private Sub btnPlay_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPlay.Click
    btnForward.Enabled = False
    btnStop.Enabled = True
    btnStop.Focus()  ' new code---------------------
    While 
        ' Perform the while statements
        If stopclick = True Then
            stopclick = False
            Application.DoEvents()
            Exit While
        End If
    End While
End Sub

Private Sub btnStop_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnStop.Click
    stopClick = True
End Sub
于 2014-09-24T07:30:07.927 回答