If you are not sure whether the object was deleted or not, you should call the Dispose method itself, not methods like Close . Although the structure does not guarantee that the Dispose method must be executed without exception, even if the object was previously deleted, it is a common template and, as far as I know, is implemented on all disposable objects within the framework.
Typical template for Dispose , according to Microsoft :
public void Dispose() { Dispose(true); // Use SupressFinalize in case a subclass // of this type implements a finalizer. GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { // If you need thread safety, use a lock around these // operations, as well as in your methods that use the resource. if (!_disposed) { if (disposing) { if (_resource != null) _resource.Dispose(); Console.WriteLine("Object disposed."); } // Indicate that the instance has been disposed. _resource = null; _disposed = true; } }
Check out the _disposed check. If you called the Dispose method that implements this template, you could call Dispose as many times as you like without being thrown into exceptions.
Ryan Brunner Aug 11 '10 at 10:55
source share