LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to pass multiple values to a callback?

I am learning how to watch a folder for new files. I need to test new files written for certain attributes.

 

I have the attached example from NI that watches a folder for new files. The example that NI provides retrieves the filename with the base path. I need the entire base path and filename so I can load it in my callback and process it.

 

The bottom line is I do not understand how the watcher and callback are working together to get the filename. 

 

It looks to me like the watcher "sees" that a new file is saved and calls the callback and passes parameters. From there I am completely confused as to how the two vi's get what data and who passes what to who.

 

How would I pass a cluster of data to the callback to be used by the callback?

0 Kudos
Message 1 of 7
(1,518 Views)

In this bit of code here:

Kyle97330_0-1628270624095.png

The thing there that says "New file name" on it is a reference to the control named that on the front panel of the VI.  Then in the callback VI, there is this code:

Kyle97330_1-1628270759433.png

That takes the name of the changed file from the event data, then uses the reference to that control to update the value shown on the front panel of the original calling VI.  If you have ever done C/C++ programming, it's as if it passed a string pointer.

 

This whole thing is done using .NET events, which are sort of "outside" of LabVIEW, so you can't just get the value directly back, you have to use some sort of "messaging" to get it back, because the .NET event could occur zero times, one time, or a thousand times, so you can't just have a simple output somewhere.

 

Do you know how to use queues or user events in LabVIEW?  If not, I suggest you look them up and what they do, and then edit the callback VI to send the file name as a user event or a queue element, and then have another part of your code get those events or queues and use those to start the file checks you need to do.

 

Another thing you could do that might be easier to get started with (but NOT better in the long run) would be to change the "New file name" indicator into a control instead, switch the "Value" property node in the callback VI with a "Value (Signaling)" node, and then in the While loop of the main VI that is now empty, put an event structure in there and create a frame that looks for a "Value change" event on the "New file name" string control, and then use that as the trigger for your code.

 

Note:  Because you're using a callback VI with .NET code, you might notice that the callback VI doesn't leave the running state when your code is done.  If so, look at this thread:

https://forums.ni.com/t5/LabVIEW/net-event-callback-VIs-Why-do-they-never-leave-running-state/td-p/3...

There are notes and instructions in there on how to set up a GC.Collect node to fix the problem.

0 Kudos
Message 2 of 7
(1,489 Views)

Kyle97330 - I was familiar with queues. I did not realize that they were "system wide" and that you can obtain a queue by name in multiple VI's and it's still the same queue. Things you learn. That certainly works.

0 Kudos
Message 3 of 7
(1,467 Views)

By the way, if you want to use a queue by reference, you can.  You can unwire the reference to the string control that's passed in, then wire in a queue reference instead.  This will mean the callback VI won't work any more, but if you unwire the callback VI, then right-click the "Register" node, there will be an option in the menu to create a callback VI, and when created it will have the queue reference that you passed into it as an input.  You'd use the same unbundle and such to get the name of the changed file from .NET, but then you could send the file over the queue reference without needing to create the reference using the same named queue if you prefer to pass it by reference.

0 Kudos
Message 4 of 7
(1,459 Views)

I am using an example that uses .NET and reports the name of files that were added/created to a directory.  I want to copy those files to a different directory, but I don't know how to do that.

I attached the watch directory vi and the callback event.

 

Thanks

Avi.

0 Kudos
Message 5 of 7
(1,489 Views)

If you already have the source file paths and their destinations, there's a File Copy function under Advance File functions palette.

 

Sorry couldn't show a snippet, posting from mobile.

Santhosh
Soliton Technologies

New to the forum? Please read community guidelines and how to ask smart questions

Only two ways to appreciate someone who spent their free time to reply/answer your question - give them Kudos or mark their reply as the answer/solution.

Finding it hard to source NI hardware? Try NI Trading Post
0 Kudos
Message 6 of 7
(1,479 Views)

Thanks, but since it is retrieved by a .NET callback I don't actually have the filename. If I understand how it works, it is kind of outside LV. 

0 Kudos
Message 7 of 7
(1,432 Views)