From: Brent on
Better late than never...

I discovered this bug last month and it is fixed in the Jan 2010 rollup.

Essentially this is a problem with the internal .NetCF seek pointer getting out of sync with the OS seek pointer.

You can find the fix described at

http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=0a003b66-5c00-43c2-8658-5453be22c402

and downloadable at
http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=0a003b66-5c00-43c2-8658-5453be22c402

Thanks
-Brent





Jon Payne wrote:

FileStream bug?
07-Dec-07

I'm finding that in certain cases I'm getting inconsistent results
from FileStream.Read after a calling Seek. The problem only occurs
after certain patterns of Read and Seek. Calling Flush before Read
removes the problem but I cannot find anything in the documentation to
indicate this is required and I'm concerned that this could cause
performance problems. I am using the Compact Framework version
2.0.7045.0.

Can anyone confirm if this is a bug or if I am doing something wrong?

The following code and sample output demonstrates the problem:

using System;
using System.IO;

static class Program
{
static void Log(string t)
{
System.Diagnostics.Debug.WriteLine(t);
}

static void Main()
{
// create a test file
using (FileStream stream = new FileStream(@"\test.dat",
FileMode.Create))
{
for (int i = 0; i < 1000; ++i)
{
stream.WriteByte((byte)(i % 256));
}
}

// demonstrate issue
using
(
FileStream stream = new FileStream(
@"\test.dat", FileMode.Open,
FileAccess.Read, FileShare.Read, 128
)
)
{
byte[] buffer = new byte[500];

stream.Read(buffer, 0, 252);
stream.Read(buffer, 0, 102);
stream.Read(buffer, 0, 5);
stream.Read(buffer, 0, 63);
stream.Read(buffer, 0, 39);
stream.Read(buffer, 0, 34);
stream.Read(buffer, 0, 73);
stream.Read(buffer, 0, 73);
stream.Read(buffer, 0, 73);
stream.Read(buffer, 0, 73);

long pos = stream.Position;

Log(string.Format("At {0}", stream.Position));
stream.Read(buffer, 0, 73);
Log(string.Format("Read {0}, {1}, {2}...", buffer[0], buffer[1],
buffer[2]));

Log(string.Format("Seek to {0}", pos));
stream.Seek(pos, SeekOrigin.Begin);

Log(string.Format("At {0}", stream.Position));
stream.Read(buffer, 0, 73);
Log(string.Format("Read {0}, {1}, {2}...", buffer[0], buffer[1],
buffer[2]));
}
}
}

Sample output:

At 787
Read 19, 20, 21...
Seek to 787
At 787
Read 1, 2, 3...

Regards,

Jon Payne

Previous Posts In This Thread:

On Friday, December 07, 2007 1:50 AM
Jon Payne wrote:

FileStream bug?
I'm finding that in certain cases I'm getting inconsistent results
from FileStream.Read after a calling Seek. The problem only occurs
after certain patterns of Read and Seek. Calling Flush before Read
removes the problem but I cannot find anything in the documentation to
indicate this is required and I'm concerned that this could cause
performance problems. I am using the Compact Framework version
2.0.7045.0.

Can anyone confirm if this is a bug or if I am doing something wrong?

The following code and sample output demonstrates the problem:

using System;
using System.IO;

static class Program
{
static void Log(string t)
{
System.Diagnostics.Debug.WriteLine(t);
}

static void Main()
{
// create a test file
using (FileStream stream = new FileStream(@"\test.dat",
FileMode.Create))
{
for (int i = 0; i < 1000; ++i)
{
stream.WriteByte((byte)(i % 256));
}
}

// demonstrate issue
using
(
FileStream stream = new FileStream(
@"\test.dat", FileMode.Open,
FileAccess.Read, FileShare.Read, 128
)
)
{
byte[] buffer = new byte[500];

stream.Read(buffer, 0, 252);
stream.Read(buffer, 0, 102);
stream.Read(buffer, 0, 5);
stream.Read(buffer, 0, 63);
stream.Read(buffer, 0, 39);
stream.Read(buffer, 0, 34);
stream.Read(buffer, 0, 73);
stream.Read(buffer, 0, 73);
stream.Read(buffer, 0, 73);
stream.Read(buffer, 0, 73);

long pos = stream.Position;

Log(string.Format("At {0}", stream.Position));
stream.Read(buffer, 0, 73);
Log(string.Format("Read {0}, {1}, {2}...", buffer[0], buffer[1],
buffer[2]));

Log(string.Format("Seek to {0}", pos));
stream.Seek(pos, SeekOrigin.Begin);

Log(string.Format("At {0}", stream.Position));
stream.Read(buffer, 0, 73);
Log(string.Format("Read {0}, {1}, {2}...", buffer[0], buffer[1],
buffer[2]));
}
}
}

Sample output:

At 787
Read 19, 20, 21...
Seek to 787
At 787
Read 1, 2, 3...

Regards,

Jon Payne


Submitted via EggHeadCafe - Software Developer Portal of Choice
Adding WCF Service References
http://www.eggheadcafe.com/tutorials/aspnet/a1647f10-9aa4-4b0c-bbd9-dfa51a9fab8e/adding-wcf-service-refere.aspx