1

I'm trying to automate one of task by creating code that will execute the IDatabaseCompact.Compact method to a GDB file. Everything seems to work fine but there is always a .lock file generated after the Compact method is called. I am planning to do something else after the Compact process but it errors out because of the lock file.

Is there any way to close/delete the lock file?

BTW, this is how my process works:

first, I bind and initialize my system using: ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.EngineOrDesktop) _ao = New ESRI.ArcGIS.esriSystem.AoInitialize() _ao.Initialize(ESRI.ArcGIS.esriSystem.esriLicenseProductCode.esriLicenseProductCodeBasic)

I then call my compact method:

Public Sub CompressFileGDB(gdbFilePath As String)
    Dim dbCompact As IDatabaseCompact
    Dim ws As IWorkspace = FileGdbWorkspaceFromPath(gdbFilePath)

    If (TypeOf ws Is IDatabaseCompact) Then
        dbCompact = CType(ws, IDatabaseCompact)
        If (dbCompact.CanCompact) Then
            Dim didFinish As Boolean = True
            Try
                dbCompact.Compact()
            Catch ex As Exception
                didFinish = False
            Finally
                dbCompact = Nothing
                ws = Nothing
                _ao.Shutdown()

                RaiseEvent OnProcessDone(didFinish)
            End Try
        End If
    End If
End Sub

The compact method works, there is a significant change in the GDB file size. I also tried to set my attributes to Nothing after the process but it doesn't seem to help that much.

1

After doing more research i was able to fix the issue by allowing ComReleaser to manage the lifetime of my IDatabaseCompact object.

For ArcObjects 10.1, the ComReleaser is in the ESRI.ArcGIS.ADF.Local.dll

This is the Resulting code:

    Using comReleaser As ComReleaser = New ComReleaser()
        Dim dbCompact As IDatabaseCompact
        Dim ws As IWorkspace = FileGdbWorkspaceFromPath(gdbFilePath)

        If (TypeOf ws Is IDatabaseCompact) Then
            dbCompact = CType(ws, IDatabaseCompact)
            comReleaser.ManageLifetime(dbCompact)

            If (dbCompact.CanCompact) Then
                Try
                    dbCompact.Compact()
                Catch ex As Exception
                    didFinish = False
                Finally
                    dbCompact = Nothing
                    ws = Nothing

                End Try
            End If
        End If
    End Using

Hope this is helpful.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.