<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" 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:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>freachable.net - .NET Internals</title>
    <link>http://freachable.net/</link>
    <description>Next generation's garbage</description>
    <language>en-us</language>
    <copyright>Hafthor Stefansson</copyright>
    <lastBuildDate>Fri, 13 Nov 2009 23:41:25 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.1.8102.813</generator>
    <managingEditor>blog@freachable.net</managingEditor>
    <webMaster>blog@freachable.net</webMaster>
    <item>
      <trackback:ping>http://freachable.net/Trackback.aspx?guid=453a46fd-3285-411b-b7d8-386c1eee6033</trackback:ping>
      <pingback:server>http://freachable.net/pingback.aspx</pingback:server>
      <pingback:target>http://freachable.net/PermaLink,guid,453a46fd-3285-411b-b7d8-386c1eee6033.aspx</pingback:target>
      <dc:creator>Hafthor Stefansson</dc:creator>
      <wfw:comment>http://freachable.net/CommentView,guid,453a46fd-3285-411b-b7d8-386c1eee6033.aspx</wfw:comment>
      <wfw:commentRss>http://freachable.net/SyndicationService.asmx/GetEntryCommentsRss?guid=453a46fd-3285-411b-b7d8-386c1eee6033</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">There appears to be a bug in how Visual
Studio does "Add Service Reference" proxy generation for VB.NET projects.<br /><br />
I ran into this issue when trying to add a service reference to Bing Maps and I believe
it is not limited to that. I think it stems from it misunderstanding that enum instance
variables cannot be tested for equality using .Equals in VB... not sure why.<br /><br />
I think the solution is to simply replace the offending line:<br /><blockquote><font color="#0000ff">If <u>Me</u></font><u>.<i>enumfield</i>.Equals(value)</u> &lt;&gt; <font color="#0000ff">true
Then</font><br /></blockquote>with<br /><blockquote><font color="#0000ff">If Not Object<font color="#000000">.Equals(</font>Me</font>.<i>enumfield</i> ,Value) <font color="#0000ff">Then</font><br /></blockquote><br />
BTW: DO NOT compare boolean results to <font color="#0000ff">True </font>or <font color="#0000ff">False </font>as
they have done!<br /><br />
Use<br /><blockquote><font color="#0000ff">If </font><i>boolean_results</i><font color="#0000ff">Then</font><br /></blockquote>or<br /><blockquote><font color="#0000ff">If Not</font><i>boolean_results</i><font color="#0000ff">Then</font><br /></blockquote>[EDIT 2010-07-07]<br />
Specifically, this block of code is wrong:<br /><pre><span style="color: Black; background-color: Transparent; font-family: Courier New; font-size: 11px;">&lt;System.Runtime.Serialization.DataMemberAttribute()&gt;
_<br /><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">Public</span><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">Property</span> CompareOperator() <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">As</span> SearchService.CompareOperator<br /><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">Get</span><br /><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">Return</span><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">Me</span>.CompareOperatorField<br /><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">End</span><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">Get</span><br /><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">Set</span><br /><span style="background-color: rgb(255, 255, 204);"><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">If</span> (<span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">Me</span>.CompareOperatorField.Equals(value)
&lt;&gt; <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">true</span>) <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">Then</span></span><br /><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">Me</span>.CompareOperatorField <span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px;">=</span> value<br /><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">Me</span>.RaisePropertyChanged(<span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"CompareOperator"</span>)<br /><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">End</span><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">If</span><br /><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">End</span><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">Set</span><br /><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">End</span><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">Property</span></span></pre>and
should be replaced with this:<br /><pre><span style="color: Black; background-color: Transparent; font-family: Courier New; font-size: 11px;">&lt;System.Runtime.Serialization.DataMemberAttribute()&gt;
_<br /><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">Public</span><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">Property</span> CompareOperator() <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">As</span> SearchService.CompareOperator<br /><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">Get</span><br /><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">Return</span><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">Me</span>.CompareOperatorField<br /><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">End</span><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">Get</span><br /><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">Set</span><br /><span style="background-color: rgb(255, 255, 204);"><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">If
Not Object</span>.Equals(<span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">Me</span>.CompareOperatorField,value)<span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"></span><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">Then</span></span><br /><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">Me</span>.CompareOperatorField <span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px;">=</span> value<br /><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">Me</span>.RaisePropertyChanged(<span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"CompareOperator"</span>)<br /><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">End</span><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">If</span><br /><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">End</span><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">Set</span><br /><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">End</span><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">Property</span></span></pre>Made
this edit so search engines would pick it up... so I can find my own fix next time
I run into it. :) [/EDIT]<br /><p></p><img width="0" height="0" src="http://freachable.net/aggbug.ashx?id=453a46fd-3285-411b-b7d8-386c1eee6033" /></body>
      <title>Bug in Visual Studio 2008 Add Service Reference for VB.NET</title>
      <guid isPermaLink="false">http://freachable.net/PermaLink,guid,453a46fd-3285-411b-b7d8-386c1eee6033.aspx</guid>
      <link>http://freachable.net/2009/11/13/BugInVisualStudio2008AddServiceReferenceForVBNET.aspx</link>
      <pubDate>Fri, 13 Nov 2009 23:41:25 GMT</pubDate>
      <description>There appears to be a bug in how Visual Studio does "Add Service Reference" proxy generation for VB.NET projects.&lt;br&gt;
&lt;br&gt;
I ran into this issue when trying to add a service reference to Bing Maps and I believe
it is not limited to that. I think it stems from it misunderstanding that enum instance
variables cannot be tested for equality using .Equals in VB... not sure why.&lt;br&gt;
&lt;br&gt;
I think the solution is to simply replace the offending line:&lt;br&gt;
&lt;blockquote&gt;&lt;font color="#0000ff"&gt;If &lt;u&gt;Me&lt;/u&gt;&lt;/font&gt;&lt;u&gt;.&lt;i&gt;enumfield&lt;/i&gt;.Equals(value)&lt;/u&gt; &amp;lt;&amp;gt; &lt;font color="#0000ff"&gt;true
Then&lt;/font&gt;
&lt;br&gt;
&lt;/blockquote&gt;with&lt;br&gt;
&lt;blockquote&gt;&lt;font color="#0000ff"&gt;If Not Object&lt;font color="#000000"&gt;.Equals(&lt;/font&gt;Me&lt;/font&gt;.&lt;i&gt;enumfield&lt;/i&gt; ,Value) &lt;font color="#0000ff"&gt;Then&lt;/font&gt;
&lt;br&gt;
&lt;/blockquote&gt;
&lt;br&gt;
BTW: DO NOT compare boolean results to &lt;font color="#0000ff"&gt;True &lt;/font&gt;or &lt;font color="#0000ff"&gt;False &lt;/font&gt;as
they have done!&lt;br&gt;
&lt;br&gt;
Use&lt;br&gt;
&lt;blockquote&gt;&lt;font color="#0000ff"&gt;If &lt;/font&gt;&lt;i&gt;boolean_results&lt;/i&gt; &lt;font color="#0000ff"&gt;Then&lt;/font&gt;
&lt;br&gt;
&lt;/blockquote&gt;or&lt;br&gt;
&lt;blockquote&gt;&lt;font color="#0000ff"&gt;If Not&lt;/font&gt; &lt;i&gt;boolean_results&lt;/i&gt; &lt;font color="#0000ff"&gt;Then&lt;/font&gt;
&lt;br&gt;
&lt;/blockquote&gt;[EDIT 2010-07-07]&lt;br&gt;
Specifically, this block of code is wrong:&lt;br&gt;
&lt;pre&gt;&lt;span style="color: Black; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;&amp;lt;System.Runtime.Serialization.DataMemberAttribute()&amp;gt;
_&lt;br&gt;
&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;Public&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;Property&lt;/span&gt; CompareOperator() &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;As&lt;/span&gt; SearchService.CompareOperator&lt;br&gt;
&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;Get&lt;/span&gt;
&lt;br&gt;
&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;Return&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;Me&lt;/span&gt;.CompareOperatorField&lt;br&gt;
&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;End&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;Get&lt;/span&gt;
&lt;br&gt;
&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;Set&lt;/span&gt;
&lt;br&gt;
&lt;span style="background-color: rgb(255, 255, 204);"&gt;&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;If&lt;/span&gt; (&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;Me&lt;/span&gt;.CompareOperatorField.Equals(value)
&amp;lt;&amp;gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;true&lt;/span&gt;) &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;Then&lt;/span&gt;&lt;/span&gt;
&lt;br&gt;
&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;Me&lt;/span&gt;.CompareOperatorField &lt;span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; value&lt;br&gt;
&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;Me&lt;/span&gt;.RaisePropertyChanged(&lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"CompareOperator"&lt;/span&gt;)&lt;br&gt;
&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;End&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;If&lt;/span&gt;
&lt;br&gt;
&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;End&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;Set&lt;/span&gt;
&lt;br&gt;
&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;End&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;Property&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;and
should be replaced with this:&lt;br&gt;
&lt;pre&gt;&lt;span style="color: Black; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;&amp;lt;System.Runtime.Serialization.DataMemberAttribute()&amp;gt;
_&lt;br&gt;
&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;Public&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;Property&lt;/span&gt; CompareOperator() &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;As&lt;/span&gt; SearchService.CompareOperator&lt;br&gt;
&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;Get&lt;/span&gt;
&lt;br&gt;
&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;Return&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;Me&lt;/span&gt;.CompareOperatorField&lt;br&gt;
&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;End&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;Get&lt;/span&gt;
&lt;br&gt;
&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;Set&lt;/span&gt;
&lt;br&gt;
&lt;span style="background-color: rgb(255, 255, 204);"&gt;&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;If
Not Object&lt;/span&gt;.Equals(&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;Me&lt;/span&gt;.CompareOperatorField,value)&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;Then&lt;/span&gt;&lt;/span&gt;
&lt;br&gt;
&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;Me&lt;/span&gt;.CompareOperatorField &lt;span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; value&lt;br&gt;
&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;Me&lt;/span&gt;.RaisePropertyChanged(&lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"CompareOperator"&lt;/span&gt;)&lt;br&gt;
&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;End&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;If&lt;/span&gt;
&lt;br&gt;
&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;End&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;Set&lt;/span&gt;
&lt;br&gt;
&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;End&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;Property&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;Made
this edit so search engines would pick it up... so I can find my own fix next time
I run into it. :) [/EDIT]&lt;br&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://freachable.net/aggbug.ashx?id=453a46fd-3285-411b-b7d8-386c1eee6033" /&gt;</description>
      <comments>http://freachable.net/CommentView,guid,453a46fd-3285-411b-b7d8-386c1eee6033.aspx</comments>
      <category>.NET Internals</category>
      <category>Bing Maps</category>
      <category>Visual Studio</category>
    </item>
    <item>
      <trackback:ping>http://freachable.net/Trackback.aspx?guid=81445717-f038-4a59-a30b-a22d2eb75dac</trackback:ping>
      <pingback:server>http://freachable.net/pingback.aspx</pingback:server>
      <pingback:target>http://freachable.net/PermaLink,guid,81445717-f038-4a59-a30b-a22d2eb75dac.aspx</pingback:target>
      <dc:creator>Hafthor Stefansson</dc:creator>
      <wfw:comment>http://freachable.net/CommentView,guid,81445717-f038-4a59-a30b-a22d2eb75dac.aspx</wfw:comment>
      <wfw:commentRss>http://freachable.net/SyndicationService.asmx/GetEntryCommentsRss?guid=81445717-f038-4a59-a30b-a22d2eb75dac</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">I'm actually going to encourage you to
consider using GC methods. Well, now hang on cowboys... this is for pretty limited
circumstances.<br /><br />
You see, the GC class can do more than compact memory. It can also run finalizers
on unreferenced objects. If you are writing a class that included a finalizer (which
you would only need if your class is the lowest-level class to deal with an unmanaged
resource or are managing a pool of scarce resources) then you might consider using
GC. Even though you might (read should) implement IDisposable, if a user of your library
fails to Dispose your object, that unmanaged resource will leak until, at some non-deterministic
(read random) point, the garbage collector runs and that object's finalizer might
(read random) be queued and run. If it leaks often enough, your attempt to create
additional objects with the unmanaged resource could fail because you have exhausted
the available supply.<br /><br />
Rather than fail, you could attempt:<br /><b><br />
 GC.Collect(0); 
<br />
 GC.WaitForPendingFinalizers(); 
<br /></b><br />
and try again. If it works on the retry, you might want to log that for the developer's
information. One might argue that this is just encouraging and forgiving sloppiness,
but I look at it as a step towards robustness and can actually help the developer
diagnose a real problem whereas not doing this would just lead to more random run-time
only-on-heavy-load failures.<br /><br />
If you wanted to be really awesome, once you detect that you could recover, flip a
bit on to start recording a stacktrace on each create, then in your finalizer, put
that stacktrace in a application-wide queue that then gets logged by the next create.
Now you've told the dev exactly where to go find the spot where they got your object
but didn't dispose it.<br /><br />
Another place is when you are calling something that has an unmanaged resource behind
it along with other code that isn't behaving. For example, you are writing a web part
in SharePoint that is calling new SPSite and OpenWeb... perhaps you are being a good
SPRequest citizen, but another web part on the site that you didn't write isn't being
so nice.<br /><br />
You could make auto-recovery wrappers for the stuff you use. Your newSPSite method
would attempt to return new SPSite but if that should fail, Collect + WFPF and try
again. Ugh, what a pain. But sometimes it beats trying to explain to someone why it
is another webpart's fault that your webpart explodes.<br /><p></p><img width="0" height="0" src="http://freachable.net/aggbug.ashx?id=81445717-f038-4a59-a30b-a22d2eb75dac" /></body>
      <title>GC.Heresy</title>
      <guid isPermaLink="false">http://freachable.net/PermaLink,guid,81445717-f038-4a59-a30b-a22d2eb75dac.aspx</guid>
      <link>http://freachable.net/2009/10/20/GCHeresy.aspx</link>
      <pubDate>Tue, 20 Oct 2009 04:40:14 GMT</pubDate>
      <description>I'm actually going to encourage you to consider using GC methods. Well, now hang on cowboys... this is for pretty limited circumstances.&lt;br&gt;
&lt;br&gt;
You see, the GC class can do more than compact memory. It can also run finalizers
on unreferenced objects. If you are writing a class that included a finalizer (which
you would only need if your class is the lowest-level class to deal with an unmanaged
resource or are managing a pool of scarce resources) then you might consider using
GC. Even though you might (read should) implement IDisposable, if a user of your library
fails to Dispose your object, that unmanaged resource will leak until, at some non-deterministic
(read random) point, the garbage collector runs and that object's finalizer might
(read random) be queued and run. If it leaks often enough, your attempt to create
additional objects with the unmanaged resource could fail because you have exhausted
the available supply.&lt;br&gt;
&lt;br&gt;
Rather than fail, you could attempt:&lt;br&gt;
&lt;b&gt;
&lt;br&gt;
&amp;nbsp;GC.Collect(0); 
&lt;br&gt;
&amp;nbsp;GC.WaitForPendingFinalizers(); 
&lt;br&gt;
&lt;/b&gt;
&lt;br&gt;
and try again. If it works on the retry, you might want to log that for the developer's
information. One might argue that this is just encouraging and forgiving sloppiness,
but I look at it as a step towards robustness and can actually help the developer
diagnose a real problem whereas not doing this would just lead to more random run-time
only-on-heavy-load failures.&lt;br&gt;
&lt;br&gt;
If you wanted to be really awesome, once you detect that you could recover, flip a
bit on to start recording a stacktrace on each create, then in your finalizer, put
that stacktrace in a application-wide queue that then gets logged by the next create.
Now you've told the dev exactly where to go find the spot where they got your object
but didn't dispose it.&lt;br&gt;
&lt;br&gt;
Another place is when you are calling something that has an unmanaged resource behind
it along with other code that isn't behaving. For example, you are writing a web part
in SharePoint that is calling new SPSite and OpenWeb... perhaps you are being a good
SPRequest citizen, but another web part on the site that you didn't write isn't being
so nice.&lt;br&gt;
&lt;br&gt;
You could make auto-recovery wrappers for the stuff you use. Your newSPSite method
would attempt to return new SPSite but if that should fail, Collect + WFPF and try
again. Ugh, what a pain. But sometimes it beats trying to explain to someone why it
is another webpart's fault that your webpart explodes.&lt;br&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://freachable.net/aggbug.ashx?id=81445717-f038-4a59-a30b-a22d2eb75dac" /&gt;</description>
      <comments>http://freachable.net/CommentView,guid,81445717-f038-4a59-a30b-a22d2eb75dac.aspx</comments>
      <category>.NET Internals</category>
    </item>
    <item>
      <trackback:ping>http://freachable.net/Trackback.aspx?guid=a84dd6c8-7046-4934-9d09-40bd69bcad8d</trackback:ping>
      <pingback:server>http://freachable.net/pingback.aspx</pingback:server>
      <pingback:target>http://freachable.net/PermaLink,guid,a84dd6c8-7046-4934-9d09-40bd69bcad8d.aspx</pingback:target>
      <dc:creator>Hafthor Stefansson</dc:creator>
      <wfw:comment>http://freachable.net/CommentView,guid,a84dd6c8-7046-4934-9d09-40bd69bcad8d.aspx</wfw:comment>
      <wfw:commentRss>http://freachable.net/SyndicationService.asmx/GetEntryCommentsRss?guid=a84dd6c8-7046-4934-9d09-40bd69bcad8d</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">SharePoint has a great API for most things,
but some of things seem a bit lacking.<br /><br />
I recently had a need to auto-create folders inside a document library. Basically,
like mkdir with command extensions does on the command line. If directory x exists,
but x\y doesn't, mkdir x\y\z will mkdir x\y and mkdir x\y\z.<br /><br />
After some struggling, here's what I ended up with:<br /><br />
    <font color="#0000ff">Public Shared Sub</font> CreateDocLibFolders(<font color="#0000ff">ByVal </font>url <font color="#0000ff">As
String</font>)<br />
        <font color="#0000ff">Using</font> site <font color="#0000ff">As </font>New
SPSite(url)<br />
            <font color="#0000ff">Using </font>web <font color="#0000ff">As </font>SPWeb
= site.OpenWeb()<br />
                <font color="#0000ff">If </font>url.StartsWith(web.Url) <font color="#0000ff">Then</font><br />
                    <font color="#0000ff">Dim </font>folder <font color="#0000ff">As </font>SPFolder
= web.RootFolder<br />
                    <font color="#0000ff">For
Each </font>segment <font color="#0000ff">As </font>String <font color="#0000ff">In </font>url.Substring(web.Url.Length
+ 1).Split(<font color="#a52a2a">"/"</font>)<br />
                        <font color="#0000ff">Dim </font>nextfolder <font color="#0000ff">As </font>SPFolder
= web.GetFolder(folder.Url &amp; IIf(folder.Url.Length &gt; 0, <font color="#a52a2a">"/"</font>, <font color="#a52a2a">""</font>)
&amp; segment)<br />
                        <font color="#0000ff">If
Not</font> nextfolder.Exists <font color="#0000ff">Then</font><br />
                           
folder = folder.SubFolders.Add(nextfolder.Name)<br /><font color="#0000ff">                       
Else</font><br />
                           
folder = nextfolder<br /><font color="#0000ff">                       
End If<br />
                   
Next<br />
               
End If<br />
            End Using<br />
        End Using<br />
    End Sub</font><br /><br />
Okay, I know it doesn't look like much, but that code represents some hard won knowledge.
The main thing was dealing with folders that had spaces in them. Before, I was System.Web.HttpUtility.UrlDecode()ing
them, but my use of .Name on a .Exists=false proved to be much more elegant. Even
though I'm doing a GetFolder on a escaped url, the .Name property returns a nice,
suitable for passing into SubFolders.Add(), de%20ed name. But why use this ugly indirect
path string building technique? Well, I could not find an exception-free way of doing
existence testing of a folder other than web.GetFolder() which, of course, needs a
web relative url -- this lead to the more clunky looking GetFolder expression.<br /><blockquote><font color="#0000ff">RaiseEvent </font>OnReligiousArgument<br /></blockquote>For the two people that read this blog (me and uh, that might be an exagerated
figure) the reason my snippets have been in VB.NET lately is because I think VB.NET
is underloved in the SharePoint community. VSEWSS, for example, is a C# only club.
Heck with that. It doesn't provide enough juice to justify the opaqueness of its wsp
builder. VB.NET has some nice features and I like taking advantage of them -- I know
both cold and I make no apologies for choosing VB.NET. If you are one of those C#
1337ists... run the benchmarks and tell me its so much better. Tell me what it does
so much better that it makes up for the utter lack of exception filters, XML literals
(VB9), pleasant event raising, beyond 1980s-era switch/select case, optional parameters,
automatic by-reference parameters for callers, array resizing, and procedure scoped
static vars. I'm not saying C# is worse -- it's like the difference between ibuprofen
and acetaminophen. Minor advantages in some edge cases both ways but they all fix
most headaches.<br /><br />
I've seen some comparisons between the two languages before on the net, but none seemed
entirely complete. If I ever am put in jail with nothing but a toilet and a laptop
for a few years, perhaps I'd blog /the/ definitive list.<br /><br />
For those of you thinking of writing in your reasons why C# is soooo superior to VB.NET,
please refer to the <a href="http://en.wikipedia.org/wiki/Logical_fallacy">Logical
Fallacy article on Wikipedia</a> before embarrassing yourself. Reasons like "real
programmers use semicolons" or "only a moron would use a language with the word 'basic'
in the name" will be ridiculed mercilessly. There are some valid arguments for C#
and one could make an equally compelling valid case for it over VB.NET.<br /><p></p><img width="0" height="0" src="http://freachable.net/aggbug.ashx?id=a84dd6c8-7046-4934-9d09-40bd69bcad8d" /></body>
      <title>SharePoint document library folders / Language rant</title>
      <guid isPermaLink="false">http://freachable.net/PermaLink,guid,a84dd6c8-7046-4934-9d09-40bd69bcad8d.aspx</guid>
      <link>http://freachable.net/2008/06/19/SharePointDocumentLibraryFoldersLanguageRant.aspx</link>
      <pubDate>Thu, 19 Jun 2008 09:20:09 GMT</pubDate>
      <description>SharePoint has a great API for most things, but some of things seem a bit lacking.&lt;br&gt;
&lt;br&gt;
I recently had a need to auto-create folders inside a document library. Basically,
like mkdir with command extensions does on the command line. If directory x exists,
but x\y doesn't, mkdir x\y\z will mkdir x\y and mkdir x\y\z.&lt;br&gt;
&lt;br&gt;
After some struggling, here's what I ended up with:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color="#0000ff"&gt;Public Shared Sub&lt;/font&gt; CreateDocLibFolders(&lt;font color="#0000ff"&gt;ByVal &lt;/font&gt;url &lt;font color="#0000ff"&gt;As
String&lt;/font&gt;)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color="#0000ff"&gt;Using&lt;/font&gt; site &lt;font color="#0000ff"&gt;As &lt;/font&gt;New
SPSite(url)&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; &lt;font color="#0000ff"&gt;Using &lt;/font&gt;web &lt;font color="#0000ff"&gt;As &lt;/font&gt;SPWeb
= site.OpenWeb()&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;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color="#0000ff"&gt;If &lt;/font&gt;url.StartsWith(web.Url) &lt;font color="#0000ff"&gt;Then&lt;/font&gt;
&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color="#0000ff"&gt;Dim &lt;/font&gt;folder &lt;font color="#0000ff"&gt;As &lt;/font&gt;SPFolder
= web.RootFolder&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color="#0000ff"&gt;For
Each &lt;/font&gt;segment &lt;font color="#0000ff"&gt;As &lt;/font&gt;String &lt;font color="#0000ff"&gt;In &lt;/font&gt;url.Substring(web.Url.Length
+ 1).Split(&lt;font color="#a52a2a"&gt;"/"&lt;/font&gt;)&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color="#0000ff"&gt;Dim &lt;/font&gt;nextfolder &lt;font color="#0000ff"&gt;As &lt;/font&gt;SPFolder
= web.GetFolder(folder.Url &amp;amp; IIf(folder.Url.Length &amp;gt; 0, &lt;font color="#a52a2a"&gt;"/"&lt;/font&gt;, &lt;font color="#a52a2a"&gt;""&lt;/font&gt;)
&amp;amp; segment)&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color="#0000ff"&gt;If
Not&lt;/font&gt; nextfolder.Exists &lt;font color="#0000ff"&gt;Then&lt;/font&gt;
&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
folder = folder.SubFolders.Add(nextfolder.Name)&lt;br&gt;
&lt;font color="#0000ff"&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
Else&lt;/font&gt;
&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
folder = nextfolder&lt;br&gt;
&lt;font color="#0000ff"&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
End If&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
Next&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;
End If&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; End Using&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; End Using&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; End Sub&lt;/font&gt;
&lt;br&gt;
&lt;br&gt;
Okay, I know it doesn't look like much, but that code represents some hard won knowledge.
The main thing was dealing with folders that had spaces in them. Before, I was System.Web.HttpUtility.UrlDecode()ing
them, but my use of .Name on a .Exists=false proved to be much more elegant. Even
though I'm doing a GetFolder on a escaped url, the .Name property returns a nice,
suitable for passing into SubFolders.Add(), de%20ed name. But why use this ugly indirect
path string building technique? Well, I could not find an exception-free way of doing
existence testing of a folder other than web.GetFolder() which, of course, needs a
web relative url -- this lead to the more clunky looking GetFolder expression.&lt;br&gt;
&lt;blockquote&gt;&lt;font color="#0000ff"&gt;RaiseEvent &lt;/font&gt;OnReligiousArgument&lt;br&gt;
&lt;/blockquote&gt;For the two people that read this blog (me and uh, that might be an exagerated
figure) the reason my snippets have been in VB.NET lately is because I think VB.NET
is underloved in the SharePoint community. VSEWSS, for example, is a C# only club.
Heck with that. It doesn't provide enough juice to justify the opaqueness of its wsp
builder. VB.NET has some nice features and I like taking advantage of them -- I know
both cold and I make no apologies for choosing VB.NET. If you are one of those C#
1337ists... run the benchmarks and tell me its so much better. Tell me what it does
so much better that it makes up for the utter lack of exception filters, XML literals
(VB9), pleasant event raising, beyond 1980s-era switch/select case, optional parameters,
automatic by-reference parameters for callers, array resizing, and procedure scoped
static vars. I'm not saying C# is worse -- it's like the difference between ibuprofen
and acetaminophen. Minor advantages in some edge cases both ways but they all fix
most headaches.&lt;br&gt;
&lt;br&gt;
I've seen some comparisons between the two languages before on the net, but none seemed
entirely complete. If I ever am put in jail with nothing but a toilet and a laptop
for a few years, perhaps I'd blog /the/ definitive list.&lt;br&gt;
&lt;br&gt;
For those of you thinking of writing in your reasons why C# is soooo superior to VB.NET,
please refer to the &lt;a href="http://en.wikipedia.org/wiki/Logical_fallacy"&gt;Logical
Fallacy article on Wikipedia&lt;/a&gt; before embarrassing yourself. Reasons like "real
programmers use semicolons" or "only a moron would use a language with the word 'basic'
in the name" will be ridiculed mercilessly. There are some valid arguments for C#
and one could make an equally compelling valid case for it over VB.NET.&lt;br&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://freachable.net/aggbug.ashx?id=a84dd6c8-7046-4934-9d09-40bd69bcad8d" /&gt;</description>
      <comments>http://freachable.net/CommentView,guid,a84dd6c8-7046-4934-9d09-40bd69bcad8d.aspx</comments>
      <category>.NET Internals</category>
      <category>SharePoint</category>
    </item>
    <item>
      <trackback:ping>http://freachable.net/Trackback.aspx?guid=5a2b553a-d905-4454-bcec-6af14021828a</trackback:ping>
      <pingback:server>http://freachable.net/pingback.aspx</pingback:server>
      <pingback:target>http://freachable.net/PermaLink,guid,5a2b553a-d905-4454-bcec-6af14021828a.aspx</pingback:target>
      <dc:creator>Hafthor Stefansson</dc:creator>
      <wfw:comment>http://freachable.net/CommentView,guid,5a2b553a-d905-4454-bcec-6af14021828a.aspx</wfw:comment>
      <wfw:commentRss>http://freachable.net/SyndicationService.asmx/GetEntryCommentsRss?guid=5a2b553a-d905-4454-bcec-6af14021828a</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Everyone knows exception handling is relatively expensive, but it is important to
keep in mind where the cost lies. It is primarily in the throw action. The price of
a try is nearly zero when no exceptions occur.
</p>
        <p>
In the IL instruction stream the only difference between a wrapped block of code and
plain is a LEAVE instruction that jumps around the catch block. The method has an
exception table that marks the region of code protected. What happens when we
actually time the difference? The result is extremely small -- and strangely enough,
try/catch in the loop is slightly faster for C#! Because the numbers we are dealing
with are so small, however, I would suggest that this is coincidental noise due to
instruction alignment or something like that. I would guess that the cost of try is
essentially crushed out by the JIT compiler optimizer.
</p>
        <p>
The real expensive operation is throwing exceptions. How expensive? My tests show
it's about 36 microseconds each. Slow by comparison, but maybe not as slow as
you thought. As expected VB's exception handling was a little bit slower (~1%), due
no doubt to SetProjectError calls that are inserted before throws/rethrows.
</p>
        <p>
I always had an assumption that THROW (rethrow) was cheaper than THROW ex or THROW
NEW. I reasoned that the stackframe would not have to be gathered during rethrow.
Timing it shows rethrow is slightly slower.
</p>
        <p>
I learned you have to be pretty careful about timing code. For example, a significant
amount of time is burned JITing when a function is hit the first time, and that first
exceptions are really expensive. Firsts should be burned off before timing begins.
</p>
        <p>
Try out the code attached and post your results and please give environment.
</p>
        <p>
Here's my numbers on a Dell Latitude D830 1.7GHz 64-bit laptop w/ 4GB RAM.
</p>
        <p>
&gt;CSharp.exe 10000000000 100000<br />
trycount=10,000,000,000, throwcount=100,000<br />
Loop without try/catch: 31132 milliseconds<br />
Loop with try/catch: 21251 milliseconds<br />
difference: -9881 milliseconds<br />
throw new with 5 throw ex: 21448 milliseconds<br />
throw new with 5 rethrows: 21370 milliseconds<br />
difference: 78 milliseconds
</p>
        <p>
&gt;VB.exe 10000000000 100000<br />
trycount=10,000,000,000, throwcount=100,000<br />
Loop without try/catch: 23361 milliseconds<br />
Loop with try/catch: 30739 milliseconds<br />
difference: 7378 milliseconds<br />
throw new with 5 throw ex: 21617 milliseconds<br />
throw new with 5 rethrows: 21845 milliseconds<br />
difference: -228 milliseconds
</p>
Attached: <a href="http://freachable.net/content/binary/TestExceptionHandlingInLoop.rar">TestExceptionHandlingInLoop.rar
(12.5 KB)</a><img width="0" height="0" src="http://freachable.net/aggbug.ashx?id=5a2b553a-d905-4454-bcec-6af14021828a" /></body>
      <title>Try { Assert(TryBlocks&gt;=Expensive); } Catch(Exception ex) { assumption.reset(); }</title>
      <guid isPermaLink="false">http://freachable.net/PermaLink,guid,5a2b553a-d905-4454-bcec-6af14021828a.aspx</guid>
      <link>http://freachable.net/2007/11/10/TryAssertTryBlocksExpensiveCatchExceptionExAssumptionreset.aspx</link>
      <pubDate>Sat, 10 Nov 2007 20:59:25 GMT</pubDate>
      <description>&lt;p&gt;
Everyone knows exception handling is relatively expensive, but it is important to
keep in mind where the cost lies. It is primarily in the throw action. The price of
a try is nearly zero when no exceptions occur.
&lt;/p&gt;
&lt;p&gt;
In the IL instruction stream the only difference between a wrapped block of code and
plain is a LEAVE instruction that jumps around the catch block. The method has an
exception table that marks the region of code protected.&amp;nbsp;What happens when we
actually time the difference? The result is extremely small -- and strangely enough,
try/catch in the loop is slightly faster for C#! Because the numbers we are dealing
with are so small, however, I would suggest that this is coincidental noise due to
instruction alignment or something like that. I would guess that the cost of try is
essentially crushed out by the JIT compiler optimizer.
&lt;/p&gt;
&lt;p&gt;
The real expensive operation is throwing exceptions. How expensive? My tests show
it's about&amp;nbsp;36 microseconds each. Slow by comparison, but maybe not as slow as
you thought. As expected VB's exception handling was a little bit slower (~1%), due
no doubt to SetProjectError calls that are inserted before throws/rethrows.
&lt;/p&gt;
&lt;p&gt;
I always had an assumption that THROW (rethrow) was cheaper than THROW ex or THROW
NEW. I reasoned that the stackframe would not have to be gathered during rethrow.
Timing it shows rethrow is slightly slower.
&lt;/p&gt;
&lt;p&gt;
I learned you have to be pretty careful about timing code. For example,&amp;nbsp;a significant
amount of time is burned JITing when a function is hit the first time, and that first
exceptions are really expensive. Firsts should be burned off before timing begins.
&lt;/p&gt;
&lt;p&gt;
Try out the code attached and post your results and please give environment.
&lt;/p&gt;
&lt;p&gt;
Here's my numbers on a Dell Latitude D830 1.7GHz 64-bit laptop w/ 4GB RAM.
&lt;/p&gt;
&lt;p&gt;
&amp;gt;CSharp.exe 10000000000 100000&lt;br&gt;
trycount=10,000,000,000, throwcount=100,000&lt;br&gt;
Loop without try/catch: 31132 milliseconds&lt;br&gt;
Loop with try/catch: 21251 milliseconds&lt;br&gt;
difference: -9881 milliseconds&lt;br&gt;
throw new with 5 throw ex: 21448 milliseconds&lt;br&gt;
throw new with 5 rethrows: 21370 milliseconds&lt;br&gt;
difference: 78 milliseconds
&lt;/p&gt;
&lt;p&gt;
&amp;gt;VB.exe 10000000000 100000&lt;br&gt;
trycount=10,000,000,000, throwcount=100,000&lt;br&gt;
Loop without try/catch: 23361 milliseconds&lt;br&gt;
Loop with try/catch: 30739 milliseconds&lt;br&gt;
difference: 7378 milliseconds&lt;br&gt;
throw new with 5 throw ex: 21617 milliseconds&lt;br&gt;
throw new with 5 rethrows: 21845 milliseconds&lt;br&gt;
difference: -228 milliseconds
&lt;/p&gt;
Attached: &lt;a href="http://freachable.net/content/binary/TestExceptionHandlingInLoop.rar"&gt;TestExceptionHandlingInLoop.rar
(12.5 KB)&lt;/a&gt;&lt;img width="0" height="0" src="http://freachable.net/aggbug.ashx?id=5a2b553a-d905-4454-bcec-6af14021828a" /&gt;</description>
      <comments>http://freachable.net/CommentView,guid,5a2b553a-d905-4454-bcec-6af14021828a.aspx</comments>
      <category>.NET Internals</category>
    </item>
    <item>
      <trackback:ping>http://freachable.net/Trackback.aspx?guid=ede09ebe-dfd6-4016-a3d4-c2ae4f980eca</trackback:ping>
      <pingback:server>http://freachable.net/pingback.aspx</pingback:server>
      <pingback:target>http://freachable.net/PermaLink,guid,ede09ebe-dfd6-4016-a3d4-c2ae4f980eca.aspx</pingback:target>
      <dc:creator>Hafthor Stefansson</dc:creator>
      <wfw:comment>http://freachable.net/CommentView,guid,ede09ebe-dfd6-4016-a3d4-c2ae4f980eca.aspx</wfw:comment>
      <wfw:commentRss>http://freachable.net/SyndicationService.asmx/GetEntryCommentsRss?guid=ede09ebe-dfd6-4016-a3d4-c2ae4f980eca</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Last month I was working with some code that used System.Xml.Xsl.XslCompiledTransform.
I knew it involved compiling stuff (where did I get that idea?) and making some IL
dynamically to run. Neat-o. This month on my road to getting MCPD by month-end,
I was reading about RegEx. It didn't really dawn on me before that it could compile
the expression vs. interpret it. Pretty cool. But I was always skeptical about these
because I had heard that you can Assembly.Load, but there's no such thing as Assembly.Unload...
so I thought that this meant these would be memory leaks... but, no. I read (<a href="http://blogs.msdn.com/joelpob/archive/2004/04/01/105862.aspx">http://blogs.msdn.com/joelpob/archive/2004/04/01/105862.aspx</a>)
about something called LGC (lightweight code-gen) and DynamicMethod. I was reading
this (<a href="http://blogs.msdn.com/bclteam/archive/2004/11/12/256783.aspx">http://blogs.msdn.com/bclteam/archive/2004/11/12/256783.aspx</a>)
and read that memory leak saga about regular expressions but "<em>We've fixed that
problem in Whidbey</em>" caught my attention. They did? Yes, DynamicMethod does by
making it so code can be emitted and executed from the managed heap - very cool. I
had to know if XslCompiledTransform used this, so I reflected across Microsoft.*.dll
and System.*.dll to see what uses DynamicMethod... Yes, XslCompiledTransform does.
Nice - but I'm not sure it uses that exclusively. Given it actually spits out .dll
files, I have to assume it Assembly.Loads and executes them... We avoided any potential
leaks in a web app by holding the tranforms in the Application dictionary. This also
made the whole thing faster since each transform was only compiled once.
</p>
        <p>
By the way, I would encourage .NET devs out there to look in to getting certified
(MCTS or MCPD). Don't be a wuss and shape-test your way through. (Shape-test refers
to the process of getting test guides that so exactly mirror the real test that you
can just memorize the shape of the correct answer to ace it. - I bet if given those
kind of study guides written in Thai, I could pass the test. That's bull-<a href="http://web.utk.edu/~wratchuk/learningthai/mar3.html">khrap</a>.)
</p>
        <img width="0" height="0" src="http://freachable.net/aggbug.ashx?id=ede09ebe-dfd6-4016-a3d4-c2ae4f980eca" />
      </body>
      <title>System.Reflection.Emit.DynamicMethod</title>
      <guid isPermaLink="false">http://freachable.net/PermaLink,guid,ede09ebe-dfd6-4016-a3d4-c2ae4f980eca.aspx</guid>
      <link>http://freachable.net/2007/10/18/SystemReflectionEmitDynamicMethod.aspx</link>
      <pubDate>Thu, 18 Oct 2007 08:42:13 GMT</pubDate>
      <description>&lt;p&gt;
Last month I was working with some code that used System.Xml.Xsl.XslCompiledTransform.
I knew it involved compiling stuff (where did I get that idea?) and making some IL
dynamically to run. Neat-o.&amp;nbsp;This month on my road to getting MCPD by month-end,
I was reading about RegEx. It didn't really dawn on me before that it could compile
the expression vs. interpret it. Pretty cool. But I was always skeptical about these
because I had heard that you can Assembly.Load, but there's no such thing as Assembly.Unload...
so I thought that this meant these would be memory leaks... but, no. I read (&lt;a href="http://blogs.msdn.com/joelpob/archive/2004/04/01/105862.aspx"&gt;http://blogs.msdn.com/joelpob/archive/2004/04/01/105862.aspx&lt;/a&gt;)
about something called LGC (lightweight code-gen) and DynamicMethod. I was reading
this (&lt;a href="http://blogs.msdn.com/bclteam/archive/2004/11/12/256783.aspx"&gt;http://blogs.msdn.com/bclteam/archive/2004/11/12/256783.aspx&lt;/a&gt;)
and read that memory leak saga about regular expressions but "&lt;em&gt;We've fixed that
problem in Whidbey&lt;/em&gt;" caught my attention. They did? Yes, DynamicMethod does by
making it so code can be emitted and executed from the managed heap - very cool. I
had to know if XslCompiledTransform used this, so I reflected across Microsoft.*.dll
and System.*.dll to see what uses DynamicMethod... Yes, XslCompiledTransform does.
Nice - but I'm not sure it uses that exclusively. Given it actually spits out .dll
files, I have to assume it Assembly.Loads and executes them... We avoided any potential
leaks in a web app by holding the tranforms in the Application dictionary. This also
made the whole thing faster since each transform was only compiled once.
&lt;/p&gt;
&lt;p&gt;
By the way, I would encourage .NET devs out there to look in to getting certified
(MCTS or MCPD). Don't be a wuss and shape-test your way through. (Shape-test refers
to the process of getting test guides that so exactly mirror the real test that you
can just memorize the shape of the correct answer to ace it. - I bet if given those
kind of study guides written in Thai, I could pass the test. That's bull-&lt;a href="http://web.utk.edu/~wratchuk/learningthai/mar3.html"&gt;khrap&lt;/a&gt;.)
&lt;/p&gt;
&lt;img width="0" height="0" src="http://freachable.net/aggbug.ashx?id=ede09ebe-dfd6-4016-a3d4-c2ae4f980eca" /&gt;</description>
      <comments>http://freachable.net/CommentView,guid,ede09ebe-dfd6-4016-a3d4-c2ae4f980eca.aspx</comments>
      <category>.NET Internals</category>
      <category>Certification</category>
    </item>
    <item>
      <trackback:ping>http://freachable.net/Trackback.aspx?guid=e07a3cc3-80f1-49b2-aff0-f0f655881bee</trackback:ping>
      <pingback:server>http://freachable.net/pingback.aspx</pingback:server>
      <pingback:target>http://freachable.net/PermaLink,guid,e07a3cc3-80f1-49b2-aff0-f0f655881bee.aspx</pingback:target>
      <dc:creator>Hafthor Stefansson</dc:creator>
      <wfw:comment>http://freachable.net/CommentView,guid,e07a3cc3-80f1-49b2-aff0-f0f655881bee.aspx</wfw:comment>
      <wfw:commentRss>http://freachable.net/SyndicationService.asmx/GetEntryCommentsRss?guid=e07a3cc3-80f1-49b2-aff0-f0f655881bee</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Well, after much procrastination, I finally spent some quality time with the GoDaddy
hosting control center and various dasBlog instructions and got it working. 
</p>
        <p>
          <img alt="" src="http://freachable.net/content/binary/101807_0653_Welcometofr1.png" align="right" />For
anyone trying the same... 
</p>
        <ol style="MARGIN-LEFT: 54pt">
          <li>
From GoDaddy, go to "My Hosting Account". 
</li>
          <li>
Click the [open] link under "Control Panel" for the domain. 
</li>
          <li>
Under settings, select ASP.NET runtime and make sure it's 2.0. 
</li>
          <li>
Under settings, check FrontPage extensions and make sure they are NOT installed. 
</li>
          <li>
Select "Directory Management" under "Content". 
</li>
          <li>
Create a custom directory where you'll host the blog with "Read", "Web" and "Set Root". 
</li>
          <li>
Create the subdirectories SiteConfig and logs and give them each read+write access. 
</li>
          <li>
Create another one called content and give it read+write+web access. 
</li>
          <li>
Download the dasBlog webfiles zip and extract it locally. 
</li>
          <li>
Edit your siteconfig/site.config setting the obvious looking stuff. 
</li>
          <li>
Edit your /web.config to get rid of or comment out the line<br />
&lt;trust level="Medium" originUrl="" /&gt; (a GoDaddy-ism) 
</li>
          <li>
Copy all the extracted files to your blog directory (using explorer view on ftp://
works) 
</li>
          <li>
Point your browser at your blog. 
</li>
          <li>
Sign in using "admin", "admin" (unless you changed the user name in SiteConfig/siteSecurity.config
before you uploaded it) 
</li>
          <li>
Immediately change that password. 
</li>
        </ol>
        <p>
** Disclaimer: I didn't try those steps exactly so they may not be entirely perfect.
** 
</p>
        <p>
Edit: Okay, I just setup <a href="http://isakur.com">another blog</a> using those
instructions and they seemed to work nicely.
</p>
        <p>
Look here for more help on this. 
</p>
        <p>
          <a href="http://www.aakashjain.com/How+To+Setup+DasBlog+On+Godaddy.aspx">http://www.aakashjain.com/How+To+Setup+DasBlog+On+Godaddy.aspx</a>
        </p>
        <p>
Now, to blog from Word 2007 with images… create from the blog post template, set up
an account of type "Other" and set the Url to http://domain/blogdir/blogger.aspx,
set the picture option to "my blog provider". Publish! It should automatically upload
pictures to /content/binary/. 
</p>
        <p>
Okay, so what is freachable? It is purgatory for .NET objects with finalizers. During
garbage collection, objects that have finalizers need to have their finalizers run,
but the GC shouldn't have to wait for them (after all the entire process is stalled
while GC runs and finalizer code could affect what the GC is doing), but if there
are no references to keep the object alive, it will get collected so the object is
made to reachable by the finalizer thread by placing it in its special queue called
freachable. GC completes and the finalizer thread wakes up, executes the finalizers
in the freachable queue, and clears the queue entry, which is the last remaining reference
to the object and now the next time GC runs, the object's memory will be reclaimed. 
</p>
        <p>
Read all about GC at <a href="http://msdn.microsoft.com/msdnmag/issues/1100/gci/">http://msdn.microsoft.com/msdnmag/issues/1100/gci/</a></p>
        <p>
According to that article, it is pronounced f-reachable. That just sounds l-ame. 
</p>
        <p>
So what's the point of this blog? To blog stuff I figure out. I will try not to waste
bandwidth on "hey, go read what this other guy figured out". Okay, so most of this
post is stuff that someone else figured out, but the web.config mod on dasBlog was
not in Aakash's notes.
</p>
        <img width="0" height="0" src="http://freachable.net/aggbug.ashx?id=e07a3cc3-80f1-49b2-aff0-f0f655881bee" />
      </body>
      <title>Welcome to freachable.net – blogging with Word 2007 on dasBlog on GoDaddy</title>
      <guid isPermaLink="false">http://freachable.net/PermaLink,guid,e07a3cc3-80f1-49b2-aff0-f0f655881bee.aspx</guid>
      <link>http://freachable.net/2007/10/18/WelcomeToFreachablenetBloggingWithWord2007OnDasBlogOnGoDaddy.aspx</link>
      <pubDate>Thu, 18 Oct 2007 06:53:53 GMT</pubDate>
      <description>&lt;p&gt;
Well, after much procrastination, I finally spent some quality time with the GoDaddy
hosting control center and various dasBlog instructions and got it working. 
&lt;/p&gt;
&lt;p&gt;
&lt;img alt="" src="http://freachable.net/content/binary/101807_0653_Welcometofr1.png" align=right&gt;For
anyone trying the same... 
&lt;/p&gt;
&lt;ol style="MARGIN-LEFT: 54pt"&gt;
&lt;li&gt;
From GoDaddy, go to "My Hosting Account". 
&lt;li&gt;
Click the [open] link under "Control Panel" for the domain. 
&lt;li&gt;
Under settings, select ASP.NET runtime and make sure it's 2.0. 
&lt;li&gt;
Under settings, check FrontPage extensions and make sure they are NOT installed. 
&lt;li&gt;
Select "Directory Management" under "Content". 
&lt;li&gt;
Create a custom directory where you'll host the blog with "Read", "Web" and "Set Root". 
&lt;li&gt;
Create the subdirectories SiteConfig and logs and give them each read+write access. 
&lt;li&gt;
Create another one called content and give it read+write+web access. 
&lt;li&gt;
Download the dasBlog webfiles zip and extract it locally. 
&lt;li&gt;
Edit your siteconfig/site.config setting the obvious looking stuff. 
&lt;li&gt;
Edit your /web.config to get rid of or comment out the line&lt;br&gt;
&amp;lt;trust level="Medium" originUrl="" /&amp;gt; (a GoDaddy-ism) 
&lt;li&gt;
Copy all the extracted files to your blog directory (using explorer view on ftp://
works) 
&lt;li&gt;
Point your browser at your blog. 
&lt;li&gt;
Sign in using "admin", "admin" (unless you changed the user name in SiteConfig/siteSecurity.config
before you uploaded it) 
&lt;li&gt;
Immediately change that password. 
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
** Disclaimer: I didn't try those steps exactly so they may not be entirely perfect.
** 
&lt;/p&gt;
&lt;p&gt;
Edit: Okay, I just setup &lt;a href="http://isakur.com"&gt;another blog&lt;/a&gt; using those
instructions and they seemed to work nicely.
&lt;/p&gt;
&lt;p&gt;
Look here for more help on this. 
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.aakashjain.com/How+To+Setup+DasBlog+On+Godaddy.aspx"&gt;http://www.aakashjain.com/How+To+Setup+DasBlog+On+Godaddy.aspx&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
Now, to blog from Word 2007 with images… create from the blog post template, set up
an account of type "Other" and set the Url to http://domain/blogdir/blogger.aspx,
set the picture option to "my blog provider". Publish! It should automatically upload
pictures to /content/binary/. 
&lt;/p&gt;
&lt;p&gt;
Okay, so what is freachable? It is purgatory for .NET objects with finalizers. During
garbage collection, objects that have finalizers need to have their finalizers run,
but the GC shouldn't have to wait for them (after all the entire process is stalled
while GC runs and finalizer code could affect what the GC is doing), but if there
are no references to keep the object alive, it will get collected so the object is
made to reachable by the finalizer thread by placing it in its special queue called
freachable. GC completes and the finalizer thread wakes up, executes the finalizers
in the freachable queue, and clears the queue entry, which is the last remaining reference
to the object and now the next time GC runs, the object's memory will be reclaimed. 
&lt;/p&gt;
&lt;p&gt;
Read all about GC at &lt;a href="http://msdn.microsoft.com/msdnmag/issues/1100/gci/"&gt;http://msdn.microsoft.com/msdnmag/issues/1100/gci/&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
According to that article, it is pronounced f-reachable. That just sounds l-ame. 
&lt;/p&gt;
&lt;p&gt;
So what's the point of this blog? To blog stuff I figure out. I will try not to waste
bandwidth on "hey, go read what this other guy figured out". Okay, so most of this
post is stuff that someone else figured out, but the web.config mod on dasBlog was
not in Aakash's notes.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://freachable.net/aggbug.ashx?id=e07a3cc3-80f1-49b2-aff0-f0f655881bee" /&gt;</description>
      <comments>http://freachable.net/CommentView,guid,e07a3cc3-80f1-49b2-aff0-f0f655881bee.aspx</comments>
      <category>.NET Internals</category>
      <category>dasBlog</category>
      <category>Office 12</category>
    </item>
  </channel>
</rss>