2016-09-15 11 views
0

基本的には、アプリケーションの実行時です。株価の更新を開始し、10秒ごとにタイマーを開始します10000ミリ秒)整数変数に10を加え、間隔は60秒です。分に達すると10秒ごとにラベルに「次回の更新」というラベルを表示したいです。株式相場を更新し、タイマーをリセットしたいプログラムを開いている間は常に実行されます。タイマーを使って10秒ごとに1つのことを行いますが、分単位で何か他のことをします

これはこれまでのコードですが、if文をスキップします。これは、その文に達してから10秒後に終了するためです。 do 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...は常にtrueになります。 10秒ごとに1回、1分ごとに1回行うことを望むので、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で一度だけ行う必要があることを実行できます。また、見積もりに関連するものを実行する前にすべてをペイントする時間があるため、フォームが最初に読み込まれるときにフォームがどのように見えるかを改善することができます。

1秒または9秒が残っていても同じことが言えるので、「次のXX秒の更新」の考え方を再考することもできます。たぶん、次の更新の時刻を印刷しますか?

+0

この間隔を15分にして、毎分ラベルを更新したい場合は、間隔を900,000に変更して15に調整します。次に、updateTimerメソッドで6を変更して15にし、カウンター)* 10)* 10を外しました。私は何もしないと思う。何が起こっているのかを理解しようとすると、タイマーのティックメソッドが呼び出されます。 – Edgar

関連する問題