473,728 Members | 1,480 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Given an 'event', how do I obtain underlying delegates ?

Hi,

Various custom controls in my WinForms application register for the
'Application.Id le' event on load, and unregister on dispose. To avoid memory
leaks, it's essential that I remember to unregister on dispose.

Before my application exits, I'd like to do an 'assert' to assure myself
that there are no cases in which I've forgotten to unregister. I was under
the impression that an event is nothing more than a collection of delegates.
I therefore tried:

Debug.Assert(Ap plication.Idle. Count==0);

This doesn't compile, however, and begs the question: Given an event
object, how do I obtain the underlying delegate collection?

Dave
Jan 18 '06 #1
12 1669
What makes you think you need to do that?

You don't.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.

"The One We Call 'Dave'" <gh****@englewo od.com> wrote in message
news:JY******** ************@gi ganews.com...
Hi,

Various custom controls in my WinForms application register for the
'Application.Id le' event on load, and unregister on dispose. To avoid
memory leaks, it's essential that I remember to unregister on dispose.

Before my application exits, I'd like to do an 'assert' to assure
myself that there are no cases in which I've forgotten to unregister. I
was under the impression that an event is nothing more than a collection
of delegates. I therefore tried:

Debug.Assert(Ap plication.Idle. Count==0);

This doesn't compile, however, and begs the question: Given an event
object, how do I obtain the underlying delegate collection?

Dave

Jan 18 '06 #2
"The One We Call 'Dave'" <gh****@englewo od.com> wrote in message
news:JY******** ************@gi ganews.com...
Given an event object, how do I obtain the underlying delegate collection?


I think something along these lines:

// Number of subscribers to the event:
int n = Idle.GetInvocat ionList().Lengt h;

- OR -

// Get at each subscriber:
System.Delegate[] DelegateList = Idle.GetInvocat ionList();
foreach (IdleDelegate Delegate in DelegateList) {
...
}
-- Alan
Jan 18 '06 #3
> "The One We Call 'Dave'" <gh****@englewo od.com> wrote in message
news:JY******** ************@gi ganews.com...
Given an event object, how do I obtain the underlying delegate
collection?

I think something along these lines:

// Number of subscribers to the event:
int n = Idle.GetInvocat ionList().Lengt h;
- OR -

// Get at each subscriber:
System.Delegate[] DelegateList = Idle.GetInvocat ionList();
foreach (IdleDelegate Delegate in DelegateList) {
...
}


Both of those will result in compiler error "The event...can only appear
on the left hand side of += or -=";

This is a fun one and I'm experimenting with it right now.

Chris
Jan 18 '06 #4
> What makes you think you need to do that?

You don't.


You most certainly do. 'Application.Id le' is a static event. If you fail
to unregister, the reference will be held in the event collection until the
Application terminates. This constitutes a memory leak. Just to confirm, I
looked up the event in MSDN and found this excerpt:

Because this is a static event, you must detach any event handlers
attached to this event in the ApplicationExit event. If you do not detach
these handlers, they will remain attached to the event and continue to
consume memory.
David
Jan 18 '06 #5
> Various custom controls in my WinForms application register for the
'Application.Id le' event on load, and unregister on dispose. To avoid memory
leaks, it's essential that I remember to unregister on dispose.
If the control is disposed of while the app still runs, yes.
Before my application exits, I'd like to do an 'assert' to assure myself
that there are no cases in which I've forgotten to unregister. I was under
the impression that an event is nothing more than a collection of delegates.
I therefore tried:
When your app exits, the Application.Idl e event goes with it.
Application.Idl e is 'just' WinForms' way of multiplexing the idle
proc. There should be no consequences of terminating an app with a
populated Application.Idl e.
Debug.Assert(Ap plication.Idle. Count==0);

This doesn't compile, however, and begs the question: Given an event
object, how do I obtain the underlying delegate collection?


So far as I know (and I just yesterday found how far from
comprehensive my understanding of events is) you don't: the only
things you can do with another class's event is += and -= it.

--
<http://www.midnightbea ch.com>
Jan 18 '06 #6
> I think something along these lines:

// Number of subscribers to the event:
int n = Idle.GetInvocat ionList().Lengt h;


Thanks for the reply.

I had a similar thought, but I get the following compilation error:

Error 1 The event 'System.Windows .Forms.Applicat ion.Idle' can only appear on
the left hand side of += or -=

David
Jan 18 '06 #7
>> What makes you think you need to do that?

You don't.

You most certainly do. 'Application.Id le' is a static event. If
you fail to unregister, the reference will be held in the event
collection until the Application terminates. This constitutes a memory
leak. Just to confirm, I looked up the event in MSDN and found this
excerpt:

Because this is a static event, you must detach any event handlers
attached to this event in the ApplicationExit event. If you do not
detach
these handlers, they will remain attached to the event and continue to
consume memory.
David


Take a look at this Dave.

I just tested it and it's golden!

http://tinyurl.com/ao252
Jan 18 '06 #8
> When your app exits, the Application.Idl e event goes with it.
Application.Idl e is 'just' WinForms' way of multiplexing the idle
proc. There should be no consequences of terminating an app with a
populated Application.Idl e.


Ah, now I see why Kevin made the comment that he did. I didn't express
myself clearly (sorry kevin).

I didn't mean to imply that there were negative consequences of terminating
the app with a bunch of Application.Idl e callbacks in place. The only reason
I want to perform the assert is to confirm with myself that I'm not
forgetting to unregster these handlers.

My WinForms app will be active for weeks at a time. Many controls will be
created and subsequently disposed during that period. If I forget to
unregister these handlers, the effect of the memory leak will build up over
the lifetime of the application session. I thought that running an assert
just prior to exit would be a clever way to determine during the development
phase whether I was failing to clean up any of these handlers.

David
Jan 18 '06 #9
> Take a look at this Dave.

I just tested it and it's golden!

http://tinyurl.com/ao252


Oh my! Well, I certainly wouldn't have guessed approach that on my own. :)
Thanks, I'll try it.
Jan 19 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2496
by: Ian Richardson | last post by:
I'm writing some code which does one thing when onreadystatechange occurs, e.g. handle.onreadystatechange = function() { blah(handle,other_params) }; ....but sometimes I need to add another, so it becomes: handle.onreadystatechange = function() { whatever_was_previously_defined(whatever);some_other_function(handle) };
2
3080
by: Lore Leuneog | last post by:
Hi. You can add new delegates for an event with += and you can remove delegates from an event by using: -= but is there a way (method) to list all delegates added to one event??? Sincerely Greetings Lore
6
4823
by: Ondrej Sevecek | last post by:
Hello, what is the difference between "event" and the only use of delegate? Why one should note events with "event" keyword when the functionality seems the same as with pure delegates? When I call some delegate, in which thread this delegate gets called - callers = synchronous, or some other thread = asynchronous? And in what thread is this done with "event"? Ondra.
3
2941
by: Bob | last post by:
C# newbie here.... studying about delegates and now events. I just don't get the purpose of declaring events using the event keyword. What does this do for me that can't be done using only delegates?
18
2036
by: Elder Hyde | last post by:
Hey all, A class of mine needs to tell the outside world when its buffer is not empty. The problem is that C# seems to force you to put the event-raising code in the base class. To illustrate, consider what I'll do in Java: public interface DataAvailabilityListener extends java.util.EventListener { void dataArrived(DataAvailabilityEvent event); }
3
2410
by: Chua Wen Ching | last post by:
Hi there, I just read Chris Sells's article at http://www.codeproject.com/csharp/delegate_bedtime.asp?df=100&forumid=2983&select=922269#xx922269xx I wonder i can do this: 1) I want to built in a class library that had multithreading enabled.
1
56000
by: MuZZy | last post by:
Hi, Is there a way to remove all event handlers for a control's event? Say, i have a button and i want to remove all button.Click events for it - i don't know how many of them was hooked to the event and what are the functions hooked, but i need to make sure that i unwired all of them at once. I could unwire event handlers if i knew the functions: button.Click += new EventHandler(OnMyBtnClick); // Wire event handler ....
27
1944
by: Codemonkey | last post by:
Heya All, Sorry, but I think it's about time for a monkey-ramble. I've just had enough of trying to serialize even simple objects with VB. A simple task you may think - stick the <Serialized()> attribute on the class and away you go. As Homer would say - "D'Oh" The root of my problem lies in the way VB implements Events and the fact that you can't apply the <NonSerialized> attribute to the little rascals.
5
8923
by: AliRezaGoogle | last post by:
Hi, I have a conceptual question on Events and Multicast Delegates. Let me explain: As we know an event is a multicast delegate. What we declare as an event is inherently a multicast delegate. I really do not undrestand what additional features the "event" keyword adds to multicast delegate. The only thing that I see as an additional feature is a "Thunder Icon" near event name in VS IDE when intellisense works;).
0
8897
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9416
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...
1
9194
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
9126
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
8123
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
4524
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...
0
4787
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3232
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
2159
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.