The GIS Professional Group

ManageLifetime を使用する方法

2016/9/1 (木)

ArcObjects のオブジェクトを開放するのに ManageLifetime を使用する方法

using ステートメントで dispose されるオブジェクトを管理

using ステートメント

public static void RecyclingInappropriateExample(IFeatureClass featureClass, Boolean
        enableRecycling)
{
        using(ComReleaser comReleaser = new ComReleaser())
        {
                // Create a search cursor.
                IFeatureCursor featureCursor = featureClass.Search(null, enableRecycling);
                comReleaser.ManageLifetime(featureCursor);

                // Get the first two geometries and see if they intersect.
                IFeature feature1 = featureCursor.NextFeature();
                IFeature feature2 = featureCursor.NextFeature();
                IRelationalOperator relationalOperator = (IRelationalOperator)feature1.Shape;
                Boolean geometriesEqual = relationalOperator.Equals(feature2.Shape);
                Console.WriteLine("Geometries are equal: {0}", geometriesEqual);
        }
}
Public Shared Sub RecyclingAppropriateExample(ByVal featureClass As IFeatureClass, ByVal enableRecycling As Boolean)
Using comReleaser As ComReleaser = New ComReleaser()
' Create a search cursor.
Dim featureCursor As IFeatureCursor = featureClass.Search(Nothing, enableRecycling)
comReleaser.ManageLifetime(featureCursor)

' Create a sum of each geometry's area.
Dim feature As IFeature = featureCursor.NextFeature()
Dim totalShapeArea As Double = 0
While Not feature Is Nothing
        Dim shapeArea As IArea = CType(feature.Shape, IArea)
        totalShapeArea + = shapeArea.Area
End While

Console.WriteLine("Total shape area: {0}", totalShapeArea)
End Using
End Sub
Using comReleaser As ESRI.ArcGIS.ADF.ComReleaser = New ESRI.ArcGIS.ADF.ComReleaser()

    Dim t As System.Type = System.Type.GetTypeFromProgID("esriDataSourcesFile.ShorkspaceFactory")
    Dim pWorkspaceFactory As ESRI.ArcGIS.Geodatabase.IWorkspaceFactory = CType(Stivator.CreateInstance(t), ESRI.ArcGIS.Geodatabase.IWorkspaceFactory)
    Dim pFeatureWorkspace As ESRI.ArcGIS.Geodatabase.IFeatureWorkspace = pWorkspry.OpenFromFile("D:\Workspace\", 0)

    Dim pTable As ITable = pFeatureWorkspace.OpenTable("New_dBASE_Table.dbf")

    comReleaser.ManageLifetime(pTable)
    comReleaser.ManageLifetime(pFeatureWorkspace)
    comReleaser.ManageLifetime(pWorkspaceFactory)

End Using
  • B!