Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Synchronisation in file management

Status
Not open for further replies.

jabond007

Programmer
Jun 21, 2000
39
US
hello,

i have an app which reads data in files in te specified directory.

I am able to read the existing files and update the database. What i need is synchonisation. Reading operation is a timer based operation. How do i ensure that when the file is being written i dont try to access it. How do i ensure wait till the data is written into the file.

thank you
 
There are different ways of doing this. You can 'lock' the file by either setting a flag in a text file and reading from the file to check for the latest status. In case the flag is set you can assume that the file is being overwritten by some other process. As soon as that process completes reading/writing the file, the flag in the text file can be re-set. You can also use the Windows registry for this purpose for more security or an field in an Access table, maybe a control type table if you have one in your application
 
The Text file of the registry log suggested by Antzz would work, but be careful with with the Text file . . . what happens if one process tries to open the file to read a lock status while another process has the file open to update the lock status?
Another idea would be to use a Semaphore or a Mutex (these can be created in VB6 using the threading APIs . . . I have a working applications that uses them) to control access to your files (or any other limited resource). Basically, you create a semaphore and then before you try to access the file, call the WaitForSingleObject API. If the semaphore is not signalled (i.e. someone else hase already used the semaphore to access the file) then you other application will wait. The WaitForSingleObject API also has a timeout value, so you application will not just lockup forever . . . if it can't get to the file (for whatever reason) thenit will time out and return an error code idicating that it has timed out. If you would like to see an example of this, please let me know and I will post one. (One more note . . . VB.Net Beta 1 has free threading and Mutexes built right into the language so in the future, you will not have to rely on the API calls to do this.)
 
thanks jmarler,

Could u send me the example as indicated by u.
My application is trying to read a file given by a different application altogether. I dont want my application to try and read the file while it is being written into a directory. Right now i am using a FileListbox to select read only files in the direcotory to be read in by my application. this works fine. But if this is automated...it could cause problems...with both apps trying to write and read at the same time.



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top