0

I need read in an infinite loop some variables and in the case it changes the boolean status it must do something. I tried to use a Do...Loop but the application crashes. Is there a way in visual basic 6 to use an infinite loop without stunk?

My code:

Do
   asd1 = readValue1
   asd2 = readValue2
   If asd1 <> asd1ex Then
       Text1.Text = "yes"
   End If
   If asd2 <> asd2ex Then
       Text1.Text = "no"
   End If
Loop While True
Kerberos
  • 4,036
  • 3
  • 36
  • 55
  • Your question is unclear. Can you add some pseudocode to show the structure of what you're trying to do? – Joe M May 17 '13 at 16:41
  • 2
    Where are the variables? What is likely to change them? A plain infinite loop itself won;t cause a crash, but may cause it to stop responding unless you process messages or have an exit check. – Deanna May 17 '13 at 16:46

2 Answers2

2

Make a timer and on that timer check the status, instead of the loop.

Solved after comment that explained where the data was coming from (async COM component's property):

working with vb6 IDE on a realtime client-server project. I have to read some variables and when one of these changes status it sends a socket message to server. With the sleep it stuck equally

What did not help: DoEvents and sleep

 DoEvents 
 Sleep 100

might help, will need to refer to the windows function sleep. But VB6 is single thread (well one for UI and one for logic) so you should have a way to get out of the loop. What are you really trying to do? Can you describe at a top level?

Are you working on the VB6 IDE or in VBA code in Office?

For sleep to work declare:-

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

See this too https://stackoverflow.com/a/4540553/1643558

If your showing a form to the user to read value 1 and 2 then you can use a modal form and have a button to click when they are done, and hide the form only when you like the values. No need to have a loop then. Can show an error MsgBox on a modal form too.

See http://www.tek-tips.com/viewthread.cfm?qid=1117372

Maybe remove the sleep and only keep the DoEvents.

You could also make a timer and on that timer check the status, instead of the loop

Community
  • 1
  • 1
tgkprog
  • 4,493
  • 4
  • 41
  • 70
  • I wish windows has continued support for windows only vb6 for a few more years, it was fun to code in and might fast to start small exes compared to java/. net executables. Perfect for tiny to medium utilities and apps – tgkprog May 17 '13 at 17:46
  • I'm working with vb6 IDE on a realtime client-server project. I have to read some variables and when one of these changes status it sends a socket message to server. With the sleep it stuck equally. – Kerberos May 17 '13 at 18:19
  • Are there any docs with the relay? are you testing in the debugger? FOr something like this i think its better to test everything else, add file based logs, and then build the exe and test. also why not use some other langauge, vb6 is great for windows utils not this kind of stuff. – tgkprog May 17 '13 at 19:47
  • 1
    maybe remove the sleep and only keep the DoEvents. You could also make a timer and on that timer check the status – tgkprog May 17 '13 at 19:48
  • Perfect!! Not knowing the events I've used the timer and it works perfectly! Thanks! – Kerberos May 17 '13 at 20:20
0

It looks like you're trying to set up a sort of event handler. In effect, your loop is "listening" for a change to the variable. You don't explain how the variables get changed, and this is important . If whatever is changing the variables can also raise an event, then you're home free--you can get rid of your loop and use the event handler to send the socket message. (This is probably why Deanna asked how the variables change.) This is the preferred way to do what you want, so you should find ways to raise an event if the variables change.

BobRodes
  • 5,990
  • 2
  • 24
  • 26
  • Thanks for the reply, the variables reads a relay status, this relay il controlled by a physical button. So I should use events? Sorry but i'm newby with vb6 – Kerberos May 17 '13 at 18:46
  • If the timer works perfectly, there's no reason to change it. :) – BobRodes May 30 '13 at 21:44