How Can I Check if a File is Already Open?
Primary Software: LabVIEW Development Systems>>LabVIEW Professional Development System
Primary Software Version: 1.0
Primary Software Fixed Version: N/A
Secondary Software: N/A
When I use a file that is shared over a network, I sometimes need to know if another program already has the file open. Knowing this will help me avoid race conditions when I have multiple LabVIEW applications writing to the same file. How can I determine if the file is already in use?
In LabVIEW the Open/Create/Replace VI doesn’t generate any error or warning if you try to open a file that is already open. If you have many applications that want to write to the same file, you have to check that they don’t access the file simultaneously in order to avoid race conditions.
If these applications are on the same PC you can use semaphores to regulate the access to the shared file. See the Community Example link below for an example.
Note: Semaphores will not work if your applications run on different PCs on a network (see related links below for more information). In this case you can bypass this limitation with the following workaround, as shown in the attached example:
KnowledgeBase DIRECT-3S8RDV: What Is a Semaphore?
KnowledgeBase 3BRIBK60: Using CVI to Programmatically Check File or Directory Existence
NI Community Example: Protecting Config File Access With Semaphores
Protect Shared File.zip
Report Date: 09/28/2009
Last Updated: 09/29/2017
Document ID: 51R2HCRZ