必须在AddHandler之后调用RemoveHandler吗? [英] Must call RemoveHandler after AddHandler?

查看:127
本文介绍了必须在AddHandler之后调用RemoveHandler吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直使用动态创建的控件。 I.E.我在代码中创建了

控件,然后使用AddHandler为

处理添加必要的代理(如Click等)。


处理完成后是否必须调用RemoveHandler?在其他

字样中,当表单关闭时,我是否必须为我创建的每个控件执行一个RemoveHandler for

?或者我可以让.NET处理它

当窗体关闭并进行垃圾收集时?


Tom

- -

解决方案

不,在你描述的情况下,你不应该做任何事情。


汤姆 < to *@nospam.com写信息

新闻:%2 *************** @ TK2MSFTNGP06.phx.gbl ...


>我一直使用动态创建的控件。 I.E.我在代码中创建了

控件,然后使用AddHandler为

处理添加必要的代理(如Click等)。


处理完成后是否必须调用RemoveHandler?在其他

字样中,当表单关闭时,我是否必须为我创建的每个控件执行一个RemoveHandler for

?或者我可以让.NET处理它

当窗体关闭并进行垃圾收集时?


Tom

- -



酷...所以基本上,当表格关闭并丢弃时,那么

代表.NET'/ $
运行时/ CLR会自动处理你的问题吗?

-


Marina Levit [MVP]写道:


>不,在你描述的情况下,你不应该做任何事情。

"汤姆" < to *@nospam.com写信息
新闻:%2 *************** @ TK2MSFTNGP06.phx.gbl ...


>>我一直使用动态创建的控件。 I.E.我在代码中创建了
控件,然后使用AddHandler添加必要的代理进行处理(如Click等)。

在处理完成后是否必须调用RemoveHandler?换句话说,当表单关闭时,我是否必须为我创建的每个控件执行一个
RemoveHandler?或者我可以在表单关闭和垃圾收集完成后让.NET处理它吗?

Tom
-


如果您在设计时通过在方法末尾添加''Handles

objectName.EventName''来指定处理程序,你不会想到你

需要分离处理程序。


AddHandler只是一种编程方式来附加处理程序而不是

声明方式。


只要对象和事件处理程序方法都在用户

控件或表单中,当用户控件或表单消失了,对象应该有资格获得GC,因为所有的事件处理程序方法都在它自己的

定义范围内。


汤姆 < to *@nospam.comwrote in message

news:uM ************** @ TK2MSFTNGP04.phx.gbl ...


很酷......所以基本上,当表格关闭并丢弃时,那么你被挂钩的

代表会被自动处理掉。 NET

运行时间/ CLR?

-


Marina Levit [MVP]写道:


>>不,在您描述的情况下,您不必做任何事情。

Tom < to *@nospam.com写信息
新闻:%2 *************** @ TK2MSFTNGP06.phx.gbl ...


>>>我一直使用动态创建的控件。 I.E.我在代码中创建了
控件,然后使用AddHandler添加必要的代理进行处理(如Click等)。

在处理完成后是否必须调用RemoveHandler?换句话说,当表单关闭时,我是否必须为我创建的每个控件执行一个
RemoveHandler?或者我可以在表单关闭和垃圾收集完成后让.NET处理它吗?

Tom
-



I use dynamically created controls all the time. I.E. I create the
control in code then use AddHandler to add the necessary delegates for
processing (like Click, etc).

Does one have to call RemoveHandler after processing is done? In other
words, when the form is closing do I have to do a RemoveHandler for
each one of the controls I created? Or can I just let .NET handle it
when the form is closed and garbage collection is done?

Tom
--

解决方案

No, you shouldn''t have to do anything in the case you are describing.

"Tom" <to*@nospam.comwrote in message
news:%2***************@TK2MSFTNGP06.phx.gbl...

>I use dynamically created controls all the time. I.E. I create the
control in code then use AddHandler to add the necessary delegates for
processing (like Click, etc).

Does one have to call RemoveHandler after processing is done? In other
words, when the form is closing do I have to do a RemoveHandler for
each one of the controls I created? Or can I just let .NET handle it
when the form is closed and garbage collection is done?

Tom
--



Cool... So basically, when the form is closed and discard, then the
delegates you ''hooked'' are automatically taken care of by the .NET
runtime/CLR?
--

Marina Levit [MVP] wrote:

>No, you shouldn''t have to do anything in the case you are describing.

"Tom" <to*@nospam.comwrote in message
news:%2***************@TK2MSFTNGP06.phx.gbl...

>>I use dynamically created controls all the time. I.E. I create the
control in code then use AddHandler to add the necessary delegates
for processing (like Click, etc).

Does one have to call RemoveHandler after processing is done? In
other words, when the form is closing do I have to do a
RemoveHandler for each one of the controls I created? Or can I just
let .NET handle it when the form is closed and garbage collection
is done?

Tom
--


Had you designated the handlers at design time by adding the ''Handles
objectName.EventName'' at the end of a method, you wouldn''t be thinking you
need to detach the handler.

AddHandler is just a programmatic way of attaching the handler instead of a
declarative way.

As long as the objects and the event handler methods are all in the user
control or form, when that user control or form is gone, the object should
be eligible for GC, since all the event handler methods are within its own
definition.

"Tom" <to*@nospam.comwrote in message
news:uM**************@TK2MSFTNGP04.phx.gbl...

Cool... So basically, when the form is closed and discard, then the
delegates you ''hooked'' are automatically taken care of by the .NET
runtime/CLR?
--

Marina Levit [MVP] wrote:

>>No, you shouldn''t have to do anything in the case you are describing.

"Tom" <to*@nospam.comwrote in message
news:%2***************@TK2MSFTNGP06.phx.gbl...

>>>I use dynamically created controls all the time. I.E. I create the
control in code then use AddHandler to add the necessary delegates
for processing (like Click, etc).

Does one have to call RemoveHandler after processing is done? In
other words, when the form is closing do I have to do a
RemoveHandler for each one of the controls I created? Or can I just
let .NET handle it when the form is closed and garbage collection
is done?

Tom
--



这篇关于必须在AddHandler之后调用RemoveHandler吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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