FileStream.Lock, FileStream.Unlock and its sync.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John

    FileStream.Lock, FileStream.Unlock and its sync.

    I've got multiple threads and processes that write to same
    file. Before writing all threads / processes first lock
    part of file and then write to file. If one thread /
    process locks file, another threads / processes can not
    lock this file again. And I want that all threads /
    processes wait until first thread frees lock by calling
    FileStream.Unlo ck. But I do not know what code should I
    use that all threads / processes wait until first thread /
    process unlock file.


    My code :

    FileStream FS = new FileStream
    ("\\Logs\\Criti calError.log",F ileMode.Append, FileAccess.Wri
    te,FileShare.Re adWrite);
    BinaryWriter BW = new BinaryWriter(FS );

    try {FS.Lock(0,10); }
    catch (IOException E) { // This exception is thrown if
    file is already locked
    // Here I want that my thread waits until file is unlocked
    and then I can lock file successfully.
    }

    ....

    FS.Unlock(0,10) ;
    FS.Close();

    What sync function should I use that my thread should
    waited until file is unlocked ?

    Can you give me short sample.

    Thank you very much.
Working...