HOW TO:避免“将COM对象与其基础RCW分开后不能再使用该对象”错误

Author:水如烟  

出现这个情形,是在重写了Finalize并在里面含有调用Com对象的代码,同时在销毁对象前没有显式释放Com对象。

解决的办法是,显式调用释放代码。

如:

Public Sub Quit()
 ...
End Sub

Protected Overridable Sub Dispose(ByVal disposing As Boolean)
    If Not Me.disposedValue Then
        If disposing Then

            If gApplicationComObject IsNot Nothing Then
                Me.Quit()
            End If

        End If

    End If
    Me.disposedValue = True
End Sub

Public Sub Dispose() Implements IDisposable.Dispose
    Dispose(True)
    GC.SuppressFinalize(Me)
End Sub

使用时:

一是

MyCom.Quit()

或是

Using MyCom

   ...

End Using

不知我的认识是否正确,仅供参考.

你可能感兴趣的:(HOW TO:避免“将COM对象与其基础RCW分开后不能再使用该对象”错误)