473,395 Members | 1,457 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Best way to lock and open a file

I have a single line file where the line will be read and updated each time
it is opened.
My question is I need to open it with a lock so noone else can read or write
to it untill the lock is released.

The problem seems that what If it is locked and A different request tries to
open it. From what I understand an exception will be thrown.

It is critical that each request reads the file. Is there an easy way to
make a request wait untill the file can be opened then read from it with out
generating the exception.

Thanks,

Ron Vecchi
Nov 17 '05 #1
3 8622

I dont think that there is a 'built-in' way to do this. Best way I can think
of would be to stick your file.open() in a loop until it can aquire a lock
on the file.
Neil Ramsbottom

"Ron Vecchi" <ve******@comcast.net> wrote in message
news:up**************@TK2MSFTNGP09.phx.gbl...
I have a single line file where the line will be read and updated each time it is opened.
My question is I need to open it with a lock so noone else can read or write to it untill the lock is released.

The problem seems that what If it is locked and A different request tries to open it. From what I understand an exception will be thrown.

It is critical that each request reads the file. Is there an easy way to
make a request wait untill the file can be opened then read from it with out generating the exception.

Thanks,

Ron Vecchi

Nov 17 '05 #2
When any process opens a file, it is automatically locked until it is
closed. This is usually for only a few milliseconds, if your programs are
working with the files correctly (that is, closing them as soon as
possible).

If you want to be sure that the file is not in use, just put the code to
open it in a try/catch block inside a loop. Then you can put the thread to
sleep for a few milliseconds each time through the loop (in the Catch block)
when the file can't be opened. Be sure to include a "time-out" routine to
prevent the loop from executing infinitely.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
Big things are made up of
lots of little things.

"Ron Vecchi" <ve******@comcast.net> wrote in message
news:up**************@TK2MSFTNGP09.phx.gbl...
I have a single line file where the line will be read and updated each time it is opened.
My question is I need to open it with a lock so noone else can read or write to it untill the lock is released.

The problem seems that what If it is locked and A different request tries to open it. From what I understand an exception will be thrown.

It is critical that each request reads the file. Is there an easy way to
make a request wait untill the file can be opened then read from it with out generating the exception.

Thanks,

Ron Vecchi

Nov 17 '05 #3
Ron Vecchi wrote:
I have a single line file where the line will be read and updated each time
it is opened.
My question is I need to open it with a lock so noone else can read or write
to it untill the lock is released.

The problem seems that what If it is locked and A different request tries to
open it. From what I understand an exception will be thrown.

It is critical that each request reads the file. Is there an easy way to
make a request wait untill the file can be opened then read from it with out
generating the exception.

Thanks,

Ron Vecchi

Hi:

If the file is just used by your own applications, you may try to use a
"System.Threading.Mutex" to synchronize all the threads to access the file.

The following info is copied from MSDN library
//
When two or more threads need to access a shared resource at the same
time, the system needs a synchronization mechanism to ensure that only
one thread at a time uses the resource. Mutex is a synchronization
primitive that grants exclusive access to the shared resource to only
one thread. If a thread acquires a mutex, the second thread that wants
to acquire that mutex is suspended until the first thread releases the
mutex.
//


--
Jacky Kwok
ja***@alumni.cuhk.edu.hk
ja***@compose.com.hk

Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: MLH | last post by:
I'm developing an app that I'd like to backup each time I open it for modifications. Is it too late to do so after opening the file with Access? Here's what I've been trying... Trevor Best posted...
0
by: Andrew Dowding | last post by:
Hi Everybody, I have been looking at problems with my Windows Forms C# application and it's little Jet 4 (Access) database for the last few days. The Windows Forms app implements a facade and...
14
by: Gary Nelson | last post by:
Anyone have any idea why this code does not work? FileOpen(1, "c:\JUNK\MYTEST.TXT", OpenMode.Binary, OpenAccess.ReadWrite, OpenShare.Shared) Dim X As Integer For X = 1 To 26 FilePut(1, Chr(X +...
5
by: marcello | last post by:
Hello I need to do this: 1 opening a file for writing/appending 2 to lock the file as for writing (i mean: the program that lock can keep writing, all others programs can't ) 3 wtite and...
7
by: nvx | last post by:
Hi, I have an application working with an Access database and I need to 'lock' the currently opened DB file in order to prevent it to be deleted, modified by other applications etc. Does anyone...
12
by: Elmo Mäntynen | last post by:
Is there something better than using fnctl? It seems a bit intimidating with a quick look.
14
by: John Salerno | last post by:
What is the best way to check if a file already exists in the current directory? I saw os.path.isfile(), but I'm not sure if that does more than what I need. I just want to check if a file of a...
5
by: =?Utf-8?B?U3BlZWR5?= | last post by:
Sorry for aksing such a basic question but I have searched the internet high and low and none of the solutions seem to help. Here is my (not working) code: FileStream fs = new...
5
by: pgdown | last post by:
Hi, I have several processes accessing files from one folder, but only one process should ever access each file. Once one process has the file, no other process should be allowed to access it,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.