|
-
Jul 26th, 2012, 11:00 AM
#1
Thread Starter
Fanatic Member
Where's the best place to unsubscribe to (from?) an event
I've got a UserControl. (call this the Parent UC)
The Parent UC loads another UserControl (Call this the Child UC)
The Child UC subscribes to an event that is happening in the Parent UC.
No probllem so far all of that is working just fine.
But thinking about it - my understanding is that when the Child UC is unloaded it won't be garbage collected because the event in the parent UC still has a reference to the event handler in the child UC.
So, I want the Child UC to unsubscribe to the event before unloading.
But the Child UC doesn't have any UnLoad event so doesn't actually know when it's being unloaded. Should I just unsubscribe in the child uc's dispose method ?
Although while typing all that I've just remembered that I never actually unload the Child UC. It's the Parent UC that is unloaded by 'its' parent (The WIndows Form) so I don't need to worry about memory leaks (do I ?)
But, just because i typed all that, where 'would' the child uc unsubscribe to the event in the parent uc if I did for some reason want to unload the Child UC while keeping the parent UC ?
If you see what I mean. er. I think.
Last edited by IanS; Jul 26th, 2012 at 11:05 AM.
-
Jul 26th, 2012, 11:27 AM
#2
Re: Where's the best place to unsubscribe to (from?) an event
hard to say because normally the child control doesn't care about what's going on in it's parent... usually the parent subscribes to events of the child ... much like the way a form (the parent) subscribes/handles events of child controls (like a text box) ... the text box doesn't subscribe to the parent, it's the other way around. That said... if you are using AddHandler for event subscriptions, you should have a complimentary RemoveHandler call somewhere... the Dispose event would be a good place for that. When the parent disposes, it should be calling the dispose method of any child objects it contains, which in turn should be removing any handling/subscriptions and be disposing of it's own objects where necessary.
-tg
-
Jul 27th, 2012, 08:39 AM
#3
Thread Starter
Fanatic Member
Re: Where's the best place to unsubscribe to (from?) an event
Yes, it does seem strange doing it this way round. There's certain events being triggered in the parent UC by an external hardware/device. The parent UC is handling most of these events but the child uc would like a handle on the data supplied by one specific event (currently the parent doesn't care/respond to that event). I suppose the proper way would be for the parent to respond to that event and to push the data up to the child.
But having the child subscribe to the event just seems easier. It can do that without bothering the parent. Not necessarily good code but seems to fit this specific situation.
I just kind of feel that there ought to be an .Unload event in the UC where I can put my clean up code instead of editing the .designer.cs file which is where the dispose method is located.
-
Jul 31st, 2012, 03:08 PM
#4
Re: Where's the best place to unsubscribe to (from?) an event
You might need the opposite of the constructor, the finalizer
-
Jul 31st, 2012, 03:49 PM
#5
Thread Starter
Fanatic Member
Re: Where's the best place to unsubscribe to (from?) an event
Thank you both for your feedback.
But I'm beginning to feel uncomfortable about the way that's coded - child subscribing to events of its parent is a potential can of worms.
So going to rewrite this properly.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|