Results 1 to 5 of 5

Thread: Where's the best place to unsubscribe to (from?) an event

  1. #1
    Fanatic Member
    Join Date
    Mar 09
    Posts
    715

    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.

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 02
    Posts
    21,636

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.-I also subscribe to all threads I participate, so there's no need to pm when there's an update.*
    *Proof positive that searching the forums does work: View Thread *
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *
    * Use Offensive Programming, not Defensive Programming. * On Error Resume Next is error ignoring, not error handling(tm).
    "There is a major problem with your code, and VB wants to tell you what it is.. but you have decided to put your fingers in your ears and shout 'I'm not listening!'" - si_the_geek on using OERN

  3. #3
    Fanatic Member
    Join Date
    Mar 09
    Posts
    715

    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.

  4. #4
    Frenzied Member Lightning's Avatar
    Join Date
    Oct 02
    Location
    Eygelshoven
    Posts
    1,556

    Re: Where's the best place to unsubscribe to (from?) an event

    You might need the opposite of the constructor, the finalizer
    VB6 & C# (WCF LINQ) mostly


    If you need help with a WPF/WCF question post in the NEW WPF & WCF forum and we will try help the best we can

    My site

    My blog, couding troubles and solutions

  5. #5
    Fanatic Member
    Join Date
    Mar 09
    Posts
    715

    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
  •