我使用以下方法成功地从 Web 检索数据:
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]
正确的方法是什么?
我会使用Microsoft的响应式框架(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()
关闭订阅。
您可以在循环中尝试 Sleep 方法吗?
For i = 1 To 5
System.Threading.Thread.Sleep(300000) '// 5 mins in milliseconds
'// Do Something
Next i