我使用以下方法成功地从网上检索数据:
Dim webClient As New System.Net.WebClient
Dim result As String = webClient.DownloadString("http://www.example.org")
Dim o As Object
Dim dizi As String() = result.Split(New String() {",,,"}, StringSplitOptions.None)
' urls on the webpage are seperated with ,,, so it gets the first website
Dim urladdress As String = dizi(0)
o.Navigate2(urladdress)
但是,我需要为它添加时间循环。例如,它需要每5分钟检索一次数据。在没有任何问题的情况下尝试了这个:
Imports System.Timers
Public Class TimerRequest
Private Shared aTimer As Timer
Private Shared o as Object
Public Shared Sub Main()
aTimer = New System.Timers.Timer(300000) ' 5 minutes
AddHandler aTimer.Elapsed, AddressOf OnTimedEvent
aTimer.Enabled = True
End Sub
Private Shared Sub OnTimedEvent(source As Object, e As ElapsedEventArgs)
'----------------------------------------
Dim webClient As New System.Net.WebClient
Dim result As String = webClient.DownloadString("http://www.example.org")
Dim o As Object
Dim dizi As String() = result.Split(New String() {",,,"}, StringSplitOptions.None)
' urls on the webpage are seperated with ,,, so it gets the first website
Dim urladdress As String = dizi(0)
o.Navigate2(urladdress)
'----------------------------------------
End Sub
End Class
这些是错误
![在此处输入图片说明] [1]
这样做的正确方法是什么?
答案 0 :(得分:2)
我会使用Microsoft的Reactive Framework(NuGet“Rx-Main”)来执行此操作。
以下是您需要的全部代码:
Dim subscription = _
Observable _
.Interval(TimeSpan.FromMinutes(5.0)) _
.StartWith(-1L) _
.SelectMany( _
Observable _
.Using( _
Function() New WebClient(), _
Function(wc) _
wc.DownloadStringTaskAsync("http://www.example.org") _
.ToObservable())) _
.Select(Function(result) _
result.Split(New String() {",,,"}, StringSplitOptions.None)(0)) _
.Subscribe(Sub(urladdress) o.Navigate2(urladdress))
这将自动下载您的网页并每5分钟解析一次urladdress
。
好消息是您可以致电subscription.Dispose()
来关闭订阅。
答案 1 :(得分:0)
您可以在循环中尝试Sleep方法吗?
For i = 1 To 5
System.Threading.Thread.Sleep(300000) '// 5 mins in milliseconds
'// Do Something
Next i