2016-09-15 52 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

相关问题