Professional Web Applications Themes

Best way to lock and open a file - ASP.NET General

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 ...

  1. #1

    Default 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


    Ron Vecchi Guest

  2. #2

    Default Re: Best way to lock and open a file


    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" <vencenzocomcast.net> wrote in message
    news:upfFvZ8UDHA.1052TK2MSFTNGP09.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
    >
    >

    Neil Ramsbottom Guest

  3. #3

    Default Re: Best way to lock and open a file

    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
    [url]http://www.takempis.com[/url]
    Big things are made up of
    lots of little things.

    "Ron Vecchi" <vencenzocomcast.net> wrote in message
    news:upfFvZ8UDHA.1052TK2MSFTNGP09.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
    >
    >

    Kevin Spencer Guest

  4. #4

    Default Re: Best way to lock and open a file

    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
    [email]jackyalumni.cuhk.edu.hk[/email]
    [email]jackycompose.com.hk[/email]

    Jacky Kwok Guest

Similar Threads

  1. Anyone else have a file lock problem?
    By Bart_Androski@adobeforums.com in forum Adobe Illustrator Windows
    Replies: 21
    Last Post: July 5th, 04:30 PM
  2. lock file
    By maryvv in forum Macromedia Contribute General Discussion
    Replies: 1
    Last Post: May 29th, 09:21 AM
  3. #40008 [NEW]: lock file
    By wisans at gmail dot com in forum PHP Bugs
    Replies: 2
    Last Post: January 4th, 02:43 AM
  4. Open file, make changes, save file, close, re-open, file contents not changed
    By brock@bergdesign.com in forum Adobe Illustrator Macintosh
    Replies: 7
    Last Post: March 7th, 07:29 PM
  5. File Lock
    By Amrith in forum UNIX Programming
    Replies: 4
    Last Post: July 11th, 05:30 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139