Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   ASP .Net (http://www.velocityreviews.com/forums/f29-asp-net.html)
-   -   ASP.NET: Can't Kill EXCEL.exe with Office PIA (http://www.velocityreviews.com/forums/t123703-asp-net-cant-kill-excel-exe-with-office-pia.html)

=?Utf-8?B?Q2hyaXNGcm9obGljaA==?= 04-13-2006 11:39 PM

ASP.NET: Can't Kill EXCEL.exe with Office PIA
 
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.ReleaseComO bject(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.ReleaseComO bject(wkBook)
wkBook = Nothing

xlApp.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComO bject(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


bruce barker \(sqlwork.com\) 04-14-2006 04:20 PM

Re: ASP.NET: Can't Kill EXCEL.exe with Office PIA
 
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)




"ChrisFrohlich" <ChrisFrohlich@discussions.microsoft.com> wrote in message
news:1BFA6DA2-1FBD-401D-8325-DA8840307C3A@microsoft.com...
>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.ReleaseComO bject(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.ReleaseComO bject(wkBook)
> wkBook = Nothing
>
> xlApp.Quit()
> System.Runtime.InteropServices.Marshal.ReleaseComO bject(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
>




=?Utf-8?B?S2VpdGg=?= 04-25-2006 07:07 PM

Re: ASP.NET: Can't Kill EXCEL.exe with Office PIA
 
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();
}




"bruce barker (sqlwork.com)" wrote:

> 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)
>
>
>
>
> "ChrisFrohlich" <ChrisFrohlich@discussions.microsoft.com> wrote in message
> news:1BFA6DA2-1FBD-401D-8325-DA8840307C3A@microsoft.com...
> >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.ReleaseComO bject(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.ReleaseComO bject(wkBook)
> > wkBook = Nothing
> >
> > xlApp.Quit()
> > System.Runtime.InteropServices.Marshal.ReleaseComO bject(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
> >

>
>
>



All times are GMT. The time now is 01:56 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.