ASP.NET: Can't Kill EXCEL.exe with Office PIA

G

Guest

I have been trying to use the Office PIA's to write an ASP.NEt page to:
1. Open a template workbook
2. Populate some data
3. Save the file back to the server
4. Quit Excel and free up Memory

I have been able to do all of the above steps except #4. Each time
Excel.exe persists in memory. I have already referred to Q317109 "Office
application does not quit after automation from Visual Studio .NET client"
without much luck.

I've tried stripping down the Office code to see where this hang is coming
from exactly and found the following:

WORKS: The following DID release Excel.exe:
Dim xlApp As New Microsoft.Office.Interop.Excel.Application
xlApp.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)
xlApp = Nothing

ERROR: When I augment it to the following however, Excel.exe continues to
hang:
Dim xlApp As New Microsoft.Office.Interop.Excel.Application
Dim wkBook As Workbook = xlApp.Workbooks.Open("C:\test.xls")

xlApp.Workbooks.Close()
System.Runtime.InteropServices.Marshal.ReleaseComObject(wkBook)
wkBook = Nothing

xlApp.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)
xlApp = Nothing

If anyone could explain why this might be happening, it would be greatly
appreciated. It seems as if as soon as I get into working with a workbook,
Excel.exe is stuck in memory. Rather than a specific fix for the above, I'm
just looking to better understand the dynamics/rules here for avoiding this
problem.

Thanks in advance,

Chris Frohlich
 
B

bruce barker \(sqlwork.com\)

your problem is the line.

Dim wkBook As Workbook = xlApp.Workbooks.Open("C:\test.xls")

because .net needs wrapper to call com objects it really turns into

Dim wkBook As Workbook
dim tmp as WorkBooks
tmp = xlApp.Workbooks
wkBook = tmp.Open("C:\test.xls")

the tmp variable has a com ref that will not be released until its garabage
collected. the rule you should use is never use 2 dots with com objects.
alway create a tmp, and call ReleaseComObject() on the tmps.


-- bruce (sqlwork.com)
 
G

Guest

Besides thorough COM cleanup I still sometimes get stuck excel process so I
implemented this routine.

//kill EXCEL com processes
System.Diagnostics.Process [] PROC = Process.GetProcessesByName("EXCEL");
foreach(System.Diagnostics.Process PK in PROC)
{//User excel process always have window name
//COM process do not.
if(PK.MainWindowTitle.Length==0)
PK.Kill();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top