[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Releasing Com Object/Closing File

Posted on 2006-10-25
2
Medium Priority
?
385 Views
Last Modified: 2009-07-29
Hello experts,

I have a bit of a problem with the code I am working on at the moment. I am attempting to write something that goes through a list of directories applying MST files to a blank MSI - then pulling out certain information from the MST. The problem is that when I move onto the next MSI it will not release the previous one although I have released all the objects. The code looks like the following:

***************************************************************

    Public Shared Function GetMSIDependency(ByVal strMST_Path As String, ByVal strReferenceMSIPath As String) As String()

        On Error Resume Next

        Dim strResults(1) As String
        File.Copy(strMST_Path, Application.StartupPath & "\dtcustom.mst", True) 'make a copy of the MST locally

        Dim objInstaller As Installer = CreateObject("Windowsinstaller.Installer") 'Crate installer object
        Dim objDatabase As Database = objInstaller.OpenDatabase(strReferenceMSIPath, 0) 'Open the reference database read only
        objDatabase.ApplyTransform(Application.StartupPath & "\dtcustom.mst", 63) 'Apply the transform - suppressing all errors

        'Create a view to return the PRODUCTNAMELONG property from property table
        Dim objDatabaseView As WindowsInstaller.View = objDatabase.OpenView("SELECT Property, Value FROM Property WHERE Property='PRODUCTNAMELONG'")
        objDatabaseView.Execute() 'Execute the view
        Dim objRecord As Record = objDatabaseView.Fetch 'Assign the view results to record
        strResults(0) = objRecord.StringData(2) 'Write the long name to the string
        objDatabaseView.Close() 'Kill the view
        Application.DoEvents() 'Catchup

        ' Create a view to return the DEPENDENCIES Property from the property table
        objDatabaseView = objDatabase.OpenView("SELECT Property, Value FROM Property WHERE Property='DEPENDENCIES'")
        objDatabaseView.Execute() ' execute the view
        objRecord = objDatabaseView.Fetch ' Assign the reulst to t a record
        strResults(1) = objRecord.StringData(2) 'Write the dependancy to the strings
        If Trim(strResults(1).ToUpper.StartsWith("ENTER CORE") = True) Then strResults(1) = "None" 'Make sure no description text gets through
        objDatabaseView.Close() 'Close the view
        Application.DoEvents() 'Catchup

        'Release Com Objects
        objRecord.ClearData()
        Marshal.ReleaseComObject(objRecord)
        Marshal.ReleaseComObject(objDatabaseView)
        Marshal.ReleaseComObject(objDatabase)
        Marshal.ReleaseComObject(objInstaller)
       GC.Collect()
Application.DoEvents()

        'Clear Variables
        objRecord = Nothing
        objDatabaseView = Nothing
        objDatabase = Nothing
        objInstaller = Nothing
 
        File.Delete(Application.StartupPath & "file.mst")

        Return strResults 'Return the string array containing MST information.

    End Function

End Class

********************************************************************************

Without the On error resume next the program will throw an exception when the file is deleted saying the file is already in use. Curiously, with the on error resume next on it will go through the MST's but fall over randomly at different point through the execution. The sub is called from another sub which is looping through a list iof MST paths.  ANy idea why the file won't delete?

Thanks for any help you can give.

Cheers,
Jamie.
0
Comment
Question by:purejamie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 8

Accepted Solution

by:
razo earned 2000 total points
ID: 17803461
i use this function for disposing an object
   Private Sub DisposeObject(ByRef obj As Object)
        'Wraps ReleaseCOMObject to provide a 'safe' disposal helper method.
        Dim count As Integer
        Try
            If obj Is Nothing Then
                Exit Try
            End If
            count = Marshal.ReleaseComObject(obj)
            While count > 0
                count = Marshal.ReleaseComObject(obj)
            End While
        Catch ex As SystemException
        Finally
            obj = Nothing
            GC.Collect()
            GC.WaitForPendingFinalizers()
        End Try
    End Sub
0
 

Author Comment

by:purejamie
ID: 17804170
Razo.

Many thanks for the post, seems to work very well! Please give me a day just to test it works throughout the program (I'm sure it will)

Thanks again,
Jamie.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question