1

The application is a desktop document management system. Image files (of scanned docs) are stored within a shared network folder and its indices within a database. Now, when the image of a selected document page is displayed the user has the option to delete it (via a contextual menu). The problem is, if I try to do this then it throws an exception (the resource is locked) which has all the sense given that it's being shown on screen. So, currently I maintain a persistent delete queue. Once the app starts I go to the queue and delete the pages of the documents whose indices were deleted from DB and given that they aren't being displayed the deletion succeed but this seems to be pretty bad code (I mean it works, but not as clean as it should, I guess).

How bad my quick solution is. Given that the app is single-user then the user needs to star the app to use it. Is this a very bad idea or can I implemented using another path.

The images are shown (within the document viewer) by binding it to the current file:

View:

<Image Name="PageViewedPath" Margin="20" Grid.Column="0" />

ViewModel:

public string PageViewedPath { get; set; }

And once the user clicks next or previous I change (within the ViewModel the PageViewedPath). Maybe the problem is this binding which I can't control in detail, I'm using Caliburn Micro so that's why just by setting the image name the binding is done.

I think maybe overriding this binding and creating a hardcopy of the image before is being shown must work but I'm not sure if it will and worse, how to do it.

Erre Efe
  • 15,387
  • 10
  • 45
  • 77

2 Answers2

1

I've had a similar problem in an application I developed that was using an image pool. Although the image was not displayed anymore, the file was locked and could not be deleted.

I solved my problem by loading images with BitmapCacheOption.OnLoad, something like this:

Image myImage = new Image();
BitmapImage bi = new BitmapImage();
bi.BeginInit();

bi.CacheOption = BitmapCacheOption.OnLoad;
bi.UriSource = imageUri;

// End initialization.
bi.EndInit();
myImage.Source = bi;

Here's a link to an msdn post that shows how to use BitmapCacheOption from xaml:

http://social.msdn.microsoft.com/forums/en-US/wpf/thread/3cb97997-941f-42a8-a03b-f84b152c1139/

ekholm
  • 2,543
  • 18
  • 18
  • Thanks ekholm. Actually needed to create a converter (using your provided code) but now everything is working fine. Thanks again. – Erre Efe Jun 24 '12 at 04:56
0

If you code lock the files from your own code - stop locking. You probably missing some using/Dispose calls somewhere around loading an image.

If it is not your code or you need to handle failures due to using shared files location - your solution may be ok. Also most users will not expect such behavior - my normal expectation is that file is either deleted instantaneously or never.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179