2

I have an MVVM Project and need to run some methods when th VM is destroyed. I'm trying this:

public class MyClass
{
    public void MyCleanUpmethod(object p)
    {
       // My Clean up proccess here 
       ....
    }       

    ~ MyClass()
    {
        MyCleanUpmethod(new object());
    }
}

My problem is that ~ MyClass() only runs when close the entire application, I need to run it when MyClass is not used anymore during current procces.

It mean if MyClass is used as Datacontext of a window ~ MyClass() must run when the window closes, if an instance of MyClass is used in other things diferent as Datacontext, ~ MyClass() must run too

How can I do that ?

Juan Pablo Gomez
  • 5,203
  • 11
  • 55
  • 101

2 Answers2

2

You should take a look at the IDisposable interface. Your destructor code then goes into the Dispose method which should be called by your own code. Alternatively you can use a using statement, which takes care of calling Dispose at the end of the scope. For a clean implementation of the dispose pattern take a look at the MSDN article.

Using a finalizer is not appropriate because it is called by the garbage collector, which might be not in time (like in your case).

Matthias
  • 15,919
  • 5
  • 39
  • 84
  • Tks. I'm Reading the article now. – Juan Pablo Gomez Oct 27 '13 at 15:38
  • Regarding the call to the dispose method, this [question](http://stackoverflow.com/questions/6983801/how-to-use-disposable-view-models-in-wpf) may help. – Matthias Oct 27 '13 at 15:43
  • After read the article just one question why if my class implements IDisposble, it doesn't ``Dispose`` Automatically when close my window? – Juan Pablo Gomez Oct 27 '13 at 20:26
  • `Dispose` is not called automatically. Rather, implementing `IDisposable` is a best practice for releasing resources. You're still responsible for calling the method. Maybe this [question](http://stackoverflow.com/questions/502761/disposing-wpf-user-controls) can help you out. – Matthias Oct 27 '13 at 20:41
  • My problem is how to call Dispose From my ``WPF`` window Before it is closing. – Juan Pablo Gomez Oct 27 '13 at 20:56
  • No, I'm using MVVM pattern, and all the logic resides at VM and don't know how to use ``using`` on ``XAML`` – Juan Pablo Gomez Oct 27 '13 at 21:01
  • I think there's some misunderstanding. You wouldn`t use the `using` keyword for this. That was just regarding the dispose pattern itself (which is an important thing to know). In your case, you'd probably just call `Dispose` somewhere. I'm pretty sure this should make it: http://stackoverflow.com/a/13930385/568266 The only difference is that you don't have a user control. – Matthias Oct 27 '13 at 21:06
  • Just a problem, I need to run something commands against some lists on ``DataContext`` but in this method ``closing``, Datacontext is ``null`` Think must reformule my question. Tks a lot for your help. – Juan Pablo Gomez Oct 27 '13 at 21:37
  • 1
    I'd also argue that this is worth another question. There has been some problems regarding the finalizer ;) Better create another question for the mvvm stuff. – Matthias Oct 27 '13 at 21:40
  • Tks a lot for your help Mathias – Juan Pablo Gomez Oct 27 '13 at 21:54
1

Don't use the destructor for managed code. instead implement IDisposable:

public class MyClass : IDisposable
{
    public void MyCleanUpmethod(object p)
    {
       // My Clean up proccess here 
       ....
    }       

    public void Dispose()
    {
        MyCleanUpmethod(new object());
    }
}

and then use the class like this:

using (var cls = new MyClass())
{
 //do stuff with cls
}
Jens Kloster
  • 11,099
  • 5
  • 40
  • 54