使用 mscomm 控件存在同样的问题。
处理异步事件总是很困难。
首先这是我个人的处理方式
可能以其他更好的方式存在。所以它是一种选择。
我不止一次遇到过这个问题,但我经常忘记如何
处理那个。所以这也是我的个人参考
首先是想法
create time of expiration when it became to be reached
the information should be complete
我们需要了解事件是如何发生的
为死区时间的图形视图,死区时间只是一个时间段
现在在 vb6 中,当异步事件发生时,也需要在不同的瞬间发生两个动作,但在同一时刻进行测试
Action #1 that action update the current time
Actions #2 that is limited to update the time current time for async event
Both event are combined on one infinity loop method
the purpose is check current time against the current time of async event
我们还需要一些实用程序来跟踪我使用 win api GetTickCount 的时间
在 mscomm 中,我们还需要添加标志和字符串,以了解是否收到了一些它,因为在调用端口中的接口时没有等待太多
该代码还可以抛出事件以供重用和隔离
现在看看 vb6 类的骨架
class Module SocketReceptorVb6
Private Declare Function GetTickCount Lib "kernel32" () As Long ' API tick
withevents _tcpControll as Winsock
private m_currentState As Eventstate 'is a enumerate Waiting, Receiving
private m_currentTimeAsynEvent As Long ' current time for async event
private m_buffer As String
private m_timeOut as Long
Public Event StreamReceived(ByVal stream As String) 'Event completed
Property TimeOut
private sub Class_Initialize()
m_currentState = Waiting
end sub
sub tcpControl_DataArrival(bytesTotal)
sub Receiving
sub InitFlags
End Class
现在仔细看看这 3 种方法
InitFlags Method
清理变量并准备环境,查看内容
m_currentState = Waiting //state of the process
m_buffer = vbNullString //reset the buffer used for the information
tcpControl_DataArrival Method
那个方法有下一个职责
-update buffer
-initialize infinity loop
-update m_currentTimeAsynEvent
方法的内容
Dim sBuff As String
wskConnect.GetData sBuff, vbString
m_buffer = m_buffer & sBuff
' update time for Asyn Event
m_currentTimeAsynEvent = GetTickCount()
if m_currentState = Waiting then
m_currentState = Receiving
InfinityLoop()
end if
InfinityLoop Method
verify that the expiration time has been reached
Content method
Dim current_time As Long ' the time point o current time
Do
current_time = GetTickCount()
' timeout reach, exit of loop
If current_time > m_currentTimeAsynEvent + m_timeOut Then
Exit Do
End If
DoEvents
Loop
RaiseEvent StreamReceived(m_buffer)
InitFlags 'Reset flags and variables
注意:
该代码预期会导致单个流而不是多个套接字响应