You won't experience any kind of deadlock situation when calling GC.Collect
and GC.WaitForPendingFinalizers
unless you are accessing managed objects within your Finalize
methods. Calling methods of other objects with a public scope could potentially lead to unpredictable results, including a deadlock. The reason is that you're not in total control of those other objects' locking patterns. It could be locked by anyone while your finalizer method is trying to access it.
Additionally, locking this
explicitly in the finalizer is almost guaranteed to cause a deadlock, as LukeH's answer demonstrates. You can read Jeffrey Richter's original article on that here.
In general, you should only be releasing unmanaged resources in your finalizers, which should alleviate any such concerns about deadlocks.