PC Review


Reply
Thread Tools Rate Thread

Event handlers and finalize

 
 
Scott Meddows
Guest
Posts: n/a
 
      30th Mar 2004
I'm adding event handlers for dynamically loaded assemblies into a program. I know that I must unregister those event handlers so
the GC can pick up the objects, should I do this in the finalize method?

Thanks.


 
Reply With Quote
 
 
 
 
CJ Taylor
Guest
Posts: n/a
 
      30th Mar 2004
RemoveHandler


"Scott Meddows" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I'm adding event handlers for dynamically loaded assemblies into a

program. I know that I must unregister those event handlers so
> the GC can pick up the objects, should I do this in the finalize method?
>
> Thanks.
>
>



 
Reply With Quote
 
 
 
 
Scott Meddows
Guest
Posts: n/a
 
      30th Mar 2004
yeah, I got the syntax down. I was just wondering if the finalize method would be the best place to do this...

"CJ Taylor" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> RemoveHandler
>
>
> "Scott Meddows" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > I'm adding event handlers for dynamically loaded assemblies into a

> program. I know that I must unregister those event handlers so
> > the GC can pick up the objects, should I do this in the finalize method?
> >
> > Thanks.
> >
> >

>
>



 
Reply With Quote
 
CJ Taylor
Guest
Posts: n/a
 
      30th Mar 2004
Sorry... I apologize for the weak response. Yeah, I do some of my stuff in
the finalizer. Just don't depend on anything else with it. That is, you
don't know exactly when the finalizer is going to be called.

-CJ

"CJ Taylor" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> RemoveHandler
>
>
> "Scott Meddows" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > I'm adding event handlers for dynamically loaded assemblies into a

> program. I know that I must unregister those event handlers so
> > the GC can pick up the objects, should I do this in the finalize method?
> >
> > Thanks.
> >
> >

>
>



 
Reply With Quote
 
Scott Meddows
Guest
Posts: n/a
 
      31st Mar 2004
I'm okay with that. It's just unwiring the event handlers so it can get cleaned up properly.

Thanks for the help!

"CJ Taylor" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> Sorry... I apologize for the weak response. Yeah, I do some of my stuff in
> the finalizer. Just don't depend on anything else with it. That is, you
> don't know exactly when the finalizer is going to be called.
>
> -CJ
>
> "CJ Taylor" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > RemoveHandler
> >
> >
> > "Scott Meddows" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> > > I'm adding event handlers for dynamically loaded assemblies into a

> > program. I know that I must unregister those event handlers so
> > > the GC can pick up the objects, should I do this in the finalize method?
> > >
> > > Thanks.
> > >
> > >

> >
> >

>
>



 
Reply With Quote
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      1st Apr 2004
Scott,
> the GC can pick up the objects, should I do this in the finalize method?

No!

Three reasons:

First: Think about it, if the Finalize method is being called, then the GC
must have "picked up" the object.

Second: If the finalize method is being called on the object handling the
event, then the object offering the event may also be finalizable & already
finalized. In other words the object you are attempting to remove or remove
from may not exist!

Third: Finalize is to be used for unmanaged resources (Win32 file/GDI
handles), adding a Finalize method to classes can hurt performance, as those
objects will survive at least 2 collections of the garbage collector (adding
memory pressure).

Normally the object that called AddHandler is the object that I use to call
RemoveHandler as that is the object the "owns" the handler itself. For
example in a collection of objects that want to handle the events of
contained objects, I call AddHander in the collections Add method &
RemoveHandler in the Remove method. I may then have this collection
implement the IDisposable pattern & the Dispose method to remove all the
objects & their handlers, especially if the collection can go away without
having the objects go away.

Hope this helps
Jay



"Scott Meddows" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I'm adding event handlers for dynamically loaded assemblies into a

program. I know that I must unregister those event handlers so
> the GC can pick up the objects, should I do this in the finalize method?
>
> Thanks.
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
When does the Finalize event of a class fire? Barry Flynn Microsoft VB .NET 12 17th Jun 2008 05:18 AM
Event and Event Handlers. 00unknown.user00@gmail.com Microsoft C# .NET 2 10th Feb 2006 06:21 PM
Can I stop other event handlers from receiving an event? =?Utf-8?B?TXJOb2JvZHk=?= Microsoft C# .NET 3 13th Nov 2004 06:03 PM
Error handlers and Exception handlers =?Utf-8?B?c20=?= Microsoft VB .NET 4 30th Oct 2004 04:13 PM
Multiple event handlers for one event John S Microsoft C# .NET 3 9th Jul 2004 04:22 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:43 PM.