简体   繁体   English

将所有资源都放在窗体关闭C#上

[英]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. 谢谢。

You should use using statements for your IDisposable objects, rather than calling Dispose(). 您应该对IDisposable对象使用using语句,而不是调用Dispose()。

As a rule, when you use an IDisposable object, you should declare and instantiate it in a using statement. 通常,使用IDisposable对象时,应在using语句中声明并实例化它。 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. using语句以正确的方式在对象上调用Dispose方法,并且(如前所述,当您使用它时)它还会导致对象本身在调用Dispose时就超出范围。 Within the using block, the object is read-only and cannot be modified or reassigned. 在using块中,该对象是只读的,无法修改或重新分配。

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

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

Use Application.Exit() Method on your form close event. 在窗体关闭事件上使用Application.Exit()方法。

MSDN Says: MSDN说:

It will Informs all message pumps that they must terminate, and then closes all application windows after the messages have been processed. 它将通知所有消息泵它们必须终止,然后在处理完消息后关闭所有应用程序窗口。

I agree with @Owen Pauling, you should be using IDisposable and the relevant syntax. 我同意@Owen Pauling,您应该使用IDisposable及其相关的语法。 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. 强烈建议您为每个对象确定对象生命周期的范围,而不是像在Closing事件上放置所有对象那样一般的方法。

If you are hand tied in the current situation where you are, you could leverage GC.Collect() . 如果您在当前的情况下GC.Collect() ,则可以利用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. 为了获得最大的收益,您应该像@Owen Pauling建议的那样实现IDisposable,然后从当前表单的父线程中调用GC.Collect() ie 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() . GC.Collect()完成所有工作的表单后,调用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. 参考- https://msdn.microsoft.com/en-us/library/s5zscb2d%28v=vs.85%29.aspx哪里致电第一的做法GC.Collect建议要谨慎实施。

If it's your own resources, you can move them in one class-container, and realize all IDispose logic in one method there. 如果是您自己的资源,则可以将它们移动到一个类容器中,并在那里以一种方法实现所有IDispose逻辑。 Else you can only reduce your code, by creating Array _resources in every your form and invoke something like 否则,您只能通过在每个表单中创建Array _resources并调用类似的代码来减少代码

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 . Dispose()实现不同,对Jack进行Dispose()调用不会花费很长时间。

Common rule on perfomance optimization: first find a bottleneck, then optimize it. 性能优化的通用规则:首先找到一个瓶颈,然后对其进行优化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM