Jump to content

Stop Control inside While Loop


Recommended Posts

HI,

 

I have a while loop that is reading sql data every 15 seconds, so inside the loop is the code to read the data and a 15 seconds delay.

 

Is there anyway that I can stop the while loop cycle anytime I want, without waiting that the delay finish the 15 seconds time? (If I press the stop button at the second #1 of the delay, I need to wait other 14 seconds until the program stops).

 

Thank you.

Link to comment
Use an Event Structure with a 15,000 ms timeout, registered for the Value-change event of the Stop button.

You probably know this already, but the 15,000ms timer would get reset every time any event is handled.  So if I had a Mouse Move event on the panel and continually moved the mouse, the timeout case would never get handled.  

 

Because of this I would often have a small timeout like 100ms, with an elapsed timer inside to see if 15s have passed.

 

That being said I think Gregs answer is the better then this.

Link to comment

Why would he be handling mouse-move events in his data collecting loop?  Handle that somewhere else.

 

Any finite timeout is unreliable if there can be repeated events; your 100ms timeout would fail during a mouse move.  The only reliable timeouts are 0, −1, and a timeout recalculated after each event based on the previous time the timeout case executed.

 

Added later: actually, 0 isn’t that reliable either.

Link to comment
Why would he be handling mouse-move events in his data collecting loop?  Handle that somewhere else.

 

Any finite timeout is unreliable if there can be repeated events; your 100ms timeout would fail during a mouse move.  The only reliable timeouts are 0, −1, and a timeout recalculated after each event based on the previous time the timeout case executed.

 

Added later: actually, 0 isn’t that reliable either.

My example isn't quite right.  I just meant if you were handling other events, in addition to setting a timeout to be very large, that the result may not be what you expected.  

Link to comment
My example isn't quite right.  I just meant if you were handling other events, in addition to setting a timeout to be very large, that the result may not be what you expected.  

 

That makes much more sense.  At first it sounded like you were suggesting that the event structure would respond to all events, even without subscribing to them.

Link to comment
That makes much more sense.  At first it sounded like you were suggesting that the event structure would respond to all events, even without subscribing to them.

 

Until recent LV versions there was a very interesting behavior.  If you wired a dynamic event refnum with multiple events to an event structure but only handled a subset of them, even the unhandled events firing would reset the timeout counter.  Yuk.

 

In a different discussion I was one of the few (close to only one) who advocated using multiple event structures on a BD.  This is an example of one of those cases, where I like the decreased coupling.  As mentioned earlier by drjdpowell, there shall be no mouse handling events in the data handling event structure.  I have a cohesive set of events, and a timeout pertinent to those events if desired.  Easier to read (for me), to write, and to reuse.

Link to comment

Whatever timing method you choose, you will get the maximum flexibility by making it a separate loop from your actual periodic task.  Have the separate timing loop signal the main loop handling the task via some messaging method such as a User Event.  That way the main loop is not constrained by the timing technique; the OP’s process could be reading data every 15 sec AND saving to disk every 5 min AND updating some UI element every 500 ms (by using three external timing loops). And as a message-handler, the main loop can accept (and immediately act on) other messages than just “stop”.  Meanwhile, the dedicated timing loop can be made a reusable component (as it only does timing).

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.