0

I have several images binded to an ObservableCollection for a listview that I need to delete when I close the window and and generate again when I open the window again. Unfortunately, whenever I try to delete the images after closing the window I get the error "The process cannot access the file because it is being used by another process." My question is: How can I free the images from all processes when I close the window?

Here is the code for my WPF window:

    ObservableCollection<ModuleView> otherModules = new ObservableCollection<ModuleView>();

    public ValidationWindow()
    {
        Closed += WindowClosed;
        InitializeComponent();


        // Populate List viewer with ObservableCollection
        string resourceFolder = //path to resource folder
        for (int i = 1; i < nClusters - 1; i++)
        {
            if (i == currentCluster) continue;
            ModuleView obj = new ModuleView() { Module = i, ModuleText = "Module " + i, ImagePath = resourceFolder + "/" + i + ".png" };
            otherModules.Add(obj);
        }
        moduleViewer.ItemsSource = otherModules;
    }

    private void WindowClosed(object sender, EventArgs e)
    {
        otherModules.Clear();
        Close();
    }

This is my custom object for the Observable collection:

    class ModuleView
    {
        public int Module { get; set; }
        public string ModuleText { get; set; }
        public string ImagePath { get; set; }
    }

And this is where I open my window and try to delete the files; this code is being called from a button:

        // Delete image files
        string[] pngfiles = Directory.GetFiles(resourcesFolder, "*.png", SearchOption.TopDirectoryOnly);
        foreach (var file in pngfiles)
            File.Delete(file);  // ERROR HAPPENS HERE

        // Code to generate the images again

        // Open Validation Windows
        ValidationWindow myForm = new ValidationWindow();
        myForm.Show();

The error happens when I try to delete the files. I'm not sure what to do. I've tried clearing the Garbage Collector, clearing the Observable Collection list while closing the window but the error persists.

SofiaFeist
  • 53
  • 4
  • How are the images generated from their path? – Arkane Sep 13 '21 at 10:20
  • They are exported from Revit's API, which is an IDisposable class. [1]: https://www.revitapidocs.com/2021.1/c2e823a1-6eb0-2bf3-f07b-ed46d8f7b70a.htm – SofiaFeist Sep 13 '21 at 10:36
  • Well that library seems to be closed source, so that doesn't help much. Have you tried disposing each element before clearing the collection? Why don't you reuse the images instead? Are you sure you need that third-party library, wouldn't a `` (in your DataTemplate) work as well? – Arkane Sep 13 '21 at 10:57
  • 2
    `` invokes the default TypeConverter from string to ImageSource, i.e. an instance of the ImageSourceConverter class. That converter creates a BitmapFrame from the path, which keeps the file open. See the answers to the duplicate questions for how to deal with this situation. If you can't change the type of the view model property to ImageSource, bind to ImagePath via a Binding Converter that creates a BitmapImage as shown in the answers. – Clemens Sep 13 '21 at 11:18
  • @Clemens Those did solve my question! Thank you so much! I've been trying to solve this for days. I didn't find those at all in my research T-T – SofiaFeist Sep 13 '21 at 11:44

0 Answers0