How can I break a loop with a button and make it responsive when it works?

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

    Private 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 is happening right now is that it starts and freezes during the cycle.

I would like another button to stop the loop when pressed.

Also, the Sleep function does not work, because I get a β€œHi” warning every milliseconds.

+3
source share
2 answers

you need to run a long operation in the background thread so as not to block the user interface thread, for example the BackgroundWorker class in C #, I don’t know if it is available in vb.

+2
source

You can use BackgroundWorker in VB.

, , , application.doevents . , , , .

0

Source: https://habr.com/ru/post/1731746/


All Articles