2016-09-15 48 views
0

基本上什麼,我試圖做的應用程序運行時,我開始一個更新的股票報價,並開始每隔10秒的定時器(這是10000毫秒)它會將10添加到一個整數變量的時間間隔爲60秒,我希望標籤在達到分鐘後每隔10秒顯示一個「下一次更新」標籤我希望更新股票行情並重置計時器它會重新開始,只要程序打開,它就會一直運行。使用定時器做一兩件事,每10秒,但做別的事情在分鐘的間隔

這是到目前爲止我的代碼,它跳過if語句,因爲它不是在10秒,當它到達該語句並退出。我應該使用一個while while循環嗎?

Imports System.Net 
Imports System.IO 
Imports Newtonsoft.Json 

公共類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 

回答

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和蜱的活動。通過初始化Counter爲6,它允許第一次更新發生在第一個Tick上。這將允許您在FormLoad中執行只需要執行一次的操作。它也可以改善表單在第一次加載時的樣子,因爲它在引用相關內容之前有時間繪製所有內容。

你也可能要重新考慮「在XX秒下次更新」的主意,因爲它會說同樣的事情,如果有剩下1或9秒。也許打印下次更新的時間?

+0

如果我想使這個間隔15分鐘,並已將其更新的每一個標籤分鐘,我改變了間隔90萬和專櫃15.然後我更改updateTimer方法6的,讓他們和15後(15〜櫃檯)* 10)我脫掉了* 10。我沒有看到它做任何事情。計時器滴答法如何被調用來試圖理解正在發生的事情。 – Edgar

相關問題