Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Displaying Real-Time Stock Information Using Multithreading : Page 3

Learn to use multithreading to build applications whose UI remains active even while performing time-consuming or resource-intensive background tasks.




Full Text Search: The Key to Better Natural Language Queries for NoSQL in Node.js

Date: 1/31/2018 @ 2 p.m. ET

Activating the Chart
Next, import the following namespaces (at the top of the code window):

Imports SoftwareFX.ChartFX Imports System.Threading

Declare a global variable called t1 to be used for threading:

Dim t1 As Thread

In the Chart1_Load event, initialize the Chart component:

Private Sub Chart1_Load(ByVal sender As _ System.Object, ByVal e As System.EventArgs) _ Handles Chart1.Load '---show the time on the x-axis every 5 point--- Chart1.AxisX.Step = 5 '---use 5 pixels to separate between each point--- Chart1.AxisX.PixPerUnit = 5 '---make chart scrollable--- Chart1.Scrollable = True '---Open and close the communication channel--- Chart1.OpenData(COD.Values, 1, COD.Unknown) Chart1.CloseData(COD.Values) End Sub

Add a new class named StockQuote to the current form. The StockQuote class invokes the Web service and uses the returned stock price to update the chart.

Public Class StockQuote '---number of graph to plot in a component Const NUM_SERIES = 1 Private lastPoint As Integer = 0 Dim stockPrice As Single Private pStockSymbol As String Private pStockSeries As Integer = 0 Private pChartControl As Chart WriteOnly Property StockSymbol() Set(ByVal Value) pStockSymbol = Value End Set End Property WriteOnly Property ChartControl() Set(ByVal Value) pChartControl = Value End Set End Property Public Sub InvokeWebService() Dim ws As New StockWS.Service1 For i As Integer = 0 To 10000 stockPrice = ws.getPrice(pStockSymbol) pChartControl.Invoke(New _ myDelegate(AddressOf updateChart), _ New Object() {}) '---wait for 1 second before continuing Thread.Sleep(1000) Next End Sub Public Delegate Sub myDelegate() Public Sub updateChart() pChartControl.OpenData(COD.Values, NUM_SERIES, _ COD.Unknown) pChartControl.Value(pStockSeries, lastPoint) = _ stockPrice '---displays the time on the x-axis pChartControl.AxisX.Label(lastPoint) = _ DateTime.Now.ToShortTimeString lastPoint += 1 pChartControl.CloseData(COD.Values) '---move the scroll bar to the rightmost pChartControl.AxisX.ScrollPosition = _ pChartControl.AxisX.ScrollSize End Sub End Class

You pass the required stock symbol to the StockQuote class via the StockSymbol property, and set the chart to update using the ChartControl property. The InvokeWebService() method calls the Web service repeatedly in a loop (set to 10,000 in this example). As this class will execute in a separate thread, you must take special care to ensure that you do not update a Windows control directly, because Windows controls are not thread-safe. Instead, you use a delegate and call the Invoke() method of the control you want to update. The code calls the Web service every second, as evidenced by the Thread.Sleep(1000) statement.

To start the thread to update the chart with the latest stock information, add code to the Get Stock Quote button's Click event:

Private Sub btnGetStockQuote1_Click( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnGetStockQuote1.Click Dim sq As New StockQuote sq.StockSymbol = cmbStocks1.SelectedItem sq.ChartControl = Chart1 t1 = New Thread(AddressOf sq.InvokeWebService) t1.Start() End Sub

Figure 5. Testing the Application: When you select a stock symbol and click the Get Stock Quote button, the results of the repeated calls to the Web service appear on the chart; however, because the Web service runs on a background thread, calling it doesn't prevent normal UI operations.
The main reason to package the code to invoke the Web service as a class is that the Thread class constructor accepts only a ThreadStart delegate (the delegate that represents the method in which to start the thread), and there's no overloaded Thread.Start() method that accepts parameter values. Hence the only way to pass parameters into a thread is to package the relevant code to invoke as part of a class. You can then pass parameters in via properties of that class.

To test the code, press F5. Select a stock and click on the Get Stock Quote button. You should now be able to move the window (proving that the UI isn't locked by the repeated Web service calls) and at the same time see the chart update with the latest stock information (see Figure 5).

Author's Note: The pricing for the stock selected in Figure 5 is only a simulated one; it is not the actual stock price.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date