Click here to Skip to main content
15,902,112 members
Home / Discussions / C#
   

C#

 
AnswerRe: File Watcher Pin
Alan N1-Feb-09 12:42
Alan N1-Feb-09 12:42 
AnswerRe: File Watcher Pin
Colin Angus Mackay1-Feb-09 12:47
Colin Angus Mackay1-Feb-09 12:47 
GeneralRe: File Watcher Pin
Bob Pawley1-Feb-09 13:14
Bob Pawley1-Feb-09 13:14 
GeneralRe: File Watcher Pin
Colin Angus Mackay1-Feb-09 13:27
Colin Angus Mackay1-Feb-09 13:27 
GeneralRe: File Watcher Pin
Bob Pawley1-Feb-09 13:41
Bob Pawley1-Feb-09 13:41 
GeneralRe: File Watcher Pin
Colin Angus Mackay1-Feb-09 13:49
Colin Angus Mackay1-Feb-09 13:49 
AnswerRe: File Watcher Pin
Colin Angus Mackay1-Feb-09 13:52
Colin Angus Mackay1-Feb-09 13:52 
Questionfilestream : read with and lock-unlock problem [modified] Pin
JossGP1-Feb-09 11:54
JossGP1-Feb-09 11:54 
Hi to all,
I am new in this forum. I was searching help about a recent problem i have got with filestream object.
Specifically I have the need that two or more users read and write a file content and so I coded these lines of code:

. . .
. . .  this code is in a function.. for simplicity I reported a snippet
public enum 	dbLockType	{ _none =0, _lock, _unLock, _default}
long lockPos = -1;
long lockRec = -1;
int  blockSize = 100;
 
  FileStream DtFile = new FileStream(@"\\server\d\folder\test.txt", FileMode.Open, FileAccess.ReadWrite,
 FileShare.ReadWrite,8000); 
. . .
. . .

public bool getBufferFromDisk(long RecordNumber, byte[] toArray, int startingAt, int length, dbLockType dbLock)
{
  long seekPosition = GetFilePos(RecordNumber); // calc RecordNumber*blockSize
  try {
	DtFile .Seek(seekPosition, System.IO.SeekOrigin.Begin);
	DtFile .Read(toArray, startingAt, length);
	if ( dbLock == dbLockType._lock) {
  	   if ( lockPos > -1)
		DtFile .Unlock(lockPos, blockSize);  // unlock previous locks
	   DtFile .Lock(seekPosition, blockSize); 
	   
	   lockPos = seekPosition;
	   lockRec = RecordNumber;
	}
  }
  catch(IOException e) {
	return false;	
  }
  return true;
}


After that the first user open the file and call the getBufferFromDisk with these parms:

getBufferFromDisk( 8, toArray, 0, 100, dbLock);

in this way it lock the filestream at pos (7 x100) for 100 bytes.


the second user try to open the file and to execute the same function with these parms:
getBufferFromDisk( 0, toArray, 0, 100, dbLock);

at this point I got an error telling me that the file is partially locked by other process.
I found the prorblem occur at this line :
DtFile .Read(toArray, startingAt, length);

Stange , because I have locked 100 bytes starting from pos 700 not at 0.

After some reading I found this article that seems more like my problem :

So I tried to go further and after some investigation I found :
1. Even though I change the FileAccess to .Read the second user find the record at pos 0 locked.
2. the lock is extended to the whole buffer , that is 8K.
3. all this is true if the second user try to open and read something in this buffer if something in this buffer is locked otherwise all is ok!, I mean if the first user lock something at pos 8500 , 100 the second user has no problem at all.

What is this , a bug? Am I wrong in something ? this is the correct way to lock-unlock regions ?.

Could someone help me to get rid of this problem ?

Thanks to all ?

JossGP

modified on Monday, February 2, 2009 5:01 AM

AnswerRe: filestream : read with and lock-unlock problem Pin
JossGP1-Feb-09 11:57
JossGP1-Feb-09 11:57 
AnswerRe: filestream : read with and lock-unlock problem Pin
Colin Angus Mackay1-Feb-09 12:49
Colin Angus Mackay1-Feb-09 12:49 
GeneralRe: filestream : read with and lock-unlock problem Pin
JossGP1-Feb-09 13:27
JossGP1-Feb-09 13:27 
GeneralRe: filestream : read with and lock-unlock problem Pin
Colin Angus Mackay1-Feb-09 13:28
Colin Angus Mackay1-Feb-09 13:28 
Generalmethod overloading.constructor overl, delegates. Pin
Ali_1001-Feb-09 8:10
Ali_1001-Feb-09 8:10 
GeneralRe: method overloading.constructor overl, delegates. Pin
Colin Angus Mackay1-Feb-09 12:54
Colin Angus Mackay1-Feb-09 12:54 
GeneralRe: method overloading.constructor overl, delegates. Pin
Dragonfly_Lee1-Feb-09 18:47
Dragonfly_Lee1-Feb-09 18:47 
QuestionVideo Download's No Idea!!! Pin
s4n4t4n1-Feb-09 7:49
s4n4t4n1-Feb-09 7:49 
AnswerRe: Video Download's No Idea!!! Pin
harold aptroot1-Feb-09 9:18
harold aptroot1-Feb-09 9:18 
Questionobtaining local lan address,,, Pin
Mubeen.asim1-Feb-09 7:28
Mubeen.asim1-Feb-09 7:28 
AnswerRe: obtaining local lan address,,, Pin
CPallini1-Feb-09 8:03
mveCPallini1-Feb-09 8:03 
GeneralRe: obtaining local lan address,,, Pin
Mubeen.asim1-Feb-09 8:04
Mubeen.asim1-Feb-09 8:04 
Questionaccessing shared database... Pin
Mubeen.asim1-Feb-09 6:43
Mubeen.asim1-Feb-09 6:43 
AnswerRe: accessing shared database... Pin
Mubeen.asim1-Feb-09 7:24
Mubeen.asim1-Feb-09 7:24 
QuestionCustom IE button calling BHO Pin
Haim Nachum1-Feb-09 6:32
Haim Nachum1-Feb-09 6:32 
QuestionSaving text file with StreamWriter Pin
Mark F.1-Feb-09 5:27
Mark F.1-Feb-09 5:27 
AnswerRe: Saving text file with StreamWriter Pin
PIEBALDconsult1-Feb-09 6:42
mvePIEBALDconsult1-Feb-09 6:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.