Solved

Excel hangs after closing application in asp.net

Posted on 2004-10-02
9
319 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

730 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