GC.Collect versus Marshal.ReleaseComObject and RCW

I have a reference to a COM object in my C # that sometimes I need to undo if you configured to Null and then recreated. It looks like there is some caching going on or something that could result in "the COM object being decoupled from its RCW type". I got around this by forcing GC.Collect immediately after setting the object to Null to discard RCW, but this heavy and inefficient approach is bad.

I wanted to use Marshal.ReleaseComObject, but that just decreases the reference count and the problem persists until the GC actually starts. (Not to mention, this is also a bad idea)

How can I ensure that when the COM link is restored, I get a new RCW and everything behaves as it should?


source to share

All Articles