Killing EXCEL.exe after using Microsoft Excel COM object in C#

by Brad 29. October 2007 11:53
I've been using the Microsoft Excel COM object in C# to allow me to open spreadsheets, process them and then upload to a web service, all worked nicely - or so i thought! After a while i noticed the PC running slowly - so i checked Task Manager and was horrified to see hundreds of EXCEL.exe processes running!

As the Excel Application object is a COM object there's no nice Dispose() method to call, and setting to null didn't help either... after some searching i found the answer:
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
//Do some stuff here...
excel.Quit();
while (Marshal.ReleaseComObject(excel) != 0) { }
excel = null;
GC.Collect();
GC.WaitForPendingFinalizers();

Some of the posts i found didn't have the last two lines which call the .NET garbage collector - and it didn't solve the problem until i added them. Also some people found they had to call Marshal.ReleaseComObject() on any Workbook or Worksheet objects they'd used (in my case i didn't need to).

Tags:

C#

Comments

11/23/2007 3:12:20 PM #

Great!!!

I noticed a similar problem when:

The com using program is running and you close excel inbetween. Then an excel-zombie-process is established.

I am using VB.net and with your snippet I now can kill the COM-channels after interacting with excel.

If you close excel after that cleanup, no zombies around, even your program is still running.

Thx for the hint!

Peter Danielczyk |

3/5/2008 4:45:38 PM #

Thanks, this is helpful

Dean |

4/1/2008 6:50:32 AM #

Very Good

PK |

8/26/2008 5:21:08 AM #

Thanks a lot, it's helpful for me also. I was looking for this.
Once again thanks a lot

Deepak |

8/29/2008 5:04:49 PM #

Dont forget using System.Runtime.InteropServices;

for the marshalling

Andy |

9/17/2008 5:30:19 PM #

erg, doesn't bloody work for me - very frustrating problem :/

Dustin |

11/4/2008 9:56:01 PM #

Didn't work for me either...

what are you doing in between the while brackets.  As per your code above you do nothing.

while (Marshal.ReleaseComObject(excel) != 0) { }

Dave |

12/10/2008 1:44:18 AM #

If you havent already, try releasing all com objects individually and even set to null as a precaution:

System.Runtime.InteropServices.Marshal.ReleaseComObject(xlsWorkbook);
xlsWorkbook = null;

System.Runtime.InteropServices.Marshal.ReleaseComObject(xlsRange);
xlsRange = null;

System.Runtime.InteropServices.Marshal.ReleaseComObject(xlsApp);
xlsApp = null;

GC.Collect();
GC.WaitForPendingFinalizers();

This was the only way I could get Excel to fully close without ending the app

D |

12/31/2008 4:19:13 PM #

maybe you can help with that :
i have this object in my c# program
       Application excelFile ;
        Workbook theWorkbook;
        Sheets sheets ;
        Worksheet worksheet;
i create them in the object constructor and try to kill them in the destructor
i follow your instructions one by one but the proccess refuse to dieuntil the i close the form i want it dead when i leaving the destructor
thanks
Eran
i use your instractions one by one but

eran |

Powered by BlogEngine.NET 1.5.0.7
Theme by Interakting

Interakting

A full service digital agency offering online strategy, design and usability, systems integration and online marketing services that deliver real business benefits and ensure your online objectives are met.

Calendar

<<  July 2010  >>
MoTuWeThFrSaSu
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

View posts in large calendar