winsock控件用于网络发送指令到客户端
希望发送两条指令,中间间隔1秒
WinSock1.SendData Cmd1
'间隔1秒
WinSock1.SendData cmd2如果使用sleep后
第一次senddata时,客户端收不到任何东西,等到发送第二条指令时,客户端一下子收到了2条指令不知道怎么解决这个问题
这到底是怎么回事?
高手能告诉我解决方案么?

解决方案 »

  1.   

    '加一个Timer 控件,设定Timer1.Interval = 1000
    '代码如下:
    Dim cmd(1) As String
    Private Sub Timer1_Timer()
        Static i As Integer
        sendD i
        i = i + 1
        If i > 2 Then Timer1.Enabled = False: i = 0
    End SubPrivate Sub sendD(i As Integer)
        WinSock1.SendData cmd(i)
    End SubPrivate Sub Command1_Click()
        Timer1.Interval = 1000
        Timer1.Enabled = True
    End Sub
      

  2.   

    WinSock1.SendData Cmd1
    '间隔1秒
    '自己写一个函数,WAIT
    call wait1000WinSock1.SendData cmd2
    Option Explicit
    Private Declare Function GetTickCount Lib "kernel32" () As Long
    Public Sub wait1000()
        Dim StartNum As Long
        Dim EndNum As Long
        StartNum = GetTickCount
        DoEvents
    LL:          EndNum = GetTickCount
        DoEvents
        If EndNum - StartNum >= 1000 Then
            Exit Sub
        Else
            GoTo LL
        End IfEnd Sub
      

  3.   

    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    Private Declare Function GetTickCount Lib "kernel32" () As LongPrivate Sub Command1_Click()
        
        Winsock1.SendData cmd1
        DoEvents
        Wait 1000
        Winsock1.SendData cmd2
        DoEvents
        
    End SubPublic Sub Wait(ByVal dwMilliseconds As Long)
        
        Dim t   As Long
        
        t = GetTickCount()
        
        Do While (GetTickCount() < t + dwMilliseconds)
            Sleep 1
        Loop
        
    End Sub