ArcGIS Server笔记之ManageLifetime
ManageLifetime:从字面意思我们理解为“生命周期管理”。生命周期应该有一个范围,也就是一个作用域吧。在这个作用域范围生命存在,超出这个范围生命终止(个人理解)。
Use the ManageLifetime method to add your COM object to the set of objects that will be explicitly released when the WebObject is disposed.
当WebObject析构时,使用ManageLifetime方法管理的一系列创建的COM对象将被显示释放。
When you scope the use of WebObject within a using block, any object (including your cursor) that you have added to the WebObject using the ManageLifetime method will be explicitly released at the end of the using block.
当你使用Using语句块管理WebObject对象时,WebObject对象的ManageLifetime方法管理的任何COM对象在Using语句块结束后都将被显示释放。
2 {
3 using (WebObject webobj = new WebObject())
4 {
5 ServerConnection serverConn = new ServerConnection("doug", true);
6 IServerObjectManager som = serverConn.ServerObjectManager;
7
8 IServerContext ctx = som.CreateServerContext("Yellowstone","MapServer");
9 IMapServer mapsrv = ctx.ServerObject as IMapServer;
10 IMapServerObjects mapo = mapsrv as IMapServerObjects;
11 IMap map = mapo.get_Map(mapsrv.DefaultMapName);
12
13 IFeatureLayer flayer = map.get_Layer(0) as IFeatureLayer;
14 IFeatureClass fclass = flayer.FeatureClass;
15
16 IFeatureCursor fcursor = fclass.Search(null, true);
17 webobj.ManageLifetime(fcursor);
18
19 IFeature f = null;
20 while ((f = fcursor.NextFeature()) != null)
21 {
22 // do something with the feature
23 }
24
25 ctx.ReleaseContext();
26 }
27 }
备注 :
1、using 语句:
在 using 语句中创建一个实例,确保退出 using 语句时在对象上调用 Dispose。当到达 using 语句的末尾,或者如果在语句结束之前引发异常并且控制离开语句块,都可以退出 using 语句。
2、WebObject.ManageLifetime 方法 [C#]
public void ManageLifetime(object o);
参数:o Com对象
3、WebObject对象:
命名空间: ESRI.ArcGIS.Server.WebControls
程序集: ESRI.ArcGIS.Server.WebControls (in ESRI.ArcGIS.Server.WebControls.dll)