Solved

Excel hangs after closing application in asp.net

Posted on 2004-10-02
9
292 Views
Last Modified: 2006-11-17
This code works fine except for the fact that an instance of excel is still in the task manager on the server after the application is done.  I've searched everywhere and no code seems to end the process after the code has been run.  

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

        Dim xl As New Excel.Application()
        Dim wb As Excel.Workbook
        Dim sh As Excel.Worksheet

        Dim xlsPath As String = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath & "\documents\")
        Dim xlsTemplateFile As String = xlsPath & "Test2.xls"
        wb = xl.Workbooks.Open(xlsTemplateFile)
        sh = wb.Worksheets("sheet1")
        sh.Cells(3, 4) = "its a sample page"
        wb.SaveAs(xlsPath & "Test" & Format(Now(), "ddMMyyhhmmss") & ".xls")
        sh = Nothing
        wb.Close()
        System.Runtime.InteropServices.Marshal.ReleaseComObject(wb)
        wb = Nothing

       
        ' Close Excel.
       
        xl.Quit()
        System.Runtime.InteropServices.Marshal.ReleaseComObject(xl)


        xl = Nothing
       
        System.GC.Collect()
        System.GC.WaitForPendingFinalizers()
        System.GC.Collect()
        System.GC.WaitForPendingFinalizers()

    End Sub

Anyone know how I can get rid of the process on the server.

Thanks
0
Comment
Question by:OttawaMission
  • 4
  • 2
9 Comments
 
LVL 26

Expert Comment

by:Rejojohny
ID: 12211823
try this
....
        sh=nothing
        System.Runtime.InteropServices.Marshal.ReleaseComObject(sh)
        wb.Close()
        wb = nothing
        System.Runtime.InteropServices.Marshal.ReleaseComObject(wb)
        xl.Quit()
        xl = nothing
        System.Runtime.InteropServices.Marshal.ReleaseComObject(xl)
.....
0
 

Author Comment

by:OttawaMission
ID: 12212010
Rejojohny

I tried your code but I get the error 'Object reference not set to an instance of an object. ' on every System.Runtime.InteropServices.Marshal.ReleaseComObject() line.  Am I creating the objects wrong?  Any idea?

Thanks
0
 
LVL 26

Expert Comment

by:Rejojohny
ID: 12212175
oh . i am sorry .. just try adding all the " = nothing " statements after the 'releasecomobject" statements ...
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:OttawaMission
ID: 12212263
moving the  '=nothing' makes the code work but the excel.exe is still staying in the process after every run of the code.
0
 
LVL 26

Expert Comment

by:Rejojohny
ID: 12214822
doesn't it go off after some time ??? i.e. even if we r forcing garbage collector to clean the memory immediately, i have noticed that in the case of office applications, it still remains alive for some more time in some of the machines
0
 
LVL 7

Accepted Solution

by:
smolam earned 175 total points
ID: 12215613
I use this sub to clean up my Excel objects :

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

So when I am done with excel I put this at the end:

            oXL.Visible = False
            oXL.UserControl = False

            NAR(oSheet)
            oWB.Close(False)
            NAR(oWB)
            oWBs.Close()
            NAR(oWBs)

            oXL.Quit()

            NAR(oXL)

            'force final cleanup!
            GC.Collect()

This isn't perfect, but it works most times, I have done extensive research on this subject, and Microsoft admits that Excel Automation isn't really meant to be done with a Client front end ie from a server because Excel wasn't originally designed for this.  They have also noted that you mustn't reference methods of objects long hand, so for example if you had this code
                oSheet.Range("A1,A2").BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlMedium, Excel.XlColorIndex.xlColorIndexAutomatic)
You should change it to something like this

Dim oRng as Excel.Range
oRng = oSheet.Range("A1", oCell)
oRng.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlMedium, Excel.XlColorIndex.xlColorIndexAutomatic)

And then remember to clean up the oRng variable as well as the oSheet variable at the end.
0
 
LVL 26

Expert Comment

by:Rejojohny
ID: 12456345
please do have a look at my comments ...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

863 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now