Best way to lock and open a file

Discussion in 'ASP .Net' started by Ron Vecchi, Jul 26, 2003.

  1. Ron Vecchi

    Ron Vecchi Guest

    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
     
    Ron Vecchi, Jul 26, 2003
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:...
    > 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
    >
    >
     
    Neil Ramsbottom, Jul 27, 2003
    #2
    1. Advertising

  3. 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" <> wrote in message
    news:...
    > 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
    >
    >
     
    Kevin Spencer, Jul 27, 2003
    #3
  4. Ron Vecchi

    Jacky Kwok Guest

    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

     
    Jacky Kwok, Jul 28, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fuzzyman
    Replies:
    3
    Views:
    519
    Andrew MacIntyre
    Dec 5, 2003
  2. Robert Brewer
    Replies:
    0
    Views:
    507
    Robert Brewer
    Dec 5, 2003
  3. =?iso-8859-1?q?Elmo_M=E4ntynen?=

    How to lock files (the easiest/best way)?

    =?iso-8859-1?q?Elmo_M=E4ntynen?=, Jul 15, 2006, in forum: Python
    Replies:
    15
    Views:
    10,846
    Carl Banks
    Jul 18, 2006
  4. k3xji
    Replies:
    7
    Views:
    855
    Gabriel Genellina
    Dec 30, 2008
  5. nano2k

    Application.Lock()/UnLock() or lock(Application)

    nano2k, Jul 23, 2007, in forum: ASP .Net Web Services
    Replies:
    2
    Views:
    304
    nano2k
    Aug 9, 2007
Loading...

Share This Page