MarkB42 MarkB42 - 1 year ago 135
C# Question

C# Excel 2007 RTD Server crashes on exit

I have written a c# RTD server based off of Kenny Ker's Multiple Topics in C#

The main difference between his design and mine is that my data comes from a WCF client. I use the same type of timer and every couple of seconds I call

. My
method calls a function in my WCF client with the topic values and uses the result as the value for excel. It all works excellent.

The problem comes when I close excel.

When I close Excel I get a message box that says "Microft Excel has stopped working"

method clears all of my topics, calls close on my WCF client and exits without error.

I thought the problem might be a COM issue so I have tried adding

while ( Marshal.ReleaseComObject( m_callback ) > 0 ) ;
m_callback = null;

The pop up still showed up so I tried adding

GC.WaitForPendingFinalizers(); //SEHException thrown from this

Adding these lines does throw an exception. if I ignore the exception excel closes without any problems, but if I install my RTD server on a computer with excel 2010, then the pop up box is still there.

I have Excel 2007 (12.0.6665.5003) SP3 MSO (12.0.6662.5000)
I am developing my c# RTD Server using Visual Studio 2008 and my project has a reference to Microsoft.Office.Interop.Excel version

Answer Source

The question was fairly vague, but that's because I really had no idea where to start. Every thing seemed to be working correctly.

After playing around with the code I noticed that I had an object that implemented IDisposable that I called Dispose on when I was done with it. The object also had a finalizer that called Dispose. I changed it to be more like this with a protected Dispose(bool).

I also removed the Excel assembly per Kenny Ker again (however just copying his code didn't work. I actually copied the interfaces straight from the excel interop assembly).

After doing that I was able to get rid of the lines I added (for marshalling the garbage collecting) above and excel now closes without a problem.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download