Locked resources (image files) management

喜欢而已 提交于 2019-12-11 04:23:48

问题


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.


回答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/




回答2:


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.



来源:https://stackoverflow.com/questions/11161653/locked-resources-image-files-management

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!