Reputation: 2940
I have a large text file (0.5 gig) which I need to parse over and over in different situations, as much as 40 times in a single method. Of course this is going to take a long time and I tried to work over the file faster by doing it concurrently. I understand that a MemoryMappedFile
is great for handling large files and concurrency so I chose to use it.
Now, I'm concurrently creating two views of the file (the views are of two different parts) but while one view works great the other one throws an UnauthorizedAccessException
. Here's the guilty code:
private void PartitionAndAnalyzeTextBlock(int start, int length)
{
Console.WriteLine("Starting analysis");
//Exception thrown here
using (var accessor = file.CreateViewAccessor(start, length, MemoryMappedFileAccess.Read))
{
char[] buffer = new char[BufferSize];
for (long i = 0; i < length; i += 5)
{
accessor.ReadArray(i, buffer, 0, 5);
string retString = new string(buffer);
frequencyCounter.AddOrUpdate(retString, 1, (s, j) => j++);
}
}
Console.WriteLine("Finished analysis");
}
file
is instantiated in this line:
private MemoryMappedFile file = MemoryMappedFile.CreateFromFile(path, FileMode.Open, "MemoryMappedPi");
Do you have any idea what would cause this?
Upvotes: 2
Views: 1054
Reputation: 1389
That's maybe related on how you create your memory mapped file. Check the answer by John Skeet on this post. MemoryMappedFileAccess.Read access is passed to the CreateFromFile method.
EDIT: As indicated by the comments, the CreateViewAccessor method takes an offset and a size as parameters to determine which part of the file the view will access. If these values fall outside the actual size of the file, an UnauthorizedAccessException will be thrown.
Upvotes: 2