473,770 Members | 7,213 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 7143
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
1475
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
6671
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
1307
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 latter is the case, the problem would be solved, but I couldn't find any comments
0
1659
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 Dispose function. (I have added example at the end of this message. )
22
3197
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 also a "stop" button on the form that contains the exact same code. Obviously it would be easier if...
3
1193
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
5014
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 not a delegate type." I tried other datatypes as well but was not able to find anything that...
0
872
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 RemoveHandler another one is created which results to RemoveHandler removing handler which had been...
4
5352
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 variable, and use the variable to add or remove the method as the handler. There appears to be no...
0
10230
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10058
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10004
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9870
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8886
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6678
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5313
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3972
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
3
2817
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.