vb.net 如何通过单击按钮打破循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18338343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 17:05:59  来源:igfitidea点击:

How to break a loop with a single button click

vb.netloopsbuttonclickbreak

提问by user2150279

I am using VB.NET and I have:

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

  • a Startbutton
  • a Stopbutton
  • a Whileloop
  • 一个Start按钮
  • 一个Stop按钮
  • 一个While循环

When the Startbutton is pressed, the Whileloop begins. I want to stop the loop when the Stopbutton is pressed.

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

I had tried to use Applications.DoEvents, but the loop stops when the Stopbutton is pressed twice.

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

Below is my code using Applications.DoEvents

下面是我的代码使用 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. Have I used Application.DoEventsincorrectly?
  2. What are the other options besides Application.DoEvents? How can I stop the loop with a single Stopbutton click?
  1. 我用Application.DoEvents错了吗?
  2. 除此之外还有哪些选择Application.DoEvents?如何通过Stop单击按钮停止循环?

回答by Steven Doggart

You need to put the call to Application.DoEventsoutside of your Ifstatement, like this:

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

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

The Stop_Clickwon't have a chance to be processed until you call DoEvents, so, with the way you had it, stopClickwould never get set to True.

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

However, the larger issue, here, is that calling DoEvents, at all, is very bad practice and can lead to some very difficult-to-fix bugs if you aren't very careful. It would be far better if the loop was performed in a background thread. Check out the BackgroundWorkercomponent for an easy to implement threading mechanism for WinForm projects. You'll find it in the tool box of your form designer.

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

Here's an example of how to do it with the BackgroundWorkercomponent:

以下是如何使用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

回答by Luke

You can simplify your while loop significantly by using stopClick as your condition:

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

While Not stopClick
  Application.DoEvents()
End While

回答by rogue5pawn

I know this is an old thread, but I found a simple workaround. Add a CheckBox to the form (it can even be "off the side of the form," if that made sense).

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

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

    chkEndLoop.CheckState = CheckState.Checked

 End Sub

Then, in the Loop you wish to exit:

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

 Do Until chkEndLoop.CheckState = CheckState.Checked

   'Do Stuff Here

 Loop

As long as the CheckBox.CheckState = CheckState.Unchecked, the Loop will continue, but when the CheckBox becomes Checked, the Loop will Exit

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

回答by Linendra Soni

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

您必须btnstop.focus()在循环开始时使用。这一定能解决您的问题。

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