08-27-2020 12:40 PM
I am attempting to make a status logging VI that retains a file refnum as a Functional Global (FG). The reason I want to make this an FG is for convenience of using it anywhere in my multi-thread program and not needing to wire the refnum through. For proof of concept, I am simply running my LogStatus2.VI by itself, first with an Open operation, and then again with a Write. The problem I am having is that I receive an error (Code 1) any time after the first execution. It feels as though the refnum is not storing as an FG in the way that I am thinking it should. Am I missing something fundamental or can a file reference not be used as a FGV?
Note: I intend to add some verification later for empty path, etc.
Solved! Go to Solution.
08-27-2020 01:06 PM
If you are running this VI by itself, you will not get the results you want. When the VI stops running (and nothing else is running) it is likely the references are being closed. You need to create another VI that calls your Action Engine several times to test what you are trying to test.
08-27-2020 01:17 PM - edited 08-27-2020 01:17 PM
Edit: Dangit, crossrulz beat me to it!
If you run the subVI from within another VI, it works fine. The issue is that LabVIEW will automatically close opened file references when the VI is closed. Your refnum is being stored correctly, but the file is being closed.
There are two ways to fix this. First is to use it as-is; when you call the VI from within another VI, it works just fine:
(I am guessing you're calling it multiple times manually, which doesn't work because the reference is closed automagically)
The second way, and the way I'd recommend, is to not store the refnum but to store the path. Each call would then open/write/close the path. This seems to be a status logger, so it's not going to be called hundreds of times a second, so the overhead with reopening the file is minimal. The reason to do this is so that the file gets reliably closed. Note in my example that I did *not* close the file, but that it was fine; that's because LabVIEW saw that I wasn't using the reference anymore and auto-closed it. In the dev environment this works, but in a normal application what happens if the user force-closes the application? Or what if it crashes? Or what if you have some edge case where the file doesn't quite get closed properly?
In general it's a better practice to not leave references open if you can help it. It's certainly no hard and fast rule, but in the case of a logger IMHO it would be better to save the *path*, not the *refnum*, to the FGV.