0
於是我就到使用時的OLE嵌入情況下的WorkbookOpen事件的一個問題:它是一個有據可查的問題:如何清理Excel.AppEvents AdviseSinks?
- https://blogs.msdn.microsoft.com/mshneer/2008/10/18/com-interop-handling-events-has-side-effects/
- https://blogs.msdn.microsoft.com/rcook/2008/10/30/iconnectionpoint-and-net-or-how-i-learned-to-stop-worrying-and-love-managed-event-sinks-part-1/
我不僅需要一個IConnectionSink,而且在每個作爲ap傳遞的工作簿對象上調用Marshal.ReleaseComObject arameter。
public class ExcelAppEventSink : Excel.AppEvents
{
... other functions ...
public void WorkbookOpen(Excel.Workbook Wb)
{
<-- operate on workbook -->
Marshal.ReleaseComObject(Wb);
}
... other functions ...
}
但是從微軟發佈的另一篇博客文章發佈2年後聲稱Marshal.ReleaseComObject是危險的。 https://blogs.msdn.microsoft.com/visualstudio/2010/03/01/marshal-releasecomobject-considered-dangerous/
,因爲我只需要鬆開RCWs這將是在2016年安全調用 CleanupUnusedObjectsInCurrentContext在函數調用,而不是結束了嗎?
額外的閱讀材料: