I'm trying to handle when my app opens Excel and the user closes it instead
of my app. Excel will remain in memory until a reboot. To demonstrate,
create a CSharp WinForms app with one button. Here is the button click
code:
private void button1_Click(object sender, System.EventArgs e)
{
// create the Excel app and show it to the user
Excel.Application app = new Excel.ApplicationClass();
app.Visible = true;
// When this pops up, close Excel
MessageBox.Show("Excel should be visible");
// no error here but excel will never be released from Memory
app.Quit();
app = null;
}
As a note, I have tried setting the objects to null, using
System.Runtime.Interop.Marshalling.ReleaseCOMObject and nothing seems to
work.
Tim Anderson - 15 Apr 2004 19:23 GMT
> I'm trying to handle when my app opens Excel and the user closes it instead
> of my app. Excel will remain in memory until a reboot. To demonstrate,
[quoted text clipped - 3 lines]
> System.Runtime.Interop.Marshalling.ReleaseCOMObject and nothing seems to
> work.
In desperation, there is always process.Kill.
Tim
Paul Clement - 15 Apr 2004 19:58 GMT
¤ I'm trying to handle when my app opens Excel and the user closes it instead
¤ of my app. Excel will remain in memory until a reboot. To demonstrate,
¤ create a CSharp WinForms app with one button. Here is the button click
¤ code:
¤
¤ private void button1_Click(object sender, System.EventArgs e)
¤ {
¤ // create the Excel app and show it to the user
¤ Excel.Application app = new Excel.ApplicationClass();
¤ app.Visible = true;
¤
¤ // When this pops up, close Excel
¤ MessageBox.Show("Excel should be visible");
¤
¤ // no error here but excel will never be released from Memory
¤ app.Quit();
¤ app = null;
¤ }
¤
¤
¤
¤ As a note, I have tried setting the objects to null, using
¤ System.Runtime.Interop.Marshalling.ReleaseCOMObject and nothing seems to
¤ work.
Are you creating any (implicit) objects (such as a new Worksheet) that are not set to an object
variable which can be subsequently destroyed?
Paul ~~~ pclement@ameritech.net
Microsoft MVP (Visual Basic)
Santhosh Pillai [MS] - 15 Apr 2004 20:27 GMT
Try calling
GC.Collect();
GC.WaitForPendingFinalizers ();
After you call app.Quit().
Thanks,
Santhosh
> I'm trying to handle when my app opens Excel and the user closes it instead
> of my app. Excel will remain in memory until a reboot. To demonstrate,
[quoted text clipped - 18 lines]
> System.Runtime.Interop.Marshalling.ReleaseCOMObject and nothing seems to
> work.
Aaron Prohaska - 15 Apr 2004 22:40 GMT
> I'm trying to handle when my app opens Excel and the user closes it instead
> of my app. Excel will remain in memory until a reboot. To demonstrate,
[quoted text clipped - 18 lines]
> System.Runtime.Interop.Marshalling.ReleaseCOMObject and nothing seems to
> work.
I'm also having a similar problem that I posted here yesterday. I have
not had a single reply to it, but the code that I posted may work better
in your situation. Look for the post with the subject "Excel automation
problem." dated 4/14 10:38am. I think this code is not working for me
because I'm trying to leave the application open for the user to
continue making changes to the excel file.
Regards,
Aaron Prohaska
-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-
Wrench Science Inc.
http://www.wrenchScience.com/
Phone: 510.841.4748 x206
Fax: 510.841.4708
-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-
DotNetJunkies User - 11 May 2004 06:37 GMT
try this:
workbook.Close(false, 0, 0);
excelApp.Quit();
//System.Runtime.Interop.Marshalling.ReleaseCOMObject(excelApp); -- no use
//GC.Collect(); -- no use
//GC.WaitForPendingFinalizers(); -- no use
// this will help
FormWindowState oldState = this.WindowState;
this.WindowState = FormWindowState.Minimized;
this.WindowState = oldState;
hoho
---
Hendrix4578 - 14 Jun 2004 03:30 GMT
We are having the same problem. This issue needs to be addressed by MS because it is an obvious flaw with their technology. This code works on one box but not on another - same OS etc? The funny thing is that if you code it up using VB.Net
you do not have the problem at all - C# seems not to be able to handle it. It is not possible for me to use Process.Kill because the users of the ASP.Net app do not have Admin rights on the domain.
finally
{
if(ws != null)
{
while(System.Runtime.InteropServices.Marshal.ReleaseComObject(this.ws) > 0)
System.Runtime.InteropServices.Marshal.ReleaseComObject(this.ws);
this.ws = null;
}
if(wb != null)
{
for(int i = 1 ;i < wb.Sheets.Count; i++)
((Excel.Worksheet)wb.Sheets[i]).Delete();
excelApp.ActiveWorkbook.Close(null,null,null);
while(System.Runtime.InteropServices.Marshal.ReleaseComObject(this.wb) > 0)
System.Runtime.InteropServices.Marshal.ReleaseComObject(this.wb);
this.wb = null;
}
if(excelApp != null)
{
excelApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(this.excelApp);
this.excelApp = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
> I'm trying to handle when my app opens Excel and the user closes it instead
> of my app. Excel will remain in memory until a reboot. To demonstrate,
[quoted text clipped - 18 lines]
> System.Runtime.Interop.Marshalling.ReleaseCOMObject and nothing seems to
> work.