public void BeginSaveImage(TileIndex id, BitmapImage image) { string imagePath = GetImagePath(id); bool errorWhileDeleting = false; bool containsOld = Contains(id); if (containsOld && saveOption == SaveOption.ForceUpdate) try { File.Delete(imagePath); } catch (IOException exc) { // todo возможно, тут добавить файл в очередь на удаление или перезапись новым содержимым // когда он перестанет быть блокированным Debug.WriteLine(String.Format("{0} - error while deleting tile {1}: {2}", Name, id, exc.Message)); errorWhileDeleting = true; } bool shouldSave = saveOption == SaveOption.ForceUpdate && !errorWhileDeleting || saveOption == SaveOption.PreserveOld && !containsOld; if (shouldSave) { Debug.WriteLine("Began to save id = " + id); Statistics.IntValues["ImagesSaved"]++; ImageSaver saver = ScreenshotHelper.SaveBitmapToFile; saver.BeginInvoke((BitmapImage)image.GetAsFrozen(), imagePath, null, null); } }
protected BitmapImage BeginLoadImageAsync(TileIndex id) { //Debug.Assert(Contains(id)); string imagePath = GetImagePath(id); BitmapImage bmp = new BitmapImage(); bmp.BeginInit(); bmp.CacheOption = BitmapCacheOption.OnLoad; bmp.UriSource = new Uri(imagePath); try { bmp.EndInit(); } catch (NotSupportedException exc) { Debug.WriteLine(String.Format("{0}: failed id = {1}. Exc = \"{2}\"", GetCustomName(), id, exc.Message)); Action corruptedFileDeleteAction = () => { try { File.Delete(imagePath); } catch (Exception e) { Debug.WriteLine("Exception while deleting corrupted image file \"" + imagePath + "\": " + e.Message); } }; lock (fileDeleteActions) { fileDeleteActions.Add(corruptedFileDeleteAction); if (!corruptedFilesDeleteTimer.IsEnabled) corruptedFilesDeleteTimer.Start(); } return null; } catch (FileNotFoundException exc) { Debug.WriteLine(String.Format("{0}: failed id = {1}. Exc = \"{2}\"", GetCustomName(), id, exc.Message)); return null; } return (BitmapImage)bmp.GetAsFrozen(); }