I am trying to understand multithreading with VB.net.
Here is what I am doing it all works for now but This is my first try at it.
Can anyone see any problems with this. the timer ticks every 10 seconds.
It will be updating the database next.
Form Main
Imports System.Data.OleDb
Imports System.Threading
Public Class frmMain
Dim sTrucks() As String
Dim lngCnt As Long
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim strTrucks As String
strTrucks = "59|1;41|2;42|3;45|4;47|5"
sTrucks = Split(strTrucks, ";")
End Sub
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim intcnt As Int32
Dim t As Thread
Dim ThisTruck() As String
lngCnt += 1
Label1.Text = lngCnt
For intcnt = LBound(sTrucks) To UBound(sTrucks)
Dim oUpdateTruck As New UpdateTruck
'Set the Truck Number and ID
ThisTruck = Split(sTrucks(intcnt), "|")
Label2.Text = sTrucks(intcnt)
Application.DoEvents()
oUpdateTruck.strTruckNum = CStr(ThisTruck(0))
oUpdateTruck.strTrkID = CStr(ThisTruck(1))
'Set up a new Thread
t = New Thread(AddressOf oUpdateTruck.UpdateTrucksPosition)
'Start the Thread
t.Start()
Next 'Go to the next truck and start a new thread
End Sub
End Class
Class UpdateTruck
Imports System.Net.Sockets
Imports System.Data.OleDb
Public Class UpdateTruck
Public strTruckNum As String
Public strTrkID As String
Public Sub UpdateTrucksPosition()
Dim strTrucksConnectionString As String
Dim dblGps_Return(0 To 3) As Double
Dim UpdateSQL As String
strTrucksConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\ceccfile\updates\Files\VehicleTracker\Data\VehLocation.mdb"
Dim UpdateTruck As New OleDb.OleDbConnection(strTrucksConnectionString)
dblGps_Return = GetTruckGPSInfoByTruckNumber(strTruckNum)
If dblGps_Return(0) > -1 Then
UpdateSQL = "Update VehLocation Set"
UpdateSQL += " Latitude=" & CStr(dblGps_Return(0))
UpdateSQL += ", Longitude=" & CStr(dblGps_Return(1))
UpdateSQL += ", Speed=" & CStr(dblGps_Return(2))
UpdateSQL += ", Direction=" & CStr(dblGps_Return(3))
UpdateSQL += ", DTStamp=#" & CStr(Date.FromOADate(dblGps_Return(4))) & "#"
UpdateSQL += " Where TrkID=" & strTrkID
Debug.Print("Status" & Format(Now, "mm/dd/yy hh:mm:ss") & ": " & UpdateSQL)
Dim UpdateTruckComm As New OleDb.OleDbCommand(UpdateSQL, UpdateTruck)
UpdateTruckComm.Connection = UpdateTruck
UpdateTruckComm.Connection.Open()
UpdateTruckComm.ExecuteNonQuery()
UpdateTruck.Close()
End If
End Sub
Private Shared Function GetTruckGPSInfoByTruckNumber(ByVal sTruck As String)
On Error GoTo Err_Control
Dim dGPS_Info(0 To 4) As Double
dGPS_Info(0) = -1
dGPS_Info(1) = -1
dGPS_Info(2) = -1
dGPS_Info(3) = -1
dGPS_Info(4) = -1
Dim strResponse() As String
Dim strParsedResponse() As String
Dim intcnt As Integer
Dim port As Int32 = 9494
Dim client As New TcpClient("cecc-trk" & sTruck & ".eairlink.com", port) '"cecc-trk" & sTruck & ".eairlink.com"
Dim dLat As Double
Dim dLong As Double
Dim dSpeed As Double
Dim dDirection As Double
Dim TruckDT As DateTime
' Get a client stream for reading and writing.
' Stream stream = client.GetStream();
Dim stream As NetworkStream = client.GetStream()
Dim MyData As [Byte]()
' Receive the TcpServer.response.
' Buffer to store the response bytes.
MyData = New [Byte](256) {}
' String to store the response ASCII representation.
Dim responseData As [String] = [String].Empty
' Read the first batch of the TcpServer response bytes.
Dim bytes As Int32 = stream.Read(MyData, 0, MyData.Length)
responseData = System.Text.Encoding.ASCII.GetString(MyData, 0, bytes)
strResponse = Split(responseData, vbCrLf)
For intcnt = LBound(strResponse) To UBound(strResponse)
If InStr(strResponse(intcnt), "$GPRMC") > 0 Then
'1 220516 Time Stamp
'2 A(validity - A - ok, V - invalid)
'3 5133.82 current Latitude
'4 N(North / South)
'5 00042.24 current Longitude
'6 W(East / West)
'7 173.8 Speed in knots
'8 231.8 True course
'9 130694 Date Stamp
'10 004.2 Variation
'11 W(East / West)
'12 *70 checksum
strParsedResponse = ParseGPSString(strResponse(intcnt))
If strParsedResponse(2) = "A" Then
TruckDT = ParseDateTimeFromGPS(strParsedResponse(9), strParsedResponse(1))
dLat = CStr(CDbl(Strings.Left(strParsedResponse(3), 2)) + (CDbl(Strings.Right(strParsedResponse(3), Len(strParsedResponse(3)) - 2)) / 60))
If strParsedResponse(4) = "S" Then
dLat = -1 * dLat
End If
dLong = CDbl(Strings.Left(strParsedResponse(5), 3)) + (CDbl(Strings.Right(strParsedResponse(5), Len(strParsedResponse(5)) - 3)) / 60)
If strParsedResponse(6) = "W" Then
dLong = -1 * dLong
End If
If strParsedResponse(7) = "" Then strParsedResponse(7) = "0"
If strParsedResponse(8) = "" Then strParsedResponse(8) = "0"
dSpeed = CDbl(strParsedResponse(7)) * 1.150779
dDirection = strParsedResponse(8)
dLat = Math.Round(dLat, 6)
dLong = Math.Round(dLong, 6)
dSpeed = Math.Round(dSpeed, 1)
dDirection = Math.Round(dDirection, 1)
dGPS_Info(0) = dLat
dGPS_Info(1) = dLong
dGPS_Info(2) = dSpeed
dGPS_Info(3) = dDirection
dGPS_Info(4) = TruckDT.ToOADate()
End If
Exit For
End If
Next
' Close everything.
stream.Close()
client.Close()
Exit_Here:
Return dGPS_Info
Exit Function
Err_Control:
Select Case Err.Number
Case Else
'Form1.Label1.Text = "Status" & Format(Now, "mm/dd/yy hh:mm:ss") & ": " & Err.Number & ": " & Err.Description
Resume Exit_Here
End Select
End Function
Private Shared Function ParseGPSString(ByVal sToParse As String)
Dim strReturn() As String
strReturn = Split(sToParse, ",")
Return strReturn
End Function
Private Shared Function ParseDateTimeFromGPS(ByVal GPSDate As String, ByVal GPSTime As String) As DateTime
Dim sTempDate As String
Dim sTempTime As String
Dim TimeOffset As Integer
Dim iHours As Integer
Dim dtReturn As DateTime
TimeOffset = DateDiff(DateInterval.Hour, Now, Date.UtcNow)
iHours = CInt(Strings.Left(GPSTime, 2))
iHours = iHours - TimeOffset
sTempDate = Strings.Mid(GPSDate, 3, 2) & "/" & Strings.Left(GPSDate, 2) & "/" & Strings.Right(GPSDate, 2)
GPSTime = CStr(CLng(GPSTime))
sTempTime = CStr(iHours) & ":" & Strings.Mid(GPSTime, 3, 2) & ":" & Strings.Right(GPSTime, 2)
dtReturn = CDate(sTempDate & " " & sTempTime)
Return dtReturn
End Function
End Class