Using timers and DoEvents

moster67

Expert
Licensed User
Longtime User
Can someone explain how the timers actually work?

If I enable a timer in a sub it seems like the code continues to execute next instructions/statement. I would have thought when I wrote timer1.enable, it would exit the sub (and wait according to the interval I set it) and do the code I wrote in Timer1-tick sub and then return to the original sub and execute next statement.

Also, when should I use DoEvents and how does it actually work?

I need to implement a sleep-function (lasting a second or so). I know that the Threading-library includes a sleep-function but I read it should be avoided since Android could force-close my app.

I am bit confused. Anyone who wants to enlighten me a bit? :)

thanks.
 

agraham

Expert
Licensed User
Longtime User
Enabling a Timer sets a system timer that will post a message to the applications message loop when the timer interval expires. To receive the message and run the timer code your application must have exited all its Subs and be waiting for an event to occur. This means that a Timer Sub cannot be re-entered by another Timer event even if it overruns its interval.

The application message loop that receives messages and raises events only runs when all your Subs have returned. DoEvent looks at the message queue message loop and processes any waiting messages. Generally you should avoid using it as it could cause re-entrancy problems and possibly endless loops if you don't realize what it does. It might occasionally be useful to cause some GUI elements to be redrawn while you are still executing code.

Why would you need to pause the main thread? It will only serve to make the UI totally unresponsive. Sleep is really only useful for secondary threads so that they do not waste CPU cycles and power by running endless loops chacking if they need to do something.
 
Upvote 0

moster67

Expert
Licensed User
Longtime User
Many thanks for the information.

Well, I am using the http-library to send some commands to a decoder and then successively to obtain some information from its webserver. First I make it zap to a channel and then I request some information regarding the channel which it just zapped to.

The problem is that once I have sent my command to zap to a channel, my other request follows immediately and often the webserver is not yet ready to pass me on the information required (my second request is too fast) and it returns dirty-data. Therefore, after the first call (zap-command), I need to wait slightly before sending my second call. So I want to create this delay of a second or so by either using a timer, DoEvents or alternatively the sleep-method in your Threading-library and hence my question.

Hope that clarifies what I want to do.


Why would you need to pause the main thread? It will only serve to make the UI totally unresponsive. Sleep is really only useful for secondary threads so that they do not waste CPU cycles and power by running endless loops chacking if they need to do something.
 
Upvote 0

moster67

Expert
Licensed User
Longtime User
The first call simply tells the decoder to zap to a channel. Unfortunatley the webserver doesn't return some kind of information saying "channel has been changed successfully" or similar. To get information from the second call, it seems like the decoder need to be positioned on the channel for half a second before it can retrieve the data.

There is also another scenario; if the end-user has a motorized dish this will mean that the time needed from my command to swap channel until the dish has reached its new location, may be 30 seconds or more.

Well, I am sure I will figure out something. Thanks for your information and interest.
 
Upvote 0
Top