Click here to Skip to main content
15,665,276 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to refresh the backgroundworker to refresh when I click on a button. This is for the label to refresh just after the refresh. However, when I do that, I am getting error Background worker is busy. So I want only the backgroundworker to wait until the busy is over and run it again. Kindly help.

What I have tried:

Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        BackgroundWorker1.WorkerSupportsCancellation = True

            Dim ur As New Uri("")
            Dim req As HttpWebRequest = HttpWebRequest.Create(ur)
            Dim res As HttpWebResponse = req.GetResponse()
            If (res.StatusCode = HttpStatusCode.OK) Then
                Dim receiveStream As Stream = res.GetResponseStream()
                Dim readStream As StreamReader = Nothing
                If (res.CharacterSet = Nothing) Then
                    readStream = New StreamReader(receiveStream)
                    readStream = New StreamReader(receiveStream, Encoding.GetEncoding(res.CharacterSet))
                    Dim data As String = readStream.ReadToEnd()
                    Dim left = "My IP Country Name:"
                    Dim right = "IP Address Lookup Location"
                    Dim indexLeft As Integer = data.IndexOf(left)
                    Dim indexRight As Integer = data.IndexOf(right)
                    lookupData = data.Substring(indexLeft + left.Length, indexRight - indexLeft - left.Length)
                    flagLocation = extractSubject("</font>  <img src='", "'><br>My IP Country Continent<")
                End If
            End If
        End Try
    End Sub
    Private Function extractSubject(ByVal left As String, ByVal right As String)
            Dim indexLeft As Integer = lookupData.IndexOf(left)
            Dim indexRight As Integer = lookupData.IndexOf(right)
            Return lookupData.Substring(indexLeft + left.Length, indexRight - indexLeft - left.Length)
            Return "Not Resolved!"
        End Try
    End Function
#Region "informations"
    Private Function publicIP() As String
        Return extractSubject(">My IP Address lookup for ", " show IP which")
    End Function
#End Region
    Public Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
        tbPublicIP.Text = publicIP()
    End Sub
Updated 26-Jul-18 10:13am
Gerry Schmitz 26-Jul-18 3:48am    
Where's your "worker" code? That's where a "cancel" is recognized; not in anything you've shown.
Member 13857021 26-Jul-18 4:25am    
Edited the code. The button should refresh the worker to get the new IP.

1 solution

1) I suggest you study the sample BGW code and learn how to code a BGW properly.

BackgroundWorker Class (System.ComponentModel)[^]

2) Since the web can be accessed asynchronously, your use of BGW is not the best choice either.

Walkthrough: Accessing the Web by Using async and await (C#) | Microsoft Docs[^]
Share this answer
Member 13857021 26-Jul-18 22:06pm    
I have used BUsy with while along with Backgroundworker only. That runs well. Thank you for the help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900