![]() |
Excel remains running due to held references
We are building a COM addin using C# and .NET 1.1, targeting Office XP
and Office 2003. We are not using VSTO. The purpose of the COM addin is to do some custom work when certain XLS-documents are loaded (such as showing a custom toolbar, etc) The COM addin works fine when Excel is running stand-alone. But when Excel is running inside Internet Explorer, Excel remains running after IE is closed. I guess this is due to the fact that we hold references to Excel from within the addin. The problem is that I don't understand how I can avoid this, if the addin should work as expected. There's a catch-22 he In order for Excel to unload when IE closes, all references to it must be released. However, for my addin to release the references, IDTExtensibility2.OnDisconnection must be called, which doesn't happen until Excel closes. Any ideas: Here's the pattern we use (simplified): public void OnConnection(object application, ...) // Implementation of IDTExtensibility2 { applicationObject = (Excel.Application)application; // Save the app-object for later } public void OnStartupComplete(ref System.Array custom) { applicationObject.WorkbookOpen += new Microsoft.Office.Interop.Excel.AppEvents_WorkbookO penEventHandler(applicationObject_WorkbookOpen); } public void OnDisconnection(...) { applicationObject = null; GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); } public void OnBeginShutdown(ref System.Array custom) { TearDownUI(); GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); } private void applicationObject_WorkbookOpen(Microsoft.Office.In terop.Excel.Workbook wb) { SetUPUI(); } Thanks! /Fredrik |
Excel remains running due to held references
Identify the Excel instance that you are using and then use the Windows api
to kill it. This is the only reliable way to kill Excel. You can wait for GC and so on, but Excel is like the cat that came back. You have to use sheer brute force to be absolutely sure that it's dead. -- www.alignment-systems.com "Fredrik" wrote: We are building a COM addin using C# and .NET 1.1, targeting Office XP and Office 2003. We are not using VSTO. The purpose of the COM addin is to do some custom work when certain XLS-documents are loaded (such as showing a custom toolbar, etc) The COM addin works fine when Excel is running stand-alone. But when Excel is running inside Internet Explorer, Excel remains running after IE is closed. I guess this is due to the fact that we hold references to Excel from within the addin. The problem is that I don't understand how I can avoid this, if the addin should work as expected. There's a catch-22 he In order for Excel to unload when IE closes, all references to it must be released. However, for my addin to release the references, IDTExtensibility2.OnDisconnection must be called, which doesn't happen until Excel closes. Any ideas: Here's the pattern we use (simplified): public void OnConnection(object application, ...) // Implementation of IDTExtensibility2 { applicationObject = (Excel.Application)application; // Save the app-object for later } public void OnStartupComplete(ref System.Array custom) { applicationObject.WorkbookOpen += new Microsoft.Office.Interop.Excel.AppEvents_WorkbookO penEventHandler(applicationObject_WorkbookOpen); } public void OnDisconnection(...) { applicationObject = null; GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); } public void OnBeginShutdown(ref System.Array custom) { TearDownUI(); GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); } private void applicationObject_WorkbookOpen(Microsoft.Office.In terop.Excel.Workbook wb) { SetUPUI(); } Thanks! /Fredrik |
Excel remains running due to held references
Fredrik,
You may check out the following tutorial (and especially the last post) about releasing an instance of Excel. It's explicit target VB.NET but it shouldn't be an issue for You to port it to C#. http://www.xtremevbtalk.com/showthread.php?t=160433 --------------- With kind regards, Dennis Weekly Blog .NET & Excel: http://xldennis.wordpress.com/ My English site: http://www.excelkb.com/default.aspx My Swedish site: http://www.xldennis.com/ |
Excel remains running due to held references
The problem is that the plugin won't be invoked in order to kill Excel.
If Excel opens embedded within IE, and the user closes IE, my plugin will hold at least one reference to Excel, thus leaving Excel running. There is no call to my plugin where it would be appropriate to kill Excel. To me, it looks like a general problem - if a plugin needs to hook up events, Excel won't close "automatically", since the plugin will always hold a reference to Excel. But this would mean a serious drawback for plugins, so I would be surprised if there's no way around it. Any ideas? /Fredrik John.Greenan wrote: Identify the Excel instance that you are using and then use the Windows api to kill it. This is the only reliable way to kill Excel. You can wait for GC and so on, but Excel is like the cat that came back. You have to use sheer brute force to be absolutely sure that it's dead. -- www.alignment-systems.com "Fredrik" wrote: We are building a COM addin using C# and .NET 1.1, targeting Office XP and Office 2003. We are not using VSTO. The purpose of the COM addin is to do some custom work when certain XLS-documents are loaded (such as showing a custom toolbar, etc) The COM addin works fine when Excel is running stand-alone. But when Excel is running inside Internet Explorer, Excel remains running after IE is closed. I guess this is due to the fact that we hold references to Excel from within the addin. The problem is that I don't understand how I can avoid this, if the addin should work as expected. There's a catch-22 he In order for Excel to unload when IE closes, all references to it must be released. However, for my addin to release the references, IDTExtensibility2.OnDisconnection must be called, which doesn't happen until Excel closes. Any ideas: Here's the pattern we use (simplified): public void OnConnection(object application, ...) // Implementation of IDTExtensibility2 { applicationObject = (Excel.Application)application; // Save the app-object for later } public void OnStartupComplete(ref System.Array custom) { applicationObject.WorkbookOpen += new Microsoft.Office.Interop.Excel.AppEvents_WorkbookO penEventHandler(applicationObject_WorkbookOpen); } public void OnDisconnection(...) { applicationObject = null; GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); } public void OnBeginShutdown(ref System.Array custom) { TearDownUI(); GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); } private void applicationObject_WorkbookOpen(Microsoft.Office.In terop.Excel.Workbook wb) { SetUPUI(); } Thanks! /Fredrik |
All times are GMT +1. The time now is 01:21 AM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com