Apprentice leaves lockfile.lck files behind

By Adam Nagy

When using Apprentice, it creates lockfile.lck files in the folders where the files you open with it reside. These only disappear once the process using Apprentice exited.
However, as soon as you close the document the related lock file becomes deletable, so nothing prevents you from deleting them or the folders they are in.

If you want to get rid of them as soon as possible then you could create a class that removes them automatically once you closed the Apprentice document. In .NET you could do something like this:

using System; using Inv = Inventor; using SysIO = System.IO; using Inventor; using System.Collections.Generic;   namespace ApprenticeConsoleCS {   class Program   {    class LckMonitor     {       ApprenticeServerComponent asc;       Dictionary map;         public LckMonitor(ApprenticeServerComponent asc)       {         this.asc = asc;         map = new Dictionary();       }        public ApprenticeServerDocument Open(string fileName)       {         var folder = System.IO.Path.GetDirectoryName(fileName);          // Keep track of it         if (map.ContainsKey(folder))           map[folder]++;         else           map[folder] = 1;          // Open it         return asc.Open(fileName);        }        public void Close(ApprenticeServerDocument doc)       {         var folder = System.IO.Path.GetDirectoryName(doc.FullFileName);          // Close it         doc.Close();          // The folder should be in here already         System.Diagnostics.Debug.Assert(map.ContainsKey(folder));          map[folder]--;          // If no one is referencing it now         // then let's try to clean it up         // It could be that another application will hold          // on to it though         if (map[folder] < 1)         {           map.Remove(folder);            try           {             System.IO.File.Delete(folder + "\lockfile.lck");           }           catch { }         }       }     }      [STAThread]     static void Main(string[] args)     {       string filename = @"C:temptest.ipt";        ApprenticeServerComponent asc = new ApprenticeServerComponent();       LckMonitor lckMonitor = new LckMonitor(asc);        ApprenticeServerDocument asd = lckMonitor.Open(filename);        // Interact with the apprentice document        lckMonitor.Close(asd);      }   } }

Another thing you could do is migrate the Apprentice related functionality into a separate app. That way when the side process finishes the Apprentice component in it will finish too and will clean up those lock files. This blog post's solution includes that option as well.  


Comments

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading