Custom Properties lock problem

 

Hi

A have problem with writing Custom properties with SolidEdgeFileProperties.

I can read, write and also delete all properties, but where I Close

PropertySets.Close()

file is still in lock state.

Can any help

My problem function

Public Function WriteCustomProperty(ByVal FileNameWithPath As String) As Boolean

    Dim DocName As String = "", DocPath As String = ""

    Dim myList As List(Of Dialogs.SovaDialogCustomProperties)

    Dim DocPropertySets As New SolidEdgeFileProperties.PropertySets

    Dim DocProperties As SolidEdgeFileProperties.Properties

    DocPropertySets.Open(FileNameWithPath)

    DocProperties = DocPropertySets.Item("Custom")

    DocName = ObjSE.FilePart(ObjSE.FilePart(FileNameWithPath, SovaFileParts.FileNameWithoutPath), SovaFileParts.File_Name)

    DocPath = ObjSE.FilePart(FileNameWithPath, SovaFileParts.Path)

    Dim PropertyKey As String = "", PropertyValue As String = ""

    myList = ObjSE.GlobalListOfCustomProperties(DocName, DocPath)

    For Each DocProperty In DocProperties

      If UCase(Left(DocProperty.Name, 4)) = "SPM_" Then

        Try

          DocProperty.Delete()

        Catch ex As Exception

          MsgBox(ex.Message)

        End Try

      End If

    Next

    For i As Integer = 1 To myList.Count

      Try

        DocProperties.Add(myList.Item(i - 1).PropertyName, myList.Item(i - 1).PropertyValue)

      Catch ex As Exception

        MsgBox(ex.Message)

      End Try

 

    Next

    Try

      DocProperties.Save()

    Catch ex As Exception

      MsgBox(ex.Message)

    End Try

    Try

      DocPropertySets.Close()

    Catch ex As Exception

      MsgBox(ex.Message)

    End Try

 

    PropertyKey = Nothing

    PropertyValue = Nothing

    DocName = Nothing

    DocPath = Nothing

    DocProperties = Nothing

    DocPropertySets = Nothing

    myList = Nothing

    Try

      GC.Collect()

    Catch ex As Exception

      MsgBox(ex.Message)

    End Try

    Return True

  End Function

 

 

Posted by: lu on
Post date: 9/26/2010 10:32:23 AM

3 REPLIES

RE: Custom Properties lock problem

You've got a object somewhere that is still referencing something in the API. Without looking at the whole program, there is really no way for us to know what's going on. Point is, you've got to free up every object you touch.

 

Posted by: Jason Newell
Post date: 9/26/2010 4:57:02 PM

RE: Custom Properties lock problem

 

Hello Luon,

 

Try this:

 

Imports System.Runtime.InteropServices

....

....

....

....

PropertyKey = Nothing

PropertyValue = Nothing

DocName = Nothing

DocPath = Nothing

myList = Nothing

 

If Not (DocProperties Is Nothing) Then 

   Marshal.ReleaseComObject(DocProperties)

   DocProperties = Nothing

End If

If Not (DocPropertySets Is Nothing) Then

   Marshal.ReleaseComObject(DocPropertySets)

   DocPropertySets = Nothing

End If

 

GC.Collect()

GC.WaitForPendingFinalizers()

GC.Collect()

GC.WaitForPendingFinalizers()

....

 

See http://www.jasonnewell.net/Forums/tabid/55/forumid/1/tpage/1/view/topic/postid/4240/Default.aspx#424...

 

The GC needs to be called twice in order to get the finalizers called -

the first time: it simply makes a list of what is to be finalized,

the second time: it actually the finalizing.

Only then will the object do it automatic ReleaseComObject.

 

I hope, this help

 

Peter

 

Posted by: Peter Caderas
Post date: 9/26/2010 8:33:56 PM

RE: Custom Properties lock problem

 

Hi All,

Funny result

There was no change with

GC.Collect()

GC.WaitForPendingFinalizers()

GC.Collect()

GC.WaitForPendingFinalizers()

The problem section was

    For Each DocProperty In DocProperties

      If UCase(Left(DocProperty.Name, 4)) = "SPM_" Then

        Try

          DocProperty.Delete()

        Catch ex As Exception

          MsgBox(ex.Message)

        End Try

      End If

    Next

Even there was no property to delete file remained locked. (I think the problem was .Name property )

working function

  Public Function WriteCustomProperty(ByVal FileNameWithPath As String) As Boolean

    Dim DocName As String = "", DocPath As String = ""

    Dim myList As List(Of Dialogs.SovaDialogCustomProperties)

    Dim DocPropertySets As New SolidEdgeFileProperties.PropertySets

    Dim DocProperties As SolidEdgeFileProperties.Properties

    DocPropertySets.Open(FileNameWithPath)

    DocProperties = DocPropertySets.Item("Custom")

    DocName = ObjSE.FilePart(ObjSE.FilePart(FileNameWithPath, SovaFileParts.FileNameWithoutPath), SovaFileParts.File_Name)

    DocPath = ObjSE.FilePart(FileNameWithPath, SovaFileParts.Path)

    Dim PropertyKey As String = "", PropertyValue As String = ""

    myList = ObjSE.GlobalListOfCustomProperties(DocName, DocPath)

 

    For i As Integer = 1 To myList.Count

      Try

        DocProperties.Add(myList.Item(i - 1).PropertyName, myList.Item(i - 1).PropertyValue)

      Catch ex As Exception

        MsgBox(ex.Message)

      End Try

 

    Next

    Try

      DocProperties.Save()

    Catch ex As Exception

      MsgBox(ex.Message)

    End Try

    Try

      DocPropertySets.Close()

    Catch ex As Exception

      MsgBox(ex.Message)

    End Try

 

    PropertyKey = Nothing

    PropertyValue = Nothing

    DocName = Nothing

    DocPath = Nothing

    myList = Nothing

    If Not (DocProperties Is Nothing) Then

      Marshal.ReleaseComObject(DocProperties)

      DocProperties = Nothing

    End If

    If Not (DocPropertySets Is Nothing) Then

      Marshal.ReleaseComObject(DocPropertySets)

      DocPropertySets = Nothing

    End If

 

    Try

      GC.Collect()

      GC.WaitForPendingFinalizers()

      GC.Collect()

      GC.WaitForPendingFinalizers()

 

    Catch ex As Exception

      MsgBox(ex.Message)

    End Try

    Return True

  End Function

Funny is the method .Add also update property if this property exist

 

 

Posted by: lu on
Post date: 9/26/2010 10:04:19 PM