The problem with freezing COM objects

I have an application that uses COM interoperability to create a spreadsheet that opens in Excel on a client machine. However, it seems that the EXCEL.exe process does not always end when the user closes Excel, if I look at Task Manager.

If I saved the workbook and programmatically closed Excel, I would just use Marshal.ReleaseComObject() to clear it, but since I am dependent on manually closing the program, I am not sure what to do. Any suggestions?

+7
source share
4 answers

Excel cannot exit until all its objects outside the process are released. Thus, it just hides its user interface and continues to work. Until your program terminates or removes all references to Excel objects and the finalizer stream. Exiting your program will be the obvious decision. Or you can make the finalizer thread work with:

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

ReleaseComObject () rarely works because it is so easy to forget the intermediate link of a COM object. Like WorkBooks [index], you do not see this counter. Providing GK with such a solution would be a more reasonable choice for yourself, if you continue to work and do the work, then this will happen.

+9
source

Perhaps the Excel.exe file is still open because the user opened another document in the same instance of the Excell application. In this case, you probably should not kill the excel.exe process.

If you make sure that you have closed all open documents and freed all the objects you created, then why is it important for you to ensure that Excel.exe shuts down? Maybe the process serves another application or user?

+3
source

There can be many reasons (including Ran's), but I see a lot with MapPoint (another Microsoft COM application), so you have to make sure ALL object references are cleared so that the garbage collector can clear them. One low object hanging around is enough to stop the application from closing and exiting the process list.

+2
source

If I open the Excel application and workbook with:

 Excel.Application app = new Excel.Application(); Excel.Workbook wb = app.Workbooks.Open(...); 

Then I use the wb.Close() , app.Quit() methods in the last declaration (or other appropriate closing event). This cleans the exel.exe process. No need to call the Marshall.ReleaseComObject or GC methods.

+2
source

All Articles