tags:

views:

29

answers:

1

I'm implementing a UserControl which implements IMessageFilter. It calls Application.AddMessageFilter in its constructor. I'd like to remove it from the message filter in its Dispose(bool disposing) method, but I don't know whether to place the call to Application.RemoveMessageFilter inside the test (so it gets called when disposing is true) or whether I should call it outside the test (so it gets called whenever the method is called).

Which is the right approach?

+1  A: 

You may only call other managed objects when disposing and not when finalizing since there is no guarantee that the other referenced objects have not already been finalized. This means that you should call RemoveMessageFilter only when disposing is true.

Jakob Christensen