Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dispose all resources on form close c#

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.

like image 210
saadsaf Avatar asked Nov 22 '25 05:11

saadsaf


1 Answers

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

like image 158
Owen Pauling Avatar answered Nov 24 '25 20:11

Owen Pauling