首页 > 代码库 > ArcGIS Engine中正确释放打开资源

ArcGIS Engine中正确释放打开资源

转自原文 ArcGIS Engine中正确释放打开资源

AE中对MDB,SDE等数据库操作时,打开后却往往不能及时释放资源,导致别人操作提示对象被锁定。

很多帖子说了很多原理,看的也烦且不实用,比如一句话概括的用System.Runtime.InteropServices.Marshal.ReleaseComObject(object o)释放,说的很不清楚,很多人试过觉的释放不掉。

事实上,的确是用该方法,但释放的技巧在于,新建几个AE对象就要逐步释放几个,例如:

IWorkspaceFactory Fact = new AccessWorkspaceFactoryClass ();

IFeatureWorkspace Workspace = Fact.Open(Propset,0) as IFeatureWorkspace;

  IFeatureClass Fcls = Workspace.OpenFeatureClass ("District");

IFeatureLayer Fly = new FeatureLayerClass();

……

IFeature pf

IField pfield

……

再对象运行结束时释放,如:

System.Runtime.InteropServices.Marshal.ReleaseComObject(Fact);

System.Runtime.InteropServices.Marshal.ReleaseComObject(Workspace);

System.Runtime.InteropServices.Marshal.ReleaseComObject(Fcls);

System.Runtime.InteropServices.Marshal.ReleaseComObject(Fly);

System.Runtime.InteropServices.Marshal.ReleaseComObject(pf);

System.Runtime.InteropServices.Marshal.ReleaseComObject(pfield);

看到吧,千万不要以为你释放了等级最高的 IWorkspaceFactory, IWorkspace就算完事了。没办法,AE有时就这么难以理解的麻烦。

 

 

 

ArcGIS Engine中正确释放打开资源