473,549 Members | 2,573 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Must call RemoveHandler after AddHandler?

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
--

Aug 8 '06 #1
12 7116
No, you shouldn't have to do anything in the case you are describing.

"Tom" <to*@nospam.com wrote in message
news:%2******** *******@TK2MSFT NGP06.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
--

Aug 8 '06 #2
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.com wrote in message
news:%2******* ********@TK2MSF TNGP06.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
RemoveHandl er 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
--
Aug 8 '06 #3
Had you designated the handlers at design time by adding the 'Handles
objectName.Even tName' 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.com wrote in message
news:uM******** ******@TK2MSFTN GP04.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.com wrote in message
news:%2****** *********@TK2MS FTNGP06.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
RemoveHandle r 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
--

Aug 8 '06 #4
It's safe to make sure there is no possibility to re-execute the
AddHandler ... statement (a remove might even precede, just in case...)

Tommaso

Tom ha scritto:
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.com wrote in message
news:%2******** *******@TK2MSFT NGP06.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
RemoveHandle r 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
--
Aug 8 '06 #5
Tom
Marina: Very good... but let's address what Tommaso mentioned - Let's
say the user closes the form, then immediately re-opens it. And let's
assume that GC hasn't occured yet. If that is the case, would
re-opening the form cause the CLR to just reload the in-memory form,
and then (if this is the case) wouldn't doing the AddHandler again
cause an issue? Or I am completely off-base here?

Tom
--

Marina Levit [MVP] wrote:
>Had you designated the handlers at design time by adding the 'Handles
objectName.Eve ntName' 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.com wrote in message
news:uM******* *******@TK2MSFT NGP04.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.com wrote in message
news:%2***** **********@TK2M SFTNGP06.phx.gb l...
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
RemoveHandl er for each one of the controls I created? Or can I
just let .NET handle it when the form is closed and garbage
collectio n is done?

Tom
--
Aug 8 '06 #6
As marina has well explained, I would not be much worried
by your scenario. When the form is disposed, the handler is gone.

There are other situations, where one has to watch not to
add the handler multiple times, which could results in errors
usually not straightforward to debug.

About forms, it depends... if the form is modal (.shodialog)
closing the form will not dispose it. So the handler will
still be there awaiting ...

------

In general, especially if code is not to be distributed to dummies, I
feel
that using delegates instead of events is safer and more confortable
(except for some simple cases)

For instance, it is more difficult that you forget to handle calls if
the
function is not set up a null pointer error will readily occur.
Plus, it is not possible to replicate the handling of a call.
Finally, they are readily available by the intellisense...

But that is matter of taste, I guess ...

-tommaso
Tom ha scritto:
Marina: Very good... but let's address what Tommaso mentioned - Let's
say the user closes the form, then immediately re-opens it. And let's
assume that GC hasn't occured yet. If that is the case, would
re-opening the form cause the CLR to just reload the in-memory form,
and then (if this is the case) wouldn't doing the AddHandler again
cause an issue? Or I am completely off-base here?

Tom
--

Marina Levit [MVP] wrote:
Had you designated the handlers at design time by adding the 'Handles
objectName.Even tName' 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.com wrote in message
news:uM******** ******@TK2MSFTN GP04.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.com wrote in message
news:%2****** *********@TK2MS FTNGP06.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
RemoveHandle r 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
--
Aug 8 '06 #7
But you would attach the handler on a different instance of that form. The
first instance is not affected. When that item needs to be garbage
collected, it should be, since it doesn't have any handlers on external
objects, just on its own.

"Tom" <to*@nospam.com wrote in message
news:OR******** ******@TK2MSFTN GP03.phx.gbl...
Marina: Very good... but let's address what Tommaso mentioned - Let's
say the user closes the form, then immediately re-opens it. And let's
assume that GC hasn't occured yet. If that is the case, would
re-opening the form cause the CLR to just reload the in-memory form,
and then (if this is the case) wouldn't doing the AddHandler again
cause an issue? Or I am completely off-base here?

Tom
--

Marina Levit [MVP] wrote:
>>Had you designated the handlers at design time by adding the 'Handles
objectName.Ev entName' 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.com wrote in message
news:uM****** ********@TK2MSF TNGP04.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
describin g.

"Tom" <to*@nospam.com wrote in message
news:%2**** ***********@TK2 MSFTNGP06.phx.g bl...
>I use dynamically created controls all the time. I.E. I create
>the control in code then use AddHandler to add the necessary
>delegate s 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
>RemoveHand ler for each one of the controls I created? Or can I
>just let .NET handle it when the form is closed and garbage
>collecti on is done?
>
>Tom
>--

Aug 8 '06 #8
Tom
OK, cool... thanks to both of you for the advice!
--

Marina Levit [MVP] wrote:
>But you would attach the handler on a different instance of that
form. The first instance is not affected. When that item needs to
be garbage collected, it should be, since it doesn't have any
handlers on external objects, just on its own.

"Tom" <to*@nospam.com wrote in message
news:OR******* *******@TK2MSFT NGP03.phx.gbl.. .
>>Marina: Very good... but let's address what Tommaso mentioned -
Let's say the user closes the form, then immediately re-opens it.
And let's assume that GC hasn't occured yet. If that is the case,
would re-opening the form cause the CLR to just reload the
in-memory form, and then (if this is the case) wouldn't doing the
AddHandler again cause an issue? Or I am completely off-base here?

Tom
--
Marina Levit [MVP] wrote:
>>>Had you designated the handlers at design time by adding the
'Handles objectName.Even tName' 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.com wrote in message
news:uM***** *********@TK2MS FTNGP04.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.com wrote in message
>news:%2*** ************@TK 2MSFTNGP06.phx. gbl...
>>I use dynamically created controls all the time. I.E. I
>>create the control in code then use AddHandler to add the
>>necessa ry 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
>>--
Aug 8 '06 #9
>
About forms, it depends... if the form is modal (.shodialog)
closing the form will not dispose it. So the handler will
still be there awaiting ...
On what?

Aug 9 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1465
by: teo | last post by:
Hallo, I have used many time the AddHandler method (becuse I'm buildin an array of controls). Now I 'd like to know if I have to use the RemoveHandler method. In fact I use the array of control till I close my app, and I think that when my app close,
15
6653
by: Patrick.O.Ige | last post by:
Hi All i'm getting error with my TreeView Menu(I want to have a button with ExpandALL and CollapseALL):- Error:- 'AddressOf' operand must be the name of a method; no parentheses are needed. I use VB.Net and i changed the C# code from:- private void ExpandAll() { for(int i=0; i < yourTree.Nodes.Count; i++)
1
1299
by: Urs Vogel | last post by:
Hi I need to detect wether an event handler has been added to an (Word object Quit) event. Question 1: How do I look up the event handler queue of an event, either native .Net or COM interop? Question 2: Does the AddHandler function add the handler mutliple times, or does it automatically overwrite previously attached handlers? If the...
0
1636
by: Atara | last post by:
regarding the answers I got from the thread "Does each "New" needs a corresponding "Dispose" " (http://www.developersdex.com/vb/message.asp?p=&r=3582570&page=2) I have some questions: Till now, when I used dynamicCreatedControls with 'AddHandler', I wrote the 'RemoveHandler' code in my function that was called from the automaticGenerated...
22
3173
by: Ricky W. Hunt | last post by:
First, the subject probably doesn't use the correct terms but I'm not sure what it's called in VB. I'm writing a media player app. The subroutine that handles the "open file" button contains an If statement to see if a file was already playing and if so executes some code to stop the previous file from playing among other things. There's...
3
1169
by: Gary Kahrau | last post by:
If I create a form dynamically, is there some way I can point to a dll function/ sub when the dynamic button is pushed? Or do something like this. dim DynSub as string DynSub = "DoItSub" <== Value will come from the dynamic loader. Call DynSub.
3
5006
by: hartley_aaron | last post by:
Hi, I was trying to store the address of the my current handler for a particular event so as to simplify using AddHandler and RemoveHandler throughout my code. However, I cannot seem to get any kind of variable to except the data. When I tried a Long I got the message "'AddressOf' expression cannot be converted to 'Long' because 'Long' is...
0
863
by: =?Utf-8?B?xJBvbm55?= | last post by:
Hi, I think I understand why it behaves how it behaves. A new feature of VB9 allows you to AddHandler for methods with slightly different signatures than signature of event is. However as I understand it some hidden routing method/delegate (which routes event to method with different signature) is created in compile time. And when you uses...
4
5316
by: Sid Price | last post by:
I have a compiler warning I do not understand how to fix. I am trying to remove an event handler and get the following message: Warning 1 The 'AddressOf' expression has no effect in this context because the method argument to 'AddressOf' requires a relaxed conversion to the delegate type of the event. Assign the 'AddressOf' expression to a...
0
7526
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7457
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7723
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7965
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7483
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7817
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5092
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3504
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1949
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.