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();
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).
cafdc5b0-f203-46fe-b29b-c9a531e06609|0|.0
Tags:
C#