0

I have a start button stop button

start button function is running

How do I stop this function with the stop button?

  Private Sub startbutton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
    oto("x")
End Sub

Private Sub stopbutton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
    'stop()
End Sub

I can stop the function at any time with a button

Function oto(ByVal url As String)
    If CBool(InStr(LCase(anasayfa.WebBrowser1.Url.ToString), url)) = False Then : Return False : End If
    Dim sourcecode = anasayfa.WebBrowser1.Document
    Dim linkler = sourcecode.GetElementsByTagName("a")
    Dim i = 0 : Dim b = 0 : Dim l = 0
    If linkler.Count <= 0 Then : Return False : End If

    For Each kelime In kelimelistesi()
        Application.DoEvents() : i = 0
        For Each link As HtmlElement In linkler
            Application.DoEvents() : i = i + 1
            If Not link.GetAttribute("target") = "_blank" Then
                If link.GetAttribute("onclick") = "" Then
                    If CBool(InStr(link.GetAttribute("href").ToString, kelime)) = True Then : b = i : Exit For : End If
                End If
            End If
        Next
        If b > 0 Then : Exit For : End If
    Next
    If b > linkler.Count Then : Return False : End If
    If b > 0 Then : l = b - 1 : anasayfa.ToolStripStatusLabel1.Text = "İlgili link bulundu : " & linkler.Item(l).InnerHtml.ToString() : linkler.Item(l).InvokeMember("click") : waitdocumentloading() : Return True : Else : anasayfa.ToolStripStatusLabel1.Text = "İlgili link bulunamadı" : Return False : End If
End Function
ömer özer
  • 59
  • 2
  • 4

2 Answers2

1

You can Start a thread which runs off the UI, and Abort the thread when you want to stop it.

Private t As New System.Threading.Thread(AddressOf oto)

Private Sub startbutton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
    t.Start("x")
End Sub

Private Sub stopbutton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
    t.Abort()
End Sub

Function oto(ByVal url As String)
    Try
        If CBool(InStr(LCase(anasayfa.WebBrowser1.Url.ToString), url)) = False Then : Return False : End If
        Dim sourcecode = anasayfa.WebBrowser1.Document
        Dim linkler = sourcecode.GetElementsByTagName("a")
        Dim i = 0 : Dim b = 0 : Dim l = 0
        If linkler.Count <= 0 Then : Return False : End If
        For Each kelime In kelimelistesi()
            i = 0
            For Each link As HtmlElement In linkler
                i = i + 1
                If Not link.GetAttribute("target") = "_blank" Then
                    If link.GetAttribute("onclick") = "" Then
                        If CBool(InStr(link.GetAttribute("href").ToString, kelime)) = True Then : b = i : Exit For : End If
                    End If
                End If
            Next
            If b > 0 Then : Exit For : End If
        Next
        If b > linkler.Count Then : Return False : End If
        If b > 0 Then
            l = b - 1
            anasayfa.ToolStripStatusLabel1.Invoke(Sub() anasayfa.ToolStripStatusLabel1.Text = "İlgili link bulundu : " & linkler.Item(l).InnerHtml.ToString())
            linkler.Item(l).InvokeMember("click")
            waitdocumentloading()
            Return True
        Else
            anasayfa.ToolStripStatusLabel1.Invoke(Sub() anasayfa.ToolStripStatusLabel1.Text = "İlgili link bulunamadı")
            Return False
        End If
    Catch ex As System.Threading.ThreadAbortException
        ' need to do something when aborting?
    End Try
    Return False
End Function

You then need to Invoke UI thread operations on the UI thread (Control.Invoke). This also eliminates the need to call Application.DoEvents because the processing isn't on the UI anymore.

This is a simple solution but there are more elegant ways to do work on a non-UI thread. You should search online on that topic.

djv
  • 15,168
  • 7
  • 48
  • 72
  • I think the general idea is fine, but note that `Thread.Abort()` is generally considered to be a bad idea, also see https://stackoverflow.com/questions/1559255/whats-wrong-with-using-thread-abort/1560567#1560567 – Craig Nov 27 '17 at 19:56
0

Declare a form level boolean variable = False. In the click event of the Stop button set it to true. In the inner For each loop:

If bolStop Then
    Exit Function
End If

Not elegant; but it should work. If you want to Start again add a line above the Exit Function to set the bolStart back to False. I just noticed, noticed that your Function doesn't seem to be returning anything so should really be a Sub.

Mary
  • 14,926
  • 3
  • 18
  • 27
  • As a code example, I have a code with multiple loops, and the last line is restarting the loop – ömer özer Nov 27 '17 at 17:38
  • @ömerözer your posted code shows 2 loops an outer For Each and an inner For Each. Yes, the last line of the loop, the Next statement, restarts the loop. The Exit Function statement will leave the loop and the function and return to the calling code. – Mary Nov 28 '17 at 08:39