Hi,
I would like to start using Custom Events in some of my applications however I am a little confused about the when to Unsubscribe from them.
For example, say I have a web based application which has several modules. Module 1 manages a Taxi driver and their shift, Module 2 manages expenses and Module 3 manages inventory. So my taxi drive goes to work and fills up the tank with gas. When he comes back to base, he fills a form from Module 1 in that says he bought some gas and spent $50.
I want Module 2 and Module 3 are interested to hear of this expenditure and need to subscribe to an OnFuelPurchased event.
That part I get, as the Module 1 page loads, I subscribe the Module 2 and 3 event handlers. No worries.
What I don't understand is where do I unsubscribe from the events? I read that I need to so I don't have memory leaks but don't see where I do it?
Thanks
Jeff
Loading
Sam HobbsPosted Jul 6, 2011, 8:08 PM
Subscribing is essentially just adding an item to a list. I however can't find the documentation that says that. There are many collections in .Net and the .Net garbage collector is designed to do cleanup for us. Unless you need to cancel a subscription for some other reason, I am sure not aware of a need to unsubscribe. In other words, if you need the event to be handled until the object is destroyed, then you don't need to unsubscribe at all.
It is possible for a sub form to subscribe to events of a main form. So if the sub form closes then it makes sense for the sub form to unsubscribe to the event(s) of the main form. It is situations such as that for which unsubscribing makes sense.
Jaganathan BantheswaranPosted Jul 6, 2011, 1:52 AM
Unsubscribe the event from page unload..