1

I have a requirement in visual basic6 where I want to send commands to a machine connected to my PC. I can send the command only after getting the acknowledgement from it.It is like this. Sleep after I send the first command will make the program inactive and I cannot receive the acknowledgment.

So I am thinking of using variables or flags and increment in the subsequent modules for the delay. But I am not sure how to implement the thing. I have included the sample rather incomplete code for this. But I dont know how it can give the delay. I am thinking of timers,goto. Is there anyother way to implement the delay here.

Module where I send the command

//Sending Module

Sendcommand()

Send command CMD1.
If (flagcheck =2)
Send command CMD2.

Module where I receive the acknowledgment command

Receive command()

Select cmdname
public flagcheck=0
CASE ACK1.1
        flagcheck=flagcheck+1
CASE ACK1.2
        flagcheck=flagcheck+1
ShivShambo
  • 523
  • 12
  • 29

1 Answers1

2

You can generally do something like

SendCommandOne

While flag = 0 ' ReceiveCommand would set flag to 1
    DoEvents
End While

SendCommandTwo

etc

James
  • 9,064
  • 3
  • 31
  • 49
  • +1, IMO this is the best way to implement if the delay is to wait for another process to finish. – Daniel Oct 10 '12 at 00:54
  • http://stackoverflow.com/questions/4526659/what-does-doevents-do-in-vb6 someone is not recommending using doevents.. – ShivShambo Oct 10 '12 at 01:04
  • @shivoham Good point, but this could be worked around by using static boolean variable. Which I felt obliged to add as an answer for that question. – Daniel Oct 10 '12 at 01:16
  • 2
    +1 but put a tiny sleep in the loop as well as the DoEvents. Otherwise processor usage goes to 100% – MarkJ Oct 10 '12 at 06:58
  • 1
    DoEvents() calls actually do a long list of things, the last one being a Sleep(0) call. It "burns rubber" because of the many operations it must perform. Considering the re-entrancy hazard and the risk of corrupting data or blowing the stack it should be a *last resort*, not the first one. Using a Timer is always preferable when possible. – Bob77 Oct 10 '12 at 18:00