I have the following code:
try
{
string fileName = imageQueue.Dequeue();
FileStream fileStream = File.Open(
fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
Bitmap bitmap = new Bitmap(fileStream);
Image picture = (Image)bitmap;
pb.Tag = fileName;
pb.Image = picture;
return true;
}
catch (Exception ex)
{
errorCount++;
//If another PC has this image open it will error
return false;
}
Because this program is running on 2 PC’s accessing the same folder to pick files up it will throw an exception when one has a file open and then move onto the next file in its list.
When I open the application on 2 PC’s at the same time the first PC manages to open the image but the second doesn’t. I am displaying 4 images at once on screen but doing some debugging shows that the second PC is taking 10.5 seconds to fail at opening 4 files before it finds one it can open.
Why is this so expensive and what can I do to speed it up?
UPDATE: I give it exclusive access because I want the applications to show unique images so PC1 shows image 1,2,3,4 and PC shows 5,6,7,8 because it cant get acccess to 1,2,3,4. I also then free the filestream once I’m done with it and at the last possible moment so it prevents other applications trying to open it.
I can’t answer definitively, but my best suggestion is that something in the system, either in the .net framework classes or the file system, is implementing a timeout/retry mechanism in case of file sharing failures. This would explain the inordinate delay you report.