Dario24 Posted June 11, 2014 Report Share Posted June 11, 2014 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. Quote Link to comment
Tim_S Posted June 11, 2014 Report Share Posted June 11, 2014 You could put in a small delay in your loop (say, 100 msec) and check if 15 seconds has elapsed to run your SQL query. Quote Link to comment
crossrulz Posted June 11, 2014 Report Share Posted June 11, 2014 I was going to suggest using the Elasped Time express VI and only read the SQL data when "Time Elaspsed" (use a case structure). As Tim_S stated, use a small wait then. Quote Link to comment
Dario24 Posted June 11, 2014 Author Report Share Posted June 11, 2014 Nice.....Thank you fro your help. Quote Link to comment
drjdpowell Posted June 11, 2014 Report Share Posted June 11, 2014 Use an Event Structure with a 15,000 ms timeout, registered for the Value-change event of the Stop button. Quote Link to comment
GregSands Posted June 11, 2014 Report Share Posted June 11, 2014 Another (better?) approach that doesn't require many small waits is to get the Stop button to set a notifier: It will loop every 15s, but exit immediately on receiving a True value on the notifier. Quote Link to comment
hooovahh Posted June 12, 2014 Report Share Posted June 12, 2014 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. Quote Link to comment
drjdpowell Posted June 12, 2014 Report Share Posted June 12, 2014 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. Quote Link to comment
hooovahh Posted June 12, 2014 Report Share Posted June 12, 2014 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. Quote Link to comment
Jordan Kuehn Posted June 12, 2014 Report Share Posted June 12, 2014 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. Quote Link to comment
Darin Posted June 12, 2014 Report Share Posted June 12, 2014 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. Quote Link to comment
drjdpowell Posted June 12, 2014 Report Share Posted June 12, 2014 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). Quote Link to comment
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.