处理(bool),Idisposable,表格关闭等 [英] Dispose(bool), Idisposable, form closing etc.

查看:89
本文介绍了处理(bool),Idisposable,表格关闭等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我对处理等感到困惑,希望有人能把我弄好。


1. Dispose(Bool )IDE为表单生成无关
带有IDisposable的
,对吗?

2.那么什么时候调用?表格关闭时?如果在表单关闭时自动导致

,那么我怎么能在ShowDialog()返回后从

模态访问?或者它只适用于无模式

表格?

3.垃圾收集器是否自动调用Dispose(),这是IDisposable的一部分

onbject是垃圾收集?我知道这是

非确定性等等。这就是所谓的Finalizer或

无论它叫什么?

4我不明白垃圾收集如何与表格一起使用。如果我

有一个方法声明一个变量是一个形式,并且

在同一个例程中实例化它,当我离开例程时

变量被释放,表单的引用计数应该为零

因此可以用于垃圾收集,不是吗?还有其他

在某个地方推荐吗?


困惑,


Ray

推荐答案

2007-11-19 12:14:30 -0800,rbrowning1958< RB *********** @ gmail。 comsaid:
On 2007-11-19 12:14:30 -0800, rbrowning1958 <RB***********@gmail.comsaid:

您好,


我对dispose等感到困惑,并希望有人能帮我解决。


1. IDE为表单生成的Dispose(Bool)与IDisposable无关

,对吗?
Hello,

I am confused by dispose etc. and hope someone can set me right.

1. The Dispose(Bool) the IDE generates for a form has nothing to do
with IDisposable, right?



我不会那么远。它不是界面的一部分,但它是界面常见实现的一部分


I wouldn''t go that far. It''s not part of the interface, but it is part
of the common implementation of the interface.


2.所以当这叫什么?表格关闭时?如果在表单关闭时自动导致

,那么我怎么能在ShowDialog()返回后从

模态访问?或者它只适用于无形的

表格?
2. So when is this called? When a form is closed? If this is caused
automatically when a form is closed how can I then access things on a
modal from after ShowDialog() returns? Or is it only for modeless
forms?



使用ShowDialog()显示的表单在关闭时不会被处理。

Forms shown with ShowDialog() are not disposed when they are closed.


3.是否垃圾收集器自动调用Dispose(),当onbject被垃圾收集时,它是IDisposable的一部分。我知道这是

非确定性等等。这就是所谓的Finalizer或

无论它叫什么?
3. Does the garbage collector automically call Dispose() which is part
of IDisposable when an onbject is garbage collected? I know this is
non-deterministic etc. Is this what is known as the Finalizer or
whatever it''s called?



如果你没有调用Dispose(),那么终结器将不会被抑制,并且最终可能会调用终结器。在终结器运行之前,无法收集对象

(对于具有

终结器并且不能抑制最终化的对象)。

If you have not called Dispose(), then the finalizer won''t be
suppressed and the finalizer may eventually be called. The object
cannot be collected until the finalizer is run (for objects that have a
finalizer and don''t suppress finalizing).


4.我不明白垃圾收集如何与表格一起使用。
4. I don''t understand how garbage collection works with forms.



它与其他实现IDisposable的类相同。

It works the same as for other classes that implement IDisposable.


如果我

有一个方法声明一个变量,它是一个表单,并且

在同一个例程中实例化它,当我离开例程

变量被释放时,那个表格的引用数应该为零

因此可以用来收集垃圾,不是吗?还有另一个

在某个地方引用它吗?
If I
have a method which declares a variable which is a form, and
instantiate it in the same routine, when I leave the routine the
variable is released, the form''s reference count should then be zero
and therefore a candiate for garbage collection, no? Is there another
refernce to it somewhere?



没有引用计数。


就垃圾收集的资格而言,重要的是

哪里有对其他地方的表格的引用。对于

表单,其实例将添加到应用程序的打开表单列表中,

可以从Application.OpenForms属性中检索。 br />

所以,是的......某处还有对该表格的引用。


Pete

There''s no referencing counting.

As far as eligibility for garbage collection goes, what matters is
where there''s a reference to the form somewhere else. In the case of a
form, its instance is added to the application''s list of open forms,
which you can retrieve from the Application.OpenForms property.

So, yes...there''s still a reference to the form somewhere.

Pete


11月19日下午2:14,rbrowning1958< RBrowning1 ... @ gmail.comwrote:
On Nov 19, 2:14 pm, rbrowning1958 <RBrowning1...@gmail.comwrote:

您好,


我对dispose等感到困惑,希望有人能把我弄好。


1. IDE生成的Dispose(Bool)一张表格无关
带有IDisposable的
,对吗?
Hello,

I am confused by dispose etc. and hope someone can set me right.

1. The Dispose(Bool) the IDE generates for a form has nothing to do
with IDisposable, right?



嗯,它是相关的,因为这是IDisposable的规范

实现所必需的。但是,你是对的。 Dispose(bool)和IDisposable之间没有

严格的关系。

Well, it is related because that is required for the canonical
implemenation of IDisposable. But, you are correct. There is no
strict relationship between Dispose(bool) and IDisposable.


2.那么什么时候被调用?表格关闭时?如果在表单关闭时自动导致

,那么我怎么能在ShowDialog()返回后从

模态访问?或者它只适用于无形的

表格?
2. So when is this called? When a form is closed? If this is caused
automatically when a form is closed how can I then access things on a
modal from after ShowDialog() returns? Or is it only for modeless
forms?



当从

用户代码调用IDisposable.Dispose方法时调用它,或者当Finalize是GC时调用它作为最后的手段调用。这是

已经有一段时间,但我不认为关闭一个模态形式处理它。

另外,一些(大多数?)类作者选择允许财产

即使在对象处理完毕后也会返回。

It is called when either the IDisposable.Dispose method is called from
user code or as a last resort by the GC when Finalize is called. It''s
been awhile, but I don''t think closing a modal form disposes it.
Furtheremore, some (most?) class authors choose to allow property
getters to return even after the object has been disposed.


3.垃圾收集器是否自动调用Dispose()是否为<当一个onbject被垃圾收集时,
的IDisposable?我知道这是

非确定性等等。这就是所谓的Finalizer或

无论它叫什么?
3. Does the garbage collector automically call Dispose() which is part
of IDisposable when an onbject is garbage collected? I know this is
non-deterministic etc. Is this what is known as the Finalizer or
whatever it''s called?



GC不会调用IDisposable.Dispose。但是,它可能会间接地调用Dispose(bool)因为它是Finalize方法的一部分

(C#中的析构函数)...至少如果你''我选择了IDisposable的规范

实现。

The GC will not call IDisposable.Dispose. However, it may indirectly
call Dispose(bool) because it''s part of the Finalize method
(destructor in C#)...at least if you''ve choosen the canonical
implementation of IDisposable.


4.我不明白垃圾收集如何与表格一起使用。如果我

有一个方法声明一个变量是一个形式,并且

在同一个例程中实例化它,当我离开例程时

变量被释放,表单的引用计数应该为零

因此可以用于垃圾收集,不是吗?还有另一个

在某个地方引用它吗?
4. I don''t understand how garbage collection works with forms. If I
have a method which declares a variable which is a form, and
instantiate it in the same routine, when I leave the routine the
variable is released, the form''s reference count should then be zero
and therefore a candiate for garbage collection, no? Is there another
refernce to it somewhere?



不,不涉及引用计数。假设表单

并没有以某种方式在静态数据结构中注册自己,那么一旦它掉出来,它就会成为垃圾收集的候选者

范围。

No, there is no reference counting involved. Assuming the form
doesn''t somehow register itself in a static data structure then it
should be a candidate for garbage collection once it falls out of
scope.


>

困惑,


Ray
>
Confused,

Ray


Ray,


见内联:
Ray,

See inline:

谢谢所有回复我已经随意选择你的回复,

Nicholas。
Thank you all for replying I''ve arbitrarily chosen yours to reply to,
Nicholas.



我受宠若惊= P

I''m flattered =P


所以让我看看我是否有这个权利:


1. Dispose(Bool)作为表单的一部分并不是

IDisposable接口的明确部分,而是IDisposable,Dispose的实现

进一步提升了类层次结构(我猜想的形式)。这是

waht Brian称为Canonical实现(我从来没有理解

这个词是什么意思!)。
So let me see if I have this right:

1. Dispose(Bool) as part of a form is not explicitly part of the
IDisposable interface, but the implementation of IDisposable,Dispose
further up the class hierarchy (form I suppose) calls this. This is
waht Brian called the Canonical implementation (I never did understand
what that word meant!).



是的。

Yep.


2,当调用无模式表格时,点击表格'关闭按钮,

或调用close()方法调用IDisposable.Dispose,然后调用

调用Dispose(bool)。
2, When a modeless form is called, clicking the form''s close button,
or calling the close() method calls IDisposable.Dispose which in turn
calls Dispose(bool).



是的。

Yep.


3.如果是模态表格,请点击关闭按钮或致电

close方法只是隐藏了表单,并没有调用

IDisposable.dispose。因此,我必须自己做或使用Using

子句。
3. If it''s a modal form, clicking the close button or calling the
close method just hides the form, and does not call
IDisposable.dispose. Therefore I have to do it myself or use a Using
clause.



是的。

Yep.


4.表单的析构函数将调用Dispose(bool)尽管在大多数情况

它不会做太多,因为它已经被

IDisposable.dispose调用。所以...我猜是Idisposable调用

dispose(true),而析构函数调用Dispose(false)?
4. A form''s destructor will call Dispose(bool) although in most cases
it will not do much because it will already have been called by
IDisposable.dispose. SO...I''m guessing that Idisposable calls
dispose(true), whereas the destructor calls Dispose(false)?



是的。

Yep.


5.关于垃圾收集 - 彼得没有正确

他说表单不是垃圾收集的,因为它是由application.openForms引用的

5. With regard to garbage collecting - doesn''t Peter have it correct
where he says the form is not garbage collected because it is
referenced by application.openForms?



是的。

Yep.


6. Brian提到某些类作者写属性getter
即使表格被销毁后,
也会返回数据 - 我不会理解这一点。这不是说财产获取者必须是静态的 - 而且它会在哪里保存数据?在静态类变量?那个

意味着我只有一个表格实例,对吗?
6. Brian mentioned that some class authors write property getters to
return data even after the form has been destroyed - I don''t
understand this. Doesn''t this mean the property getter has to be
static - and where would it savbe the data? In static class vars? That
implies I only have one instance of the form, right?



否。属性将是实例,您可以在窗体上调用Dispose之前从

表单实例访问它们。在调用dispose后访问属性,你^ ^可以^ get ^

,但前提是它们是存储不依赖于访问已处理表单的数据的
字段

本身。


例如,如果你有一个包含文字的文本框,然后你以形式模式显示

,如果您通过调用文本框中的Text属性在属性的文本框中公开文本,那么如果您想要访问该属性,则会出现例外情况。表格被处理(因为文本框也是处理的b $ b)。但是,如果在处理表单之前将TextBox的内容存储在

a字符串中,然后访问

属性中的字符串,则不会有一个问题,即使表格处理完毕。


但是,我认为你不应该担心将所有的

控制值转移到字段(而不是从属性/方法中访问控件上的值

),因为期望

你的对象不能工作是完全合理的处置。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


>

谢谢大家,


Best


Ray

>
Thanks everbody,

Best

Ray


这篇关于处理(bool),Idisposable,表格关闭等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆