Alex F
Alex F

Reputation: 43321

IDisposable member of WPF Window class

When I add IDisposable class member to Windows Forms Form class, I add disposing code to Form's Dispose method. What should I do when I add IDisposable class member to WPF Window class, which is not IDisposable?

Upvotes: 2

Views: 5407

Answers (3)

decyclone
decyclone

Reputation: 30840

Approaches you can use:

  • Use Closed event on Window.
  • Implement IDisposable interface yourself for this Window.

Upvotes: 1

Jon Mitchell
Jon Mitchell

Reputation: 3429

You could implement the IDisposable pattern that hooks into the classes finalizer. This means that your IDisposable member would always get cleared up. The only problem is that you wouldn't know when as it depends on the GC to collect the Window class.

Alternatively you could add an event handler the the Window.Closed event and do your disposing there.

Upvotes: 0

John Källén
John Källén

Reputation: 7953

Extend your window class so that it has IDisposable, then implement the Dispose() method as before:

public class MyWindow : Window, IDisposable
{
    public void Dispose()
    {
        // Dispose your objects here as before.
    }
}

Upvotes: 6

Related Questions