3
votes

I want to dispose all resources (binded events, variables, objects, data structures, data bindings, etc.) of my form on form close. Currently I,m disposing my resources explicitly one by one on one of my form just to test this operation. But my application is big and there are many forms in it and if i started disposing all resources explicitly on every form, it will take time. So my question is that is there a general method or technique? so that all my resources gets disposed rather then doing it explicitly one by one.

Thanks.

4
Generally CLR garbage collector would dispose anything managed in your code - Iman Nemati

4 Answers

3
votes

You should use using statements for your IDisposable objects, rather than calling Dispose().

As a rule, when you use an IDisposable object, you should declare and instantiate it in a using statement. The using statement calls the Dispose method on the object in the correct way, and (when you use it as shown earlier) it also causes the object itself to go out of scope as soon as Dispose is called. Within the using block, the object is read-only and cannot be modified or reassigned.

using (Font font1 = new Font("Arial", 10.0f)) 
{
     byte charset = font1.GdiCharSet;
}

https://msdn.microsoft.com/en-us/library/yh598w02.aspx

1
votes

Use Application.Exit() Method on your form close event.

MSDN Says:

It will Informs all message pumps that they must terminate, and then closes all application windows after the messages have been processed.

1
votes

I agree with @Owen Pauling, you should be using IDisposable and the relevant syntax. It is highly recommended, you scope the object life cycle for each object rather than in a generic way like disposing all objects on a Closing event.

If you are hand tied in the current situation where you are, you could leverage GC.Collect(). However, Please Note it is not recommended until you are very sure you need this and the payoffs by calling this is high on the performance of the application.

To achieve, maximum benefits, you should implement the IDisposable like @Owen Pauling is recommending and then calling GC.Collect() from the parent thread of your current form. i.e. you will be required to create a shell (be it visible / invisible) form to call the actual form which you have. After you dispose the form which does all the work call GC.Collect().

References - https://msdn.microsoft.com/en-us/library/s5zscb2d%28v=vs.85%29.aspx where the practice of callto GC.Collect is recommended to be practiced with caution.

1
votes

If it's your own resources, you can move them in one class-container, and realize all IDispose logic in one method there. Else you can only reduce your code, by creating Array _resources in every your form and invoke something like

foreach(IDisposable elem in _resources){ elem.Dispose();}

Or use reflection for the same trick.

Furthermore, I think that it's not your real problem. Dispose() invokation on one's Jack can't take a long time, unlike Dispose() realization.

Common rule on perfomance optimization: first find a bottleneck, then optimize it.