Solved

Excel hangs after closing application in asp.net

Posted on 2004-10-02
9
304 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 AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

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…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.

773 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