Hi
This is a fully working version. The results as shown below using the code below the image. There are a couple of changes, including putting a MessageBox into each of the Try....Catch blocks (you are not doing yourself any favours by throwing away exceptions). You didn't say what the Timer1 interval is so I just used added a Timer1.Stop so I would only get one pass of data.
Option Strict On
Option Explicit On
Imports System.Net.Http
Imports System.Runtime.InteropServices
Imports System.Text
Imports Newtonsoft.Json
Public Class Form1
<DllImport("user32.dll", CharSet:=CharSet.Auto)>
Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
End Function
Private Const WM_VSCROLL As Integer = 277
Private Const SB_PAGEBOTTOM As Integer = 7
Friend Shared Sub ScrollToBottom(ByVal richTextBox As RichTextBox)
SendMessage(richTextBox.Handle, WM_VSCROLL, CType(SB_PAGEBOTTOM, IntPtr), IntPtr.Zero)
richTextBox.SelectionStart = richTextBox.Text.Length
End Sub
Public Class Result
Public Property asks As String()()
Public Property bids As String()()
Public Property seqNum As Integer
Public Property prevSeqNum As Integer
Public Property lastTs As Double
End Class
Public Class Root
Public Property [error] As Object
Public Property result As Result
Public Property id As Integer
End Class
Private URI As String = "https://api.hotbit.io/api/v1/order.depth?market=KIBA/USDT&limit=150&interval=1e-8"
Private Async Function GetJsonFromApi() As Task(Of Root)
Using http As New HttpClient(),
response As HttpResponseMessage = Await http.GetAsync(URI)
Return JsonConvert.DeserializeObject(Of Root)(Await response.Content.ReadAsStringAsync())
End Using
End Function
Private Sub OutputAsks(asks As String()())
'asks
Try
Dim contents As New StringBuilder
For Each ask() As String In asks
contents.AppendLine(String.Join(" - ", {ask(0), ToKMB(GetDouble(ask(1)))}))
Next
RichTextBox1.BeginInvoke(Sub()
RichTextBox1.Text = contents.ToString()
RichTextBox1.Lines = RichTextBox1.Lines.
Where(Function(x) Not String.IsNullOrEmpty(x)).
OrderByDescending(Function(x) Decimal.Parse(x.Split(" "c)(0))).ToArray
ScrollToBottom(RichTextBox1)
End Sub)
Catch ex As Exception
MessageBox.Show("ERROR in asks")
End Try
End Sub
Private Sub OutputBids(bids As String()())
'bids
Try
Dim contents As New StringBuilder
For Each Bid() As String In bids
contents.AppendLine(String.Join(" - ", {Bid(0), ToKMB(GetDouble(Bid(1)))}))
Next
RichTextBox2.BeginInvoke(Sub()
RichTextBox2.Text = contents.ToString()
End Sub)
Catch ex As Exception
MessageBox.Show("ERROR in Bids")
End Try
End Sub
Public Shared Function ToKMB(num As Double) As String
If num > 999999999 OrElse num < -999999999 Then
Return num.ToString("0,,,.###B", Globalization.CultureInfo.InvariantCulture)
ElseIf num > 999999 OrElse num < -999999 Then
Return num.ToString("0,,.##M", Globalization.CultureInfo.InvariantCulture)
ElseIf num > 999 OrElse num < -999 Then
Return num.ToString("0,.#K", Globalization.CultureInfo.InvariantCulture)
Else
Return num.ToString("#.00", Globalization.CultureInfo.InvariantCulture)
End If
End Function
Function GetDouble(s As String) As Double
Dim v As Double = 0.0D
If Double.TryParse(s, v) Then Return v
Return 0.0D
End Function
Private Async Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim root As Root = Await GetJsonFromApi()
OutputAsks(root.result.asks)
OutputBids(root.result.bids)
' only one pass for this test
Timer1.Stop()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Timer1.Start()
End Sub
End Class