LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to lock " Write to measurement file VI" during run time of application so that file can't be opened during run time.

Solved!
Go to solution

Hello,

 

I have an application where I'm Acquiring data from external hardware and  saving it into a file using "Write to measurement file VI".

 

During application run time; when user opens ".LVM" text file; application abendons saving of acquired data into prescribed file but application keeps on acquiring data from external hardware. When I stop application; an error message is poped up showing an error occured due to file opened at run time.

 

My question is:  How I can lock that file in which data is being saved, so that user can not open it during run time of application?  

 

 

Looking forward for your kind suggestions on this matter.

 

Regards,

 

Jamal

0 Kudos
Message 1 of 13
(4,548 Views)

Jamal,

 

there might be some Windows SDK functions available to do this, but i never done such a thing.

That being said, i think you should simply modify your approach of saving the file.

 

Usually, when a Windows process (e.g. LabVIEW.exe) opens a file reference, the OS will grant exclusive write access rights (potentially overall exclusive rights) to that process. The OS will release that if the process releases the reference to the file.

Write to Measurement File contains code to open and release files. It will always close the reference to the file before stopping execution. Therefore, this VI does NOT continuously hold (read "block) a file reference.

 

The proposed solution is rather simple:

Do not use Express VIs for such things if you want to have the option to manage resources on your own. Write your own file logger which actively holds the file reference from start to end of your application.

 

Norbert

Norbert
----------------------------------------------------------------------------------------------------
CEO: What exactly is stopping us from doing this?
Expert: Geometry
Marketing Manager: Just ignore it.
Message 2 of 13
(4,521 Views)

Thanks for reply. What is alternate option to write into file except using express VIs or other builtin funtions?

 

 Would you please suggest me that method?

 

Regards,

 

 

Jamal

0 Kudos
Message 3 of 13
(4,509 Views)

Hi Jamal,

 

the "alternative" is to get rid of the ExpressVI. Use "standard" functions for your file write routine!

 

That way you con open the file exclusivly for your program and you can keep it open as long as your program runs. Other users/programs can#t access the file in such a case…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 4 of 13
(4,499 Views)

Hi GerdW,

 

Thanks for reply. Could you please let me know the alternate standard way of writting data into file?

 

I want my application to lock that file while writting data into it so that any user can't access or open that file while application is writting into it.

 

Pls suggest that method.

 

Regards,

 

Jamal

0 Kudos
Message 5 of 13
(4,493 Views)
Solution
Accepted by Mr_Engineer

Hi Jamal,

 

as has been said several times before: use simple plain file functions!

 

check.png

Opening a file with default "read/write" access will disable access to that file for other programs!

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 6 of 13
(4,485 Views)

Hi GerdH,

 

Thanks for reply.  Infact the purpose of using express VI for writting into file provides more flexibility. If file is already availble; express VI " Write to measurement file" creates another file with number increament since user do not have to provide file path before each execution of application. That is the specific reason to use express VI.

 

I just want to lock file being written by Express VI or LabVIEW application specifically. Could you or anyone else sugget " How to lock file being written by Expree VI or LabVIEW application?".

 

Regards

0 Kudos
Message 7 of 13
(4,477 Views)
Solution
Accepted by Mr_Engineer

Jamal,

 

as we told, the Express VI doesn't provide the lock and the simplest solution is to break up the Express VI to custom implementation.

Maybe you don't know that: You can convert the Express VI to a standard subVI by right clicking the blue Express VI and select "Open Front Panel". This will remove the option to re-configure the VI as it wont be an Express VI anymore (see dialog).

However, you can now go into the VI and review the code. Please note that the code looks quite messy as parts of it is implemented dynamically using VI Scripting (so no human checking for implementation guide lines!). This enables you to learn how this VI handles "multiple files" as well as appending to existing files. Feel free to re-use parts of the VI (with cleaning up code!) to implement your own file handler!

 

Again: The important thing is that you open the file once in the beginning and release (close) the file only if you want to release the exclusive access to the file from your LV built application!

 

Norbert

Norbert
----------------------------------------------------------------------------------------------------
CEO: What exactly is stopping us from doing this?
Expert: Geometry
Marketing Manager: Just ignore it.
Message 8 of 13
(4,467 Views)

Hi Jamal,

 

Infact the purpose of using express VI for writting into file provides more flexibility.

Wrong. ExpressVIs are provided with very limited possibilities for flexibility. They are made for just one purpose - when your task needs more flexibility, you need to program your own functions!

 

express VI " Write to measurement file" creates another file with number increament since user do not have to provide file path before each execution of application.

What's the problem to program that functionality on your own? "IF file exists THEN add suffix to filename"…

 

That is the specific reason to use express VI.

The purpose of ExpressVIs is to allow beginners to create small VIs with less effort…

 

I just want to lock file being written by Express VI

You can't. That ExpressVIs opens/closes the file with each call - when the file is closed every other application has access to the file…

 

or LabVIEW application specifically.

This has been told several times before by now…

 

Could you or anyone else sugget " How to lock file being written by Expree VI or LabVIEW application?".

We told you how to do it, I even included a snippet to show you the way…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 9 of 13
(4,465 Views)

The way to have the file locked while your application runs is to do as the others have suggested and open the file references yourself at the start of the file, hold it open and then close it when your application shuts down.

 

Express VIs are good for 'Getting Started' with LabVIEW, but they do not give you the power to use the language to its full extent.

 

You can convert an Express VI to a standard SubVI - but for some Express VIs, the code it produces is pretty difficult to understand. You also can change the file permissions to prevent write access - but you would have to manage this yourself around the Express VI - it's asking for trouble. You could have proper error handling that tells the user to close the file (or writes to a new file) if it is open when you try to write to the file.

 

The code for 'If file is already availble; express VI " Write to measurement file" creates another file with number increament' is pretty easy to implement.

 

 

 

 


LabVIEW Champion, CLA, CLED, CTD
(blog)
Message 10 of 13
(4,456 Views)