Excel remains running due to held references

F

Fredrik

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 here: 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_WorkbookOpenEventHandler(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.Interop.Excel.Workbook
wb)
{
SetUPUI();
}


Thanks!
/Fredrik
 
G

Guest

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.
 
F

Fredrik

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top