我是VB.net线程的新手,我创建了一个应用程序,它创建一个线程来检查来自串口的人名和年龄是否存储在Access-DB中的coreclty。
这是代码的一部分
Public Class MainForm
...
Public accessDBThr As Thread
.. .
Public Class CParameters
Public p1 As String
Public p2 As String
End Class
...
Private para As CParameters
...
searchThrd是在端口dataReceived event handeler
中创建的searchThrd= New Thread(AddressOf isFoundinAccessDB)
para.p1 = name
para.p2 = age
searchThrd.Start(para)
此searchThrd执行isFoundinAccessDB以搜索Access DB中的数据。
Public Sub isFoundinAccessDB(ByVal param As Object)
' should wait 5 seconds for the data to be saved in the Access DB, then
' start search for the data in the DB
Thread.Sleep(1000 * 5)
para = CType(param, CParameters)
Dim name As String = para.p1
Dim age As String = para.p2
Try
If database.DataSearch(name, age) Then
msgbox("Found")
Else
msgbox("Not found")
End If
Catch ex As Exception
mesgbox(ex.message)
End Try
End Sub
问题是,当在5秒内从串口接收到多个数据时,方法 isFoundinAccessDB() (因为thread.sleep(1000 * 5))总是在搜索最后一个数据并忽略第一个收到的数据
例如,如果收到的端口(" x1,20"," x2,22"," x3,23"," x4,65& #34;)作为数据,mehtod isFoundinAccessDB()总是搜索最后的数据(" x4,65")
如何解决此问题并搜索最新值?