如何清理Excel.AppEvents AdviseSinks?
问题描述:
于是我就到使用时的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在函数调用,而不是结束了吗?
额外的阅读材料: