Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Add the following before your call to xl.Quit():</p> <pre><code>GC.Collect(); GC.WaitForPendingFinalizers(); </code></pre> <p>You can also use <code>Marshal.FinalReleaseComObject()</code> in your NAR method instead of ReleaseComObject. <a href="http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.releasecomobject.aspx" rel="nofollow noreferrer">ReleaseComObject</a> decrements the reference count by 1 while <a href="http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.finalreleasecomobject.aspx" rel="nofollow noreferrer">FinalReleaseComObject</a> releases all references so the count is 0.</p> <p>So your finally block would look like:</p> <pre><code>finally { GC.Collect(); GC.WaitForPendingFinalizers(); NAR(wSheet); if (wBook != null) wBook.Close(false, m_objOpt, m_objOpt); NAR(wBook); xl.Quit(); NAR(xl); } </code></pre> <p>Updated NAR method:</p> <pre><code>private void NAR(object o) { try { System.Runtime.InteropServices.Marshal.FinalReleaseComObject(o); } catch { } finally { o = null; } } </code></pre> <p>I had researched this awhile ago and in examples I found usually the GC related calls were at the end after closing the app. However, there's an MVP (Mike Rosenblum) that mentions that it ought to be called in the beginning. I've tried both ways and they've worked. I also tried it without the WaitForPendingFinalizers and it worked although it shouldn't hurt anything. YMMV.</p> <p>Here are the relevant links by the MVP I mentioned (they're in VB but it's not that different):</p> <ul> <li><a href="http://www.xtremevbtalk.com/showthread.php?p=1157109#post1157109" rel="nofollow noreferrer">http://www.xtremevbtalk.com/showthread.php?p=1157109#post1157109</a></li> <li><a href="http://www.xtremevbtalk.com/showthread.php?s=bcdea222412c5cbfa7f02cfaf8f7b33f&amp;p=1156479#post1156479" rel="nofollow noreferrer">http://www.xtremevbtalk.com/showthread.php?s=bcdea222412c5cbfa7f02cfaf8f7b33f&amp;p=1156479#post1156479</a></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload