LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Exiting early from a slow-paced loop

Question about loops and timings. I have a routine, one component of which is a data-logger. In its simplest form, the functionality is illustrated by the upper loop in the attached image; there's a While loop which "does stuff" on a cycle time set by a front-panel control, and it runs until a stop signal is recieved. Now, in my application, the logging cycle time is typically a few minutes, so if the user hits the stop button, it will be multiple minutes before the loop completes and the VI exits. Needless to say, not good. 

 

My current approach for solving the problem is the lower loop. The loop runs on a faster cycle time, and only triggers the "Do Stuff" case every n'th time through the loop. That way, the time to respond to a stop signal is of order a second, which is acceptable.

 

This works, but I'm wondering if there's a better way to implement the same behavior. I've thought about an Event structure with the Do Stuff code inside the Timeout case, but the issue there is I already have an Event structure in a second loop which runs in parallel with the logging loop (and it already has a different chunk of code in its Timeout case), and having two such structures in the same VI seems like a bad idea.

 

Any suggestions or thoughts appreciated. 

0 Kudos
Message 1 of 4
(2,862 Views)

You can have two event structures, each in it's own loop. No problem with that. Of course whenever an event occurs, the timeout is reset, so you need to add some extra code to keep the special code at somewhat regular intervals. Else, if events occur too often, the timeout case might never be reached.

 

(About your code, try to use the correct representation, that orange just hurts my eyes. ;))

0 Kudos
Message 2 of 4
(2,852 Views)

Thanks, I'll give that a try. I'm not too concerned about exact intervals; basically, the logging needs to be "every few minutes, more or less". The events handled by the main Event structure are all value-change events linked to front-panel buttons. Unless one of my users goes nuts (always a possibility), those get triggered on an hourly or daily timescale for this particular application.

 

And yes, in my actual code the representations are correct. I just made that toy example in about 30 seconds...

0 Kudos
Message 3 of 4
(2,843 Views)

dmsilev wrote:

And yes, in my actual code the representations are correct. I just made that toy example in about 30 seconds...


Well, if you need to deal with fractional seconds, you need DBL. Of course doing the quotient&remainder on DBL might lead to unexpected results due to the limitations of floating point math. It might be better to enter the wait in integer milliseconds instead.

0 Kudos
Message 4 of 4
(2,825 Views)