![]() |
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 |
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" <vencenzo@comcast.net> wrote in message news:upfFvZ8UDHA.1052@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 > > |
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 http://www.takempis.com Big things are made up of lots of little things. "Ron Vecchi" <vencenzo@comcast.net> wrote in message news:upfFvZ8UDHA.1052@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 > > |
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 jacky@alumni.cuhk.edu.hk jacky@compose.com.hk |
| All times are GMT. The time now is 04:10 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.