?
Solved

Excel hangs after closing application in asp.net

Posted on 2004-10-02
9
Medium Priority
?
353 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
7 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 525 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

850 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