LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Leave reference open? Or open - write/read - close?

One thing I do when using file references is use them in a functional global where I open the file reference then leave it. Then I'll go back and either read or write to it. Then I go back later and close it.

 

An associate prefers to open, read/write then close it in the same operation.

 

Question: which way is "better". I like his approach, where I don't have to worry about anything. At the same time I wonder about performance issues opening and closing a file more times than necessary.

 

Are there any "wrong" answers here?

PaulG.

LabVIEW versions 5.0 - 2023

“All programmers are optimists”
― Frederick P. Brooks Jr.
0 Kudos
Message 1 of 7
(5,526 Views)

Hi Paul,

 

both options have their pros and cons…

 

- keeping the file ref open you could block the file from access by other software, closing the file ref allows other software to access the file in parallel (theoretically)

- keeping the file ref open eases appending of new data to your file as LabVIEW handles the file read/write position on its own, opening/closing the file ref each time forces you to set the file read/write position each time…

 

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 2 of 7
(5,519 Views)

Personally, I use a Queued Message Handler to do the file IO.  This works particularly well for a log file where multiple places could be trying to write to the file (use TDMS in this case).  Then when you command the QMH to shut down, it knows to close the file first.

 

A couple more notes (cons):

Open/Write/Close: If you open the file in Excel, you will get an error when you try to open the file in LabVIEW because Excel will lock out the file.  Other applications do this as well, but that is the one that bit me the hardest.

Open/Write/Close: This will cause some slow down.

Leave open: You do have to remember to close it when you are done.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 3 of 7
(5,491 Views)

I would say that it depends on how often you need to write to the file, which is the same approach that I use for writing to databases.  If it's one write per minute, I personally don't try to maintain any open references or connections because leaving them open doesn't mean that they stay valid.  It still requires a little effort to check for a valid reference before writing and/or maybe a retry mechanism in case the write fails due to lost reference or connection.  If you are writing to a network file or database, lost references can be more problematic than local writes. 

 

As Gerd says, each option has pluses and minuses that have to be weighed on a case by case basis. 

aputman
0 Kudos
Message 4 of 7
(5,485 Views)

You should open a file reference ONCE before you start writing to it and close it ONCE when you are completely done writing to it.

 

Besides lowering the operating overhead of constantly opening and closing the file everytime you write to it.

 

Doing this locks the file, so no other program can open or access it while the reference is open.

 

If another program tries to open it, like say with Excel. Windows will popup and say the file is locked and offer to open a copy or open it read only and your labVIEW program can still write to it while you have it  (read only or copy) open in Excel

 

If you had closed the file reference and done the same thing Excel would open it just fine, but if your LabVIEW program tried to open it while you had it open in Excel your LabVIEW program will crash with a sharing violation.

 

 

========================
=== Engineer Ambiguously ===
========================
0 Kudos
Message 5 of 7
(5,470 Views)

@aputman wrote:

I would say that it depends on how often you need to write to the file, which is the same approach that I use for writing to databases.  If it's one write per minute, I personally don't try to maintain any open references or connections because leaving them open doesn't mean that they stay valid.  It still requires a little effort to check for a valid reference before writing and/or maybe a retry mechanism in case the write fails due to lost reference or connection.  If you are writing to a network file or database, lost references can be more problematic than local writes. 

 

As Gerd says, each option has pluses and minuses that have to be weighed on a case by case basis. 


That's my approach also, if it's used every second, or thereabouts, I keep it open. It it's  2 min tests I close after write.

/Y

G# - Award winning reference based OOP for LV, for free! - Qestit VIPM GitHub

Qestit Systems
Certified-LabVIEW-Developer
Message 6 of 7
(5,447 Views)

@PaulG. wrote:

One thing I do when using file references is use them in a functional global where I open the file reference then leave it. Then I'll go back and either read or write to it. Then I go back later and close it.

 

An associate prefers to open, read/write then close it in the same operation.

 

Question: which way is "better". I like his approach, where I don't have to worry about anything. At the same time I wonder about performance issues opening and closing a file more times than necessary.

 

Are there any "wrong" answers here?


Performance-wise, it is better to open once and close once. When opening a file you are asking the OS to map the file contents into the address space of your application. That requires fiddling with the address translation tables allocating memory etc.

 

While I never tested it with file references, a VISA reference will leak memory because LV only releases the resources of references until the code stops. It may not be obvious but run a VI that repeatedly opens and closes a VISA reference over a week-end you will see increased memory used by LV. Note the space associated with each Open is small, but if you do it enough times...

 

Now if you are concerned with making sure the file is written to guard against a power fail, then closing the file is one notch better than flushing after each change but each are asking the OS to do NOW what it will eventually get around to doing in its own schedule.

 

Now if you look at the config file operations supplied with LV, the files will only ever get written in you close them. There is no option to "write" with out closing.

 

Choose your pill, blue or red.

 

Ben

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
Message 7 of 7
(5,436 Views)