473,386 Members | 1,835 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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.Unlock. 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\\CriticalError.log",FileMode.Append,FileA ccess.Wri
te,FileShare.ReadWrite);
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.
Jul 21 '05 #1
0 4487

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Bohus | last post by:
I want that my thread sleeps until file is unlocked, but if I wait for signaled state of FileStream handle, it does not work and ARE.WaitOne() function returns immediately, even if file is locked...
0
by: John | last post by:
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...
2
by: Daniel | last post by:
Hi all, I have problem in using the filestream . below is my sample code: Dim s2 As New FileStream("C:Save\Grab.xls", FileMode.Open, FileAccess.Read, FileShare.Read) s2.Lock(0, 0) Dim...
14
by: Gary Nelson | last post by:
Anyone have any idea why this code does not work? FileOpen(1, "c:\JUNK\MYTEST.TXT", OpenMode.Binary, OpenAccess.ReadWrite, OpenShare.Shared) Dim X As Integer For X = 1 To 26 FilePut(1, Chr(X +...
6
by: bonk | last post by:
I am trying to create a stream that writes text to a file and: - automatically creates a new file once the current file exceeds a certain size - makes it possible to be used by multiple threads...
2
by: valentin tihomirov | last post by:
It is quite convenient to use the basic primitive: lock(object) { method(); } On the other hand, rwLock.AcquireReaderLock(-1); try { // unlock finally
45
by: Chris Forone | last post by:
hello group, is there a chance for other functions to get the lock if i have following loop: while (running) { Lock local(mutex); }
8
by: Clive Dixon | last post by:
Are there any issues I should be worried about when using C# 2.0 iterators in conjunction with lock, i.e. public IEnumerator GetEnumerator() { lock (lockObject) { foreach (object obj in...
6
by: DaveRook | last post by:
Hi I have some code from a friend and I don't understand why they have used the application lock feature! I understand this produces a random quote, but can some one explain to me what happens...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.