<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:media="http://search.yahoo.com/mrss/" xmlns:evnet="http://www.mscommunities.com/rssmodule/"><channel><title>Comment Feed for Event disposal (TechOff on Channel 9)</title><atom:link rel="self" type="application/rss+xml" href="http://channel9.msdn.com/forums/techoff/148255-event-disposal/rss/default.aspx" /><image><url>http://mschnlnine.vo.llnwd.net/d1/Dev/App_Themes/C9/images/feedimage.png</url><title>Comment Feed for Event disposal (TechOff on Channel 9)</title><link>http://channel9.msdn.com/forums/TechOff/148255-Event-disposal/</link></image><description>Event disposal</description><link>http://channel9.msdn.com/forums/TechOff/148255-Event-disposal/</link><language>en-us</language><pubDate>Tue, 11 Apr 2006 12:37:06 GMT</pubDate><lastBuildDate>Tue, 11 Apr 2006 12:37:06 GMT</lastBuildDate><generator>EvNet (EvNet, Version=1.0.3243.35083, Culture=neutral, PublicKeyToken=null)</generator><item><title>Re: Event disposal</title><description>Is any of that strictly necessary?&lt;br /&gt;&lt;br /&gt;Wouldn't the garbage collector destroy the object regardless of whether anything was listening for the events? I can't see why the GC wouldn't destroy your object - unless a subscriber holds a reference to the object to which it is subscribed. Which would be stupid - and against the whole point of events in the first place.&lt;br /&gt;&lt;br /&gt;In which case what do you gain? I can't see any benefit - and if you want to know when an object has been disposed of, then you could just add an event to tell you. In fact how would this help in that case either?</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/148255-Event-disposal/?CommentID=176726</link><pubDate>Tue, 11 Apr 2006 12:37:06 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/148255-Event-disposal/?CommentID=176726</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/176726/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>Is any of that strictly necessary?Wouldn't the garbage collector destroy the object regardless of whether anything was listening for the events? I can't see why the GC wouldn't destroy your object - unless a subscriber holds a reference to the object to which it is subscribed. Which would be stupid&amp;#8230;</evnet:previewtext><dc:creator>Massif</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/176726/Trackback.aspx</trackback:ping></item><item><title>Re: Event disposal</title><description>&lt;p&gt;I'm still interested in learning more about how this code works.&amp;nbsp; Can you 1) provide a sample application&amp;nbsp;and 2) explain how this could be used to handle delegates.&amp;nbsp; Does GetEvents() return an instance's delegates as well?&lt;br /&gt;&lt;br /&gt;Thanks,&lt;br /&gt;&lt;br /&gt;Paul&lt;/p&gt;</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/148255-Event-disposal/?CommentID=176464</link><pubDate>Mon, 10 Apr 2006 19:53:51 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/148255-Event-disposal/?CommentID=176464</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/176464/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>I'm still interested in learning more about how this code works.&amp;nbsp; Can you 1) provide a sample application&amp;nbsp;and 2) explain how this could be used to handle delegates.&amp;nbsp; Does GetEvents() return an instance's delegates as well?Thanks,Paul</evnet:previewtext><dc:creator>pauldaly1982</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/176464/Trackback.aspx</trackback:ping></item><item><title>Re: Event disposal</title><description>I'm interested in how this code works.&amp;nbsp; Do you mind adding some more comments and/or add a dummy class that inherits from your BaseObject class.&lt;br /&gt;&lt;br /&gt;Will this&amp;nbsp;remove user created events (not just the default events that are available with the control)?&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Thanks,&lt;br /&gt;&lt;br /&gt;- Paul&lt;br /&gt;</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/148255-Event-disposal/?CommentID=167005</link><pubDate>Tue, 14 Mar 2006 13:57:22 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/148255-Event-disposal/?CommentID=167005</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/167005/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>I'm interested in how this code works.&amp;nbsp; Do you mind adding some more comments and/or add a dummy class that inherits from your BaseObject class.Will this&amp;nbsp;remove user created events (not just the default events that are available with the control)?Thanks,- Paul</evnet:previewtext><dc:creator>pauldaly1982</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/167005/Trackback.aspx</trackback:ping></item><item><title>Re: Event disposal</title><description>&lt;blockquote&gt;
				&lt;div&gt;makemaster wrote:&lt;/div&gt;
				&lt;div&gt;Good work ilya! You certainly solved my problem.&lt;/div&gt;
		&lt;/blockquote&gt;
		&lt;br /&gt;
		&lt;br /&gt;No problem, it's nice to know that this has helped someone. :D</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/148255-Event-disposal/?CommentID=149545</link><pubDate>Mon, 09 Jan 2006 14:21:10 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/148255-Event-disposal/?CommentID=149545</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/149545/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>	makemaster wrote:
				Good work ilya! You certainly solved my problem.
		
		
		No problem, it's nice to know that this has helped someone. :D</evnet:previewtext><dc:creator>ilya</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/149545/Trackback.aspx</trackback:ping></item><item><title>Re: Event disposal</title><description>Good work ilya! You certainly solved my problem.</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/148255-Event-disposal/?CommentID=149531</link><pubDate>Mon, 09 Jan 2006 13:22:16 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/148255-Event-disposal/?CommentID=149531</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/149531/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>Good work ilya! You certainly solved my problem.</evnet:previewtext><dc:creator>makemaster</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/149531/Trackback.aspx</trackback:ping></item><item><title>Re: Event disposal</title><description>&lt;p&gt;Well I don't know if anyone is interested in this but I have finally found a solution for this.&lt;/p&gt;
&lt;p&gt;public class BaseObject&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public event EventHandler Event1;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;private BindingFlags flags = BindingFlags.NonPublic&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;|BindingFlags.Instance |&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;BindingFlags.Public;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public BaseObject(){}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public void ReleaseEvents()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;//Get all of the events and then use the&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;DeclaringType&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;property to get the instance of the fields&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;EventInfo[] events = this.GetType().GetEvents(flags);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;if(events == null)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;if(events.Length &amp;lt; 1)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;//Store All the FieldInfo objects in a HashTable&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;System.Collections.Hashtable ht = new&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;System.Collections.Hashtable();&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;for(int i = 0; i &amp;lt; events.Length; i++)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//Get all of the fields for the selected declared type&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;FieldInfo[] fields = events[i].DeclaringType.GetFields(flags);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;foreach(FieldInfo fi in fields)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if(events[i].Name.Equals(fi.Name) &amp;amp;&amp;amp; !ht.Contains(fi.Name))&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ht.Add(fi.Name,fi);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;System.Collections.IDictionaryEnumerator en = ht.GetEnumerator();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;while(en.MoveNext())&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;FieldInfo f = en.Value as FieldInfo;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;MulticastDelegate e = f.GetValue(this) as MulticastDelegate;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if(e != null)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;foreach(Delegate del in e.GetInvocationList())&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;getEvent(f.Name,f.DeclaringType).GetRemoveMethod().Invoke(this,new object[]{del});&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;}&lt;br /&gt;private EventInfo getEvent(string name, Type t)&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;if(name == null)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return null;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;if(t == null)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return null;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;return t.GetEvent(name,flags);&lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;public string Name&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;get{return Guid.NewGuid().ToString();}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;set&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Event1(this,null);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;}&lt;/p&gt;</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/148255-Event-disposal/?CommentID=148658</link><pubDate>Fri, 06 Jan 2006 17:57:38 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/148255-Event-disposal/?CommentID=148658</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/148658/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>Well I don't know if anyone is interested in this but I have finally found a solution for this.
public class BaseObject{&amp;nbsp;&amp;nbsp;&amp;nbsp;public event EventHandler Event1;&amp;nbsp;&amp;nbsp;&amp;nbsp;private BindingFlags flags = BindingFlags.NonPublic&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;|BindingFlags.Instance&amp;#8230;</evnet:previewtext><dc:creator>ilya</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/148658/Trackback.aspx</trackback:ping></item><item><title>Re: Event disposal</title><description>&lt;blockquote&gt;
				&lt;div&gt;ilya wrote:&lt;/div&gt;
				&lt;div&gt;
						&lt;br /&gt;1) Sometimes I will want to know if an object is being deleted/disposed because I am listening for that also...&lt;br /&gt;&lt;/div&gt;
		&lt;/blockquote&gt;
		&lt;br /&gt;
		&lt;br /&gt;on a slippery slope with that kinda requirement in the managed world... i would just use VC++.NET 2005 and delete the managed object, makes me feel better, but even then you wouldnt put a mortgage on it not being around.:P</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/148255-Event-disposal/?CommentID=148379</link><pubDate>Thu, 05 Jan 2006 22:03:57 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/148255-Event-disposal/?CommentID=148379</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/148379/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>	ilya wrote:
				
						1) Sometimes I will want to know if an object is being deleted/disposed because I am listening for that also...
		
		
		on a slippery slope with that kinda requirement in the managed world... i would just use VC++.NET 2005 and delete the managed object, makes me feel&amp;#8230;</evnet:previewtext><dc:creator>Andrew Webber FX</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/148379/Trackback.aspx</trackback:ping></item><item><title>Re: Event disposal</title><description>&lt;blockquote&gt;
				&lt;div&gt;ScanIAm wrote:&lt;/div&gt;
				&lt;div&gt;
&lt;blockquote&gt;
&lt;table&gt;

&lt;tr&gt;
&lt;td&gt;&lt;img src="http://channel9.msdn.com/Themes/AlmostGlass/images/icon-quote.gif /&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;ilya wrote:&lt;/strong&gt;

&lt;i&gt; 
&lt;p&gt;If you read what i wrote above "I don't want to explicitly specify/set the events to null for every object" &lt;br /&gt;Reason being is that if I have a lot of objects that I will create&amp;nbsp;that will inherit from the BaseObject I don't want to specifically set the Event Fields to null. Especially during the call to the Dispose method I want to be able to call this "ReleaseEvents" method to unhook all the event starting from the BaseObject and all the way up the Inheritance chain.&lt;/p&gt;&lt;/i&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/blockquote&gt;&lt;br /&gt;&lt;br /&gt;1) There is no reason for you to have to null out your events.&amp;nbsp; The subscribers will not know that your object has disappeared nor will they care.&lt;br /&gt;&lt;br /&gt;2) If you do decide to set the events to null, you should simply clear the events that are explicitly created by the child object in the code.&amp;nbsp; Then call the parent dispose function.&amp;nbsp; This is called inheritance.&lt;br /&gt;&lt;br /&gt;3) See #1.&amp;nbsp; You are wasting your time worrying about this.&lt;/div&gt;
		&lt;/blockquote&gt;
		&lt;br /&gt;
		&lt;br /&gt;1) Sometimes I will want to know if an object is being deleted/disposed because I am listening for that also.&lt;br /&gt;2) Once again I'll say it, I don't want to explicitly remove event handlers from the child objects because I might have a lot of them and in the future people might add more objects and if that happends I don't want them to be responsible for removing event handlers.&lt;br /&gt;3) I just wanted to know if there is a good way that people know about to do this, but if no one really knows then I will have to come up with my own way of doing this.&lt;br /&gt;&lt;br /&gt;</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/148255-Event-disposal/?CommentID=148371</link><pubDate>Thu, 05 Jan 2006 21:49:24 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/148255-Event-disposal/?CommentID=148371</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/148371/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>	ScanIAm wrote:
				





ilya wrote:

 
If you read what i wrote above "I don't want to explicitly specify/set the events to null for every object" Reason being is that if I have a lot of objects that I will create&amp;nbsp;that will inherit from the BaseObject I don't want to specifically&amp;#8230;</evnet:previewtext><dc:creator>ilya</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/148371/Trackback.aspx</trackback:ping></item><item><title>Re: Event disposal</title><description>&lt;blockquote&gt;
				&lt;div&gt;ilya wrote:&lt;/div&gt;
				&lt;div&gt;
&lt;p&gt;If you read what i wrote above "I don't want to explicitly specify/set the events to null for every object" &lt;br /&gt;Reason being is that if I have a lot of objects that I will create&amp;nbsp;that will inherit from the BaseObject I don't want to specifically set the Event Fields to null. Especially during the call to the Dispose method I want to be able to call this "ReleaseEvents" method to unhook all the event starting from the BaseObject and all the way up the Inheritance chain.&lt;/p&gt;&lt;/div&gt;
		&lt;/blockquote&gt;
		&lt;br /&gt;
		&lt;br /&gt;1) There is no reason for you to have to null out your events.&amp;nbsp; The subscribers will not know that your object has disappeared nor will they care.&lt;br /&gt;&lt;br /&gt;2) If you do decide to set the events to null, you should simply clear the events that are explicitly created by the child object in the code.&amp;nbsp; Then call the parent dispose function.&amp;nbsp; This is called inheritance.&lt;br /&gt;&lt;br /&gt;3) See #1.&amp;nbsp; You are wasting your time worrying about this.</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/148255-Event-disposal/?CommentID=148367</link><pubDate>Thu, 05 Jan 2006 21:35:11 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/148255-Event-disposal/?CommentID=148367</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/148367/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>	ilya wrote:
				
If you read what i wrote above "I don't want to explicitly specify/set the events to null for every object" Reason being is that if I have a lot of objects that I will create&amp;nbsp;that will inherit from the BaseObject I don't want to specifically set the Event Fields to null.&amp;#8230;</evnet:previewtext><dc:creator>ScanIAm</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/148367/Trackback.aspx</trackback:ping></item><item><title>Re: Event disposal</title><description>&lt;p&gt;If you read what i wrote above "I don't want to explicitly specify/set the events to null for every object" &lt;br /&gt;Reason being is that if I have a lot of objects that I will create&amp;nbsp;that will inherit from the BaseObject I don't want to specifically set the Event Fields to null. Especially during the call to the Dispose method I want to be able to call this "ReleaseEvents" method to unhook all the event starting from the BaseObject and all the way up the Inheritance chain.&lt;/p&gt;</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/148255-Event-disposal/?CommentID=148287</link><pubDate>Thu, 05 Jan 2006 19:08:51 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/148255-Event-disposal/?CommentID=148287</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/148287/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>If you read what i wrote above "I don't want to explicitly specify/set the events to null for every object" Reason being is that if I have a lot of objects that I will create&amp;nbsp;that will inherit from the BaseObject I don't want to specifically set the Event Fields to null. Especially during the&amp;#8230;</evnet:previewtext><dc:creator>ilya</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/148287/Trackback.aspx</trackback:ping></item><item><title>Re: Event disposal</title><description>&lt;p&gt;You dont need to explicitly remove each event handler.&lt;br /&gt;&lt;br /&gt;Here is an example of the Dispose &amp;amp; Event pattern.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;// Design pattern for a base class.&lt;br /&gt;public class Base: IDisposable&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp; public event EventHandler BaseEvent;&lt;br /&gt;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp; protected virtual void OnBaseEvent()&lt;br /&gt;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (BaseEvent != null)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; BaseEvent(this, EventArgs.Empty);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp; //Implement IDisposable.&lt;br /&gt;&amp;nbsp;&amp;nbsp; public void Dispose() &lt;br /&gt;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Dispose(true);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; GC.SuppressFinalize(this); &lt;br /&gt;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; protected virtual void Dispose(bool disposing) &lt;br /&gt;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (disposing) &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Free other state (managed objects).&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (BaseEvent != null)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; BaseEvent = null;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Free your own state (unmanaged objects).&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Set large fields to null.&lt;br /&gt;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; // Use C# destructor syntax for finalization code.&lt;br /&gt;&amp;nbsp;&amp;nbsp; ~Base()&lt;br /&gt;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Simply call Dispose(false).&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Dispose (false);&lt;br /&gt;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp; &lt;br /&gt;// Design pattern for a derived class.&lt;br /&gt;public class Derived: Base&lt;br /&gt;{&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp; public event EventHandler DerivedEvent;&lt;br /&gt;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp; protected virtual void OnDerivedEvent()&lt;br /&gt;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (DerivedEvent != null)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; DerivedEvent(this, EventArgs.Empty);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp; protected override void Dispose(bool disposing) &lt;br /&gt;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (disposing) &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Release managed resources.&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if(DerivedEvent != null)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; DerivedEvent = null;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Release unmanaged resources.&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Set large fields to null.&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Call Dispose on your base class.&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; base.Dispose(disposing);&lt;br /&gt;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp; // The derived class does not have a Finalize method&lt;br /&gt;&amp;nbsp;&amp;nbsp; // or a Dispose method with parameters because it inherits&lt;br /&gt;&amp;nbsp;&amp;nbsp; // them from the base class.&lt;br /&gt;}&lt;/p&gt;</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/148255-Event-disposal/?CommentID=148284</link><pubDate>Thu, 05 Jan 2006 17:30:47 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/148255-Event-disposal/?CommentID=148284</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/148284/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>You dont need to explicitly remove each event handler.Here is an example of the Dispose &amp;amp; Event pattern.// Design pattern for a base class.public class Base: IDisposable{&amp;nbsp;&amp;nbsp; public event EventHandler BaseEvent;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; protected virtual void OnBaseEvent()&amp;nbsp;&amp;nbsp;&amp;#8230;</evnet:previewtext><dc:creator>sprayer</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/148284/Trackback.aspx</trackback:ping></item><item><title>Re: Event disposal</title><description>&lt;blockquote&gt;
				&lt;div&gt;sprayer wrote:&lt;/div&gt;
				&lt;div&gt;Follow the Finalize &amp;amp; Dispose pattern&lt;br /&gt;&lt;br /&gt;&lt;a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconFinalizeDispose.asp" target="_blank"&gt;http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconFinalizeDispose.asp&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;At the URL there is an example of a implementing Dispose in a Base-Derived class.&lt;br /&gt;&lt;br /&gt;Tip:&lt;br /&gt;If your are using VS2005 you can create a Dispose Snippet to speed up the process and do most of the work for you.&lt;br /&gt;&lt;br /&gt;Later&lt;br /&gt;sprayer&lt;/div&gt;
		&lt;/blockquote&gt;
		&lt;br /&gt;
		&lt;br /&gt;The only problem is that I don't want to explicitly specify the removal of every event starting from the BaseObject all the way up the Inheritance chain.</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/148255-Event-disposal/?CommentID=148273</link><pubDate>Thu, 05 Jan 2006 16:56:26 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/148255-Event-disposal/?CommentID=148273</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/148273/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>	sprayer wrote:
				Follow the Finalize &amp;amp; Dispose patternhttp://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconFinalizeDispose.aspAt the URL there is an example of a implementing Dispose in a Base-Derived class.Tip:If your are using VS2005 you can create a Dispose&amp;#8230;</evnet:previewtext><dc:creator>ilya</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/148273/Trackback.aspx</trackback:ping></item><item><title>Re: Event disposal</title><description>Follow the Finalize &amp;amp; Dispose pattern&lt;br /&gt;&lt;br /&gt;&lt;a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconFinalizeDispose.asp"&gt;http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconFinalizeDispose.asp&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;At the URL there is an example of a implementing Dispose in a Base-Derived class.&lt;br /&gt;&lt;br /&gt;Tip:&lt;br /&gt;If your are using VS2005 you can create a Dispose Snippet to speed up the process and do most of the work for you.&lt;br /&gt;&lt;br /&gt;Later&lt;br /&gt;sprayer</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/148255-Event-disposal/?CommentID=148268</link><pubDate>Thu, 05 Jan 2006 16:37:57 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/148255-Event-disposal/?CommentID=148268</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/148268/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>Follow the Finalize &amp;amp; Dispose patternhttp://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconFinalizeDispose.aspAt the URL there is an example of a implementing Dispose in a Base-Derived class.Tip:If your are using VS2005 you can create a Dispose Snippet to speed up&amp;#8230;</evnet:previewtext><dc:creator>sprayer</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/148268/Trackback.aspx</trackback:ping></item><item><title>Re: Event disposal</title><description>&lt;blockquote&gt;
				&lt;div&gt;sprayer wrote:&lt;/div&gt;
				&lt;div&gt;
&lt;p&gt;Yes it is recommended to unhook your events in your dispose.&amp;nbsp; Just set your event to null in your base class dispose method.&amp;nbsp; Make sure you use the Dispose pattern. &lt;/p&gt;
&lt;p&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/p&gt;
&lt;p&gt;Later&lt;/p&gt;
&lt;p&gt;sprayer&lt;/p&gt;&lt;/div&gt;
		&lt;/blockquote&gt;
		&lt;br /&gt;
		&lt;br /&gt;That will work in my BaseObject class, but what if another class is inheriting from the BaseObject class and it has it's own events? I want to be able to call one method in my BaseObject class and dispose all the events starting from the BaseObject all the way up to the class that is inheriting from the BaseObject. I was trying to do this with Reflection but was not successful.&lt;br /&gt;</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/148255-Event-disposal/?CommentID=148266</link><pubDate>Thu, 05 Jan 2006 16:29:19 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/148255-Event-disposal/?CommentID=148266</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/148266/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>	sprayer wrote:
				
Yes it is recommended to unhook your events in your dispose.&amp;nbsp; Just set your event to null in your base class dispose method.&amp;nbsp; Make sure you use the Dispose pattern. 
&amp;nbsp;
Later
sprayer
		
		
		That will work in my BaseObject class, but what if another class&amp;#8230;</evnet:previewtext><dc:creator>ilya</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/148266/Trackback.aspx</trackback:ping></item><item><title>Re: Event disposal</title><description>&lt;blockquote&gt;
				&lt;div&gt;blowdart wrote:&lt;/div&gt;
				&lt;div&gt;Calling a dispose method does not release the object to the garbage collector, regardless of what you do inside that object.&lt;br /&gt;&lt;/div&gt;
		&lt;/blockquote&gt;
		&lt;br /&gt;
		&lt;br /&gt;Well what I ment is that if the object is not being used then the Garbage collection will remove that object, but if that object is referencing another object and in this case an event then I think that the Garbage Collector will not dispose this object. So what I am looking for is the best way of unhooking the references for all of events inside of the object.</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/148255-Event-disposal/?CommentID=148265</link><pubDate>Thu, 05 Jan 2006 16:24:39 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/148255-Event-disposal/?CommentID=148265</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/148265/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>	blowdart wrote:
				Calling a dispose method does not release the object to the garbage collector, regardless of what you do inside that object.
		
		
		Well what I ment is that if the object is not being used then the Garbage collection will remove that object, but if that object is&amp;#8230;</evnet:previewtext><dc:creator>ilya</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/148265/Trackback.aspx</trackback:ping></item><item><title>Re: Event disposal</title><description>&lt;p&gt;Yes it is recommended to unhook your events in your dispose.&amp;nbsp; Just set your event to null in your base class dispose method.&amp;nbsp; Make sure you use the Dispose pattern. &lt;/p&gt;
&lt;p&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/p&gt;
&lt;p&gt;Later&lt;/p&gt;
&lt;p&gt;sprayer&lt;/p&gt;</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/148255-Event-disposal/?CommentID=148264</link><pubDate>Thu, 05 Jan 2006 16:22:53 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/148255-Event-disposal/?CommentID=148264</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/148264/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>Yes it is recommended to unhook your events in your dispose.&amp;nbsp; Just set your event to null in your base class dispose method.&amp;nbsp; Make sure you use the Dispose pattern. 
&amp;nbsp;
Later
sprayer</evnet:previewtext><dc:creator>sprayer</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/148264/Trackback.aspx</trackback:ping></item><item><title>Re: Event disposal</title><description>Calling a dispose method does not release the object to the garbage collector, regardless of what you do inside that object.&lt;br /&gt;</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/148255-Event-disposal/?CommentID=148262</link><pubDate>Thu, 05 Jan 2006 16:19:11 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/148255-Event-disposal/?CommentID=148262</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/148262/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>Calling a dispose method does not release the object to the garbage collector, regardless of what you do inside that object.</evnet:previewtext><dc:creator>blowdart</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/148262/Trackback.aspx</trackback:ping></item></channel></rss>