基本上我要做的是当应用程序运行时我开始更新股票报价并每隔10秒启动一个计时器(10000毫秒)它将10加到整数变量,间隔为60秒并且我想要一个标签在"中显示"下一次更新在达到分钟后每隔10秒标记一次我希望更新股票报价并重置计时器再次执行此操作,只要程序打开,这将始终运行。
这是我的代码到目前为止,它会跳过if语句,因为当它到达该语句并退出时它不会在10秒内完成。我应该使用do while循环吗?
Imports System.Net
Imports System.IO
Imports Newtonsoft.Json
Public Class Form1
Private Symbols As List(Of String) = Nothing
Private Prices() As List(Of String) = Nothing
Private secondCount As Integer
Private intervalCount As Integer = 60
' force Update
Private Sub forceUpdateBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles forceUpdateBtn.Click
QuoteUpdater()
End Sub
Public Function GetStockPrices(symbol As String) As List(Of String)
' Dim pricesArray() As String
Dim prices As New List(Of String)()
Dim items() As GoogleFinanceItem
If UCase(symbol) = "INDU" Then
Dim url As String = "http://finance.google.com/finance/info?client=ig&q=INDEXDJX%3A.DJI"
Dim result As String = GetWebResponse(url).Replace("//", "")
' Dim newResult = result.Remove(0, 3)
items = JsonConvert.DeserializeObject(Of GoogleFinanceItem())(result)
' pricesArray = Split(result, ":")
Else
Dim url As String = "http://finance.google.com/finance/info?client=ig&q=" & UCase(symbol)
Dim result As String = GetWebResponse(url).Replace("//", "")
' Dim newResult = result.Remove(0, 3)
items = JsonConvert.DeserializeObject(Of GoogleFinanceItem())(result)
' pricesArray = Split(result, ":")
End If
' pricesArray = Split(pricesArray(4), """")
' prices.Add(CSng(Format(pricesArray(1), "0.00")))
'prices.Add(CSng(Format(pricesArray(1))))
prices.Add(items(0).price)
Return prices
End Function
' Get a web response.
Private Function GetWebResponse(ByVal url As String) As String
' Make a WebClient.
Dim web_client As New WebClient()
' Get the indicated URL.
Dim response As Stream = web_client.OpenRead(url)
' Read the result.
Using stream_reader As New StreamReader(response)
' Get the results.
Dim result As String = stream_reader.ReadToEnd()
' Close the stream reader and its underlying stream.
stream_reader.Close()
' Return the result.
Return result
End Using
End Function
Private Sub FillData()
Dim symbolSubSet() As Control = {Label1, Label2, Label3}
Dim priceSubSet() As Control = {Label4, Label5, Label6}
For i = 0 To 2
symbolSubSet(i).Text = Symbols(i)
priceSubSet(i).Text = Prices(i)(0) 'collection inside a collection. so in the first spot the array is in
Next
statusLbl.Text = "Last Updated: " + Now.ToString
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load, updateTimer.Tick
QuoteUpdater()
updateTimer.Start()
If updateTimer.Interval = 10000 Then
secondCount = secondCount + 10
nextUpdateLbl.Text = "in " + CStr(intervalCount - secondCount) + " secs"
End If
End Sub
Private Sub exitBtn_Click(sender As Object, e As EventArgs) Handles exitBtn.Click
Application.Exit()
End Sub
Private Sub QuoteUpdater()
Me.Cursor = Cursors.WaitCursor
' Get the ticker symbols.
'Dim symbols_text() As String = txtSymbols.Text.Split(","c)
Dim symbols_text() As String = {"PKG", "TEN", "INDU"}
Symbols = New List(Of String)()
For i As Integer = 0 To symbols_text.Length - 1
Symbols.Add(symbols_text(i).Trim())
Next i
' Get the data.
ReDim Prices(0 To Symbols.Count - 1)
For i As Integer = 0 To Symbols.Count - 1
Prices(i) = GetStockPrices(Symbols(i))
Next i
' Graph it.
'DrawGraph()
FillData()
Me.Cursor = Cursors.Default
End Sub
End Class
答案 0 :(得分:1)
计时器间隔不会随着时间的推移而改变,因此除非您更改间隔,否则If updateTimer.Interval = 10000...
将始终为真。因为你想每10秒钟做一件事,每分钟做一件事,这只意味着你想要做6次不同的事情。
Private Counter As Int32 = 6
Private Sub updateTimer_Tick(... Handles updateTimer.Tick
Counter += 1
If Counter < 6 Then
lblNext.Text = String.Format("Next Update in {0} secs", (6 - Counter) * 10)
' "early exit"
Return
End If
' stuff to do on the minute
updateTimer.Stop()
QuoteUpdater()
lblNext.Text = "Next Update in 1 minute"
Counter = 1
updateTimer.Start()
End Sub
Private Sub Form1_Load(...) Handles MyBase.Load
' do one time only things like initializing objects and collections
...
lblNext.Text = "First update in 10 secs"
updateTimer.Interval = 10000
updateTimer.Enabled = True
End Sub
代码只计算已经过了多少个间隔,并在计数达到6时进行更新。更新后,它会重置计数器。
我将FormLoad和Tick事件分开了。通过将Counter
初始化为6,它允许第一次更新发生在第一个Tick上。这将允许您在FormLoad中执行只需要执行一次的操作。它还可以改善表单在第一次加载时的外观,因为在进行引用相关的操作之前,它有时间绘制所有内容。
你也可能想重新考虑“在XX秒内进行下一次更新”的想法,因为如果剩下1或9秒,它会说同样的事情。也许打印下次更新的时间?