Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Best way to lock and open a file

Reply
Thread Tools

Best way to lock and open a file

 
 
Ron Vecchi
Guest
Posts: n/a
 
      07-26-2003
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


 
Reply With Quote
 
 
 
 
Neil Ramsbottom
Guest
Posts: n/a
 
      07-27-2003

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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
 
 
 
Kevin Spencer
Guest
Posts: n/a
 
      07-27-2003
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
Jacky Kwok
Guest
Posts: n/a
 
      07-28-2003
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
http://www.velocityreviews.com/forums/(E-Mail Removed)
(E-Mail Removed)

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Read-Write Lock vs primitive Lock() k3xji Python 7 12-30-2008 10:19 PM
Application.Lock()/UnLock() or lock(Application) nano2k ASP .Net Web Services 2 08-09-2007 09:31 AM
Threading - Why Not Lock Objects Rather than lock the interpreter Fuzzyman Python 3 12-05-2003 10:43 PM
RE: Threading - Why Not Lock Objects Rather than lock theinterpreter Robert Brewer Python 0 12-05-2003 05:33 PM
some way to lock an IE window open?????? JohnF Computer Support 3 12-03-2003 09:19 PM



Advertisments