c# - COM object has a handle to a file that needs to be deleted before GC -


i have .net application using bunch of com objects (esri arcgis) convert data in file 1 format , export file in format.

i've tried looking arcgis solution problem unsuccessful after day or of research, haven't included tag on question, it's unimportant com objects are.

okay, problem when there exception during export process. catch exception, present message user, , delete export file.

the problem file.delete call throws exception because com object associated data export still has handle file, com object has no "close" method, , gc has not reclaimed memory yet.

here code:

            try {                 ...             }             catch(exception)             {                 if (directory.exists(outputfolder) && file.exists(pgdbpath))                 {                     // release references com object holding handle .mdb file.                     int refs;                     var targetworkspacename = getworkspacename(pgdbpath, "esridatasourcesgdb.accessworkspacefactory");                      // com object w/ handle file @ 'pgdbpath'                     var targetworkspace = getworkspace(targetworkspacename);                                         {                         refs = system.runtime.interopservices.marshal.releasecomobject(targetworkspace);                     } while (refs > 0);                      // need use gc clean unmanaged memory pointed released references.                     gc.collect();                     gc.waitforpendingfinalizers();                     gc.collect();                     gc.waitforpendingfinalizers();                      // throw file access exception without gc.                     file.delete(pgdbpath);                              }                  throw;             } 

i'm relatively new working com objects in .net, there no other way release reference , clean memory associated object other forcing garbage collection? hate it. thanks.


Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -