Should it be possible, to call .Dispose
for the same instance of IDisposable
multiple times without throwing an exception? Is there some definition for this?
For example:
public class Test : IDisposable
{
public void Dispose()
{
// What ever
}
}
var t = Test();
t.Dispose();
t.Dispose(); // Should this work in any case?
The reason why I'm asking this:
I've created some mechanism, which search in the VisualTree
for elements, which inherit from IDisposable
, but i can not be sure, that one of these elements gets disposed before.
Some code snipped from a window class
/// <summary>
/// Dispose
/// </summary>
public void DisposeAll()
{
foreach (var obj in WPFVisualTreeHelper.FindChildren<IDisposable>(this))
{
if (!disposables.Contains(obj))
{
disposables.Add(obj);
}
}
foreach (IDisposable disposable in disposables)
{
disposable.Dispose();
}
}
Thank you!