0

Before I begin, I should mention that this isn't really something I want to do, I'm simply curious about how it works.

I have this method called AddLayer(), which opens a local geodatabase file and creates a map with it:

public async void AddLayer() { try { // open a geodatabase on the local device gdb = await Geodatabase.OpenAsync(@"..\..\..\test.geodatabase"); // get the first geodatabase feature table var gdbFeatureTable = gdb.FeatureTables.FirstOrDefault(); //create a layer for the feature table lyr = new FeatureLayer { ID = gdbFeatureTable.Name, DisplayName = gdbFeatureTable.Name, FeatureTable = gdbFeatureTable }; // add the graphics to the map MyMapView.Map.Layers.Add(lyr); } catch (Exception ex) { MessageBox.Show("Unable to create offline database: " + ex.Message); } return; } 

I also have another method called RemoveLayer(), which simply un-does everything AddLayer() did and then calls GC.Collect().

My question is, why, even after no longer using the resources in the file and calling the garbage collector, can I not delete the file (the geodatabase file) while the program is running?

Is this normal behavior for all programs in Windows? Is it to avoid somehow corrupting the program?

Thank you all for helping me understand this.

2
  • 1
    Don't use async void. Commented Dec 8, 2016 at 20:51
  • I assume gdb is a member variable, and you don't show where/host it is disposed. That being the result of an open operation is probably where any open file handles would be; but without knowing much more about it or how you're disposing it, it's hard to say what specifically you could do. Commented Dec 8, 2016 at 21:50

1 Answer 1

1

This code below ended up working for me:

public void OpenGeodatabase() { Geodatabase gdb = null; // path to .geodatabase var gdbPath = @"..\..\..\test.geodatabase"; // wrap OpenAsync call in Task Task.Run(async () => { // open a geodatabase on the local device gdb = await Geodatabase.OpenAsync(gdbPath); }).Wait(); // get the first geodatabase feature table var gdbFeatureTable = gdb.FeatureTables.FirstOrDefault(); // create a layer for the feature table var lyr = new FeatureLayer { ID = gdbFeatureTable.Name, DisplayName = gdbFeatureTable.Name, FeatureTable = gdbFeatureTable }; // add the graphics to the map MyMapView.Map.Layers.Add(lyr); // remove the layer - to make it similar to case explanation MyMapView.Map.Layers.Remove(lyr); // make gdb reference null gdb = null; gdbFeatureTable = null; lyr = null; // call garbage collector GC.Collect(); GC.WaitForPendingFinalizers(); // If the works, the lock has been removed System.IO.File.Delete(@"..\..\..\test.geodatabase"); } 

Basically I did everything inside of a non-async method, wrapped the call to OpenAsync within a Task, then set everything made from the geodatabase to null after use. Finally, I called the garbage collector and deleted the file.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.