PC Review


Reply
Thread Tools Rate Thread

?? Changing Event Listeners But Allowing GC ??

 
 
Tom Baxter
Guest
Posts: n/a
 
      11th Sep 2008
Hi everyone,

I have a class (let's call it "X") that listens for Resize events on a
single Control (let's call it "C"). X should receive Resize events from
exactly one Control (not multiple Controls).

I have something like this in X:

C.Resize += delegate(object sender, EventArgs e) {
Control myControl = (Control)sender;

// do something with C's Location
};

Class X never maintains a strong reference to Control C and I don't want it
to because I don't want X to prevent C from being garbage collected. If C
goes out of scope and is eventually garbage collected, this will not affect
X's Resize handler. X will simply cease to receive Resize events for C,
which is fine.

Again, it's important that X *not* maintain a strong reference to C as I do
not want X to prevent C from being GCed.

So far, so good.

Now, sometimes I need class X to *stop* receiving Resize events on C and
instead receive Resize events on some other control (let's call it D). But
since X does not have a reference to C it cannot un-install its Resize
handler. In other words, X cannot tell C it is no longer interested in
Resize events. If X installs a Resize listener on D without uninstalling the
listener on C, it will receive Resize events for both C and D, which is
unacceptable.

The solution I'm considering is to maintain a weak reference in X to C using
System.WeakReference. This will allow X to remove its Resize handler on C
(if C has not been GCed) before installing a new listener on D.

Does this sound like a reasonable approach?

 
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
Delegation, Event-Listeners =?Utf-8?B?bWF0aG9u?= Microsoft Dot NET Framework 3 27th Oct 2005 11:01 PM
Delegation, Event-Listeners =?Utf-8?B?bWF0aG9u?= Microsoft Dot NET 1 26th Oct 2005 07:49 AM
Event Listeners on a protected chart James Martin Microsoft Excel Programming 1 5th Aug 2005 12:45 PM
Event listeners? Deena Microsoft C# .NET 4 21st Jul 2004 02:51 PM
How to check for event listeners? Max Microsoft VB .NET 9 30th Mar 2004 11:42 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:55 AM.