Solved

Excel hangs after closing application in asp.net

Posted on 2004-10-02
9
282 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

744 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

10 Experts available now in Live!

Get 1:1 Help Now