How can i break loop with a button and make it responsive as it runs?
Imports System
Imports System.Net
Imports System.IO
Imports System.Threading.Thread
Public Class Form1
Public errorz As String
Public votestatus As String
Public videoid As String
Public votetries As String
Public Sub Main()
End Sub
Priv开发者_StackOverflow中文版ate Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If RadioButton1.Checked Then
votestatus = "YES"
End If
If RadioButton2.Checked Then
votestatus = "NO"
End If
videoid = TextBox1.Text
votetries = TextBox2.Text
For i As Integer = 1 To votetries Step 1
MsgBox("Hi")
Sleep(5000)
Next
End Sub
End Class
What happens right now is that it runs, and freezes during the loop.
I would like another button when clicked will stop the loop.
Also the Sleep function doesn't seem to be working because I get "Hi" alert every millisecond.
you have to run the long operation in a background thread in order not to block the UI thread, such as BackgroundWorker class in c#, don't know if it is available in vb.
You can use a BackgroundWorker in VB.
Alternatively, if you need to make sure buttons are processed while a loop is executing, you can add application.doevents
inside the loop. You can use event handlers for the buttons you want to use to interrupt the loop, and set a flag variable that the loop can check for.
精彩评论