本文介绍了如何刷新backgroundworker - VB.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在点击按钮时刷新后台工作人员进行刷新。这是为了在刷新后刷新标签。但是,当我这样做时,我收到错误后台工作人员很忙。所以我只希望背景工作者等到忙碌结束再次运行它。请帮助。
我尝试过:
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
Try
Dim ur As New Uri("http://www.find-ip-address.org/")
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)
Else
readStream = New StreamReader(receiveStream, Encoding.GetEncoding(res.CharacterSet))
Dim data As String = readStream.ReadToEnd()
res.Close()
readStream.Close()
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
Catch
End Try
End Sub
Private Function extractSubject(ByVal left As String, ByVal right As String)
Try
Dim indexLeft As Integer = lookupData.IndexOf(left)
Dim indexRight As Integer = lookupData.IndexOf(right)
Return lookupData.Substring(indexLeft + left.Length, indexRight - indexLeft - left.Length)
Catch
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()
BackgroundWorker1.CancelAsync()
End Sub
推荐答案
这篇关于如何刷新backgroundworker - VB.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!