Hi
When i use following code:
myManualResetEvent.Dispose();
Compiler gives this error:
'System.Threading.WaitHandle.Dispose(bool)' is inaccessible due to its protection level.
howevr following line works fine:
((IDisposable)myManualResetEvent).Dispose();
is it the correct way to dispose or at runtime it might crash in some scenerios.
Thanks.
The designers of the .NET Base Class Library decided to implement the
Disposemethod using explicit interface implementation:The
Disposemethod is private and the only way to call it is to cast the object toIDisposableas you have discovered.The reason this is done is to customize the name of the
Disposemethod into something that better describes how the object is disposed. For aManualResetEventthe customized method is theClosemethod.To dispose a
ManualResetEventyou have two good options. UsingIDisposable:or calling
Close:You can read more in the section Customizing a Dispose Method Name in the design guideline Implementing Finalize and Dispose to Clean Up Unmanaged Resources on MSDN: