LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How do I create a timed loop programmatically?

I have a design pattern where a timed loop does some processing.  I need to instantiate this pattern several times -- in fact, the number of times is determined at run time via a config file.  But how can I create a timed loop programmatically and have them run in parallel?  In any other language/OS, I can simply create a thread on-the-fly but I don't see a way to do that in a VI.

 

0 Kudos
Message 1 of 9
(3,955 Views)

You can take any VI and give it the extension VIT. This makes it a VI template. Opening this template programmaticly using VI server and you get a separate instance of the code that runs independently, each time you open it.

 

Mike...


Certified Professional Instructor
Certified LabVIEW Architect
LabVIEW Champion

"... after all, He's not a tame lion..."

For help with grief and grieving.
Message 2 of 9
(3,944 Views)
I'm not sure I understand.  Does a template VI not block when it's called?  So if I had a for loop and called my VIT that has the timed loop in it, does each iteration spawn a thread of execution with its own memory and return immediately?
0 Kudos
Message 3 of 9
(3,934 Views)

The vit is just a sub-vi. It works according to your code. The "vit" just forces the opening of multiple sub vis with the same block diagram.

 

Another option is to make your sub vi reentrant and call it, as shown.

see this

0 Kudos
Message 4 of 9
(3,920 Views)

Yes, I VIT does not block because the VIT file extension causes LV to load a completely new instance of it into memory when it is opened. This is how you can spawn a new process in LV.

 

Mike...


Certified Professional Instructor
Certified LabVIEW Architect
LabVIEW Champion

"... after all, He's not a tame lion..."

For help with grief and grieving.
0 Kudos
Message 5 of 9
(3,909 Views)

So ... a VIT is just a cleaner way of calling a reentrant sub-VI without waiting for it to return?

 

Before I dive into this, does anybody know if either of these approaches will work on a RT PXI target?

 

Thanks.

 

0 Kudos
Message 6 of 9
(3,887 Views)

Don't know which would be better from the standpoint of RT, but they both should work. One other difference between a VIT and a reentrant VI is that you can debug a clone coming from a VIT, where as you can't debug an individual instance of a reentrant VI.

 

Mike...


Certified Professional Instructor
Certified LabVIEW Architect
LabVIEW Champion

"... after all, He's not a tame lion..."

For help with grief and grieving.
0 Kudos
Message 7 of 9
(3,882 Views)

 

I have a design pattern where a timed loop does some processing.  I need to instantiate this pattern several times -- in fact, the number of times is determined at run time via a config file.  But how can I create a timed loop programmatically and have them run in parallel?  In any other language/OS, I can simply create a thread on-the-fly but I don't see a way to do that in a VI.


 

I'd put the Timed loop in a VIT as suggested above and run the VIT's in parallel by spwaning off background threads using VI server.

 

If you can put an resonable upper limit on the number parralel loops you could use a case structure with the appropriate number of the re-entrant VIs in each case and then LV would do all of the work and create the threads on-the-fly in your behalf. 

 

Couple of notes:

Last time I looked RT-OSs are not known for returning allocated resources. So I would tred carefully into a design that is repeatedly creating threads. Detemining the proper number at boot and staying with that is OK. Just don't do it in a loop or you could end up running out of memory.

 

This Nugget includes code that creates a bunch of processes from a template.

 


Mike wrote

 

 One other difference between a VIT and a reentrant VI is that you can debug a clone coming from a VIT, where as you can't debug an individual instance of a reentrant VI.


 

Sorry Mike but I have to split hairs and say that is partially right and patially wrong.

 

You CAN debug an individual instance of a re-entrant VI ....

 by identifying which instance you want to debug. This can be done by single stepping the caller and clicking "step-into" which will open that instance.

 

You CAN NOT debug an individual instance of a re-entrant VI ....

and click the "Stop" button while debugging the re-entrant sub-VI and expect to retain un-saved changes, since LV will crash (last time I checked). You should back out of all sub-VI calls first.

 

Ben

 

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
0 Kudos
Message 8 of 9
(3,862 Views)
You can also launch multiple threads by creating a reentrant VI and using Open VI Reference to open it (get a new instance each time) and Run VI method to launch the VI.  I use this for creating identical action engine waveform buffers, but it should work well in your application.
0 Kudos
Message 9 of 9
(3,821 Views)