Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

Excel object persisting

The Excel object that i created in my ASP.Net application is not get destroyed even after session is closed.In the code i have tried methods like .Quit,Marshal.ReleaseComObject etc to kill the object still it pesists.....Help

SK
Tuesday, August 17, 2004

Probably waiting for garbage collection. Why is this important? Does the object block any resources?

Chris Nahr
Tuesday, August 17, 2004

If you are done with the objects, you can force garbage collection and you shouldn't have any problems. We had to do this with a .NET component we use that calls into a COM object because until it was garbage collected, it would hold onto a connection. Since we sell our appliction by number of connections, customers were understandably upset by this, so in all of our top level calls, when we are done with the underlying objects, we force garbage collection to clean the connections up.

Here is a good overview on forcing garbage collection

http://www.developer.com/net/csharp/article.php/3343191

Mike
Friday, August 20, 2004

Tyr explicitly setting each level of the Excel object to nothing.  I found that (in windows programming) if I used a Worksheet object I had to make sure I did Worksheet = Nothing, Workbook = Nothing and Application = Nothing.  If you're declaring a worksheet without explicitly declaring a workbook try breaking out all that code and setting each to nothing.

zigzag
Tuesday, August 24, 2004

Like zig zag noted, you have to release all the explicitly and implicitly created objects. Here is a sample code..this is a slightly modified version of code sample from MS KB Article.

Cheers
Binu
------

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim oApp As New Excel.Application
        Dim oBooks As Excel.Workbooks = oApp.Workbooks
        Dim oBook As Excel.Workbook = oBooks.Add
        Dim oSheet As Excel.Worksheet = oApp.ActiveSheet
        Dim i As Integer

        ' Hide the automation object
        oApp.Visible = True
     
        ' Create some test data
        Dim loopy As Integer
        For loopy = 1 To 100
            Dim oRange As Excel.Range = oSheet.Range("B" & loopy)
            oRange.Value = "Monkeys " & loopy
            ReleaseExcelObject(oRange)
        Next

        ' Save the sheet
        'oBook.SaveAs("Test3.xls")
        ' Close the Workbook

        ReleaseExcelObject(oSheet)
        oBook.Close(False)
        ReleaseExcelObject(oBook)
        ReleaseExcelObject(oBooks)
        oApp.Quit()
        ReleaseExcelObject(oApp)

        Debug.WriteLine("Sleeping...")
        System.Threading.Thread.Sleep(5000)
        Debug.WriteLine("End Excel")

    End Sub

    Private Sub ReleaseExcelObject(ByVal o As Object)
        Try
            System.Runtime.InteropServices.Marshal.ReleaseComObject(o)
        Catch
        Finally
            o = Nothing
        End Try
    End Sub

Binu Thayamkery
Tuesday, September 07, 2004

Instead of
' Hide the automation object
        oApp.Visible = True
should be
' Hide the automation object
        oApp.Visible = False

xxx
Wednesday, September 08, 2004

*  Recent Topics

*  Fog Creek Home