<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-8259613</id><updated>2026-01-29T20:19:33.249+01:00</updated><title type='text'>Kristof Verbiest - Bite-size C#</title><subtitle type='html'>A collection of C# tips and tricks</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://kristofverbiest.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8259613/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://kristofverbiest.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='http://www.blogger.com/feeds/8259613/posts/default?start-index=26&amp;max-results=25&amp;redirect=false'/><author><name>Kristof</name><uri>http://www.blogger.com/profile/01727380410232817527</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>29</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-8259613.post-2940932157868775913</id><published>2010-12-24T09:51:00.003+01:00</published><updated>2010-12-24T09:53:13.548+01:00</updated><title type='text'>Silverlight: loading a WriteableBitmap from file</title><content type='html'>&lt;p&gt;I’ve just spent quite some time trying to do something that seems very simple: embedding an image in a Silverlight application and loading this image into a WriteableBitmap (because I need to read the values of the individual pixels).&lt;/p&gt;&lt;p&gt;My first attempt was to add the image to my project as a resource and use the following code:&lt;/p&gt;&lt;div class=&quot;csharpcode&quot;&gt;&lt;pre class=&quot;alt&quot;&gt;Uri uri = &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; Uri(&lt;span class=&quot;str&quot;&gt;&quot;Images/Texture.jpg&quot;&lt;/span&gt;, UriKind.Relative);&lt;/pre&gt;

&lt;pre&gt;BitmapImage imageSource = &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; BitmapImage(uri);&lt;/pre&gt;

&lt;pre class=&quot;alt&quot;&gt;WriteableBitmap bitmap = &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; WriteableBitmap(imageSource);  // EXCEPTION&lt;/pre&gt;
&lt;/div&gt;
&lt;style type=&quot;text/css&quot;&gt;
.csharpcode, .csharpcode pre
{
 font-size: small;
 color: black;
 font-family: consolas, &quot;Courier New&quot;, courier, monospace;
 background-color: #ffffff;
 /*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
 background-color: #f4f4f4;
 width: 100%;
 margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;



&lt;p&gt;However, an exception is thrown on the last line. Luckily the internet came to the rescue, and &lt;a href=&quot;http://www.blog.ingenuitynow.net/Silverlight+Creating+A+WriteableBitmap+From+A+Uri+Source.aspx&quot; target=&quot;_blank&quot;&gt;these&lt;/a&gt; &lt;a href=&quot;http://www.i-programmer.info/programming/silverlight/1216-advanced-silverlight-bitmaps.html&quot; target=&quot;_blank&quot;&gt;two&lt;/a&gt; great site suggest basically the same solution:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Using BitmapCreateOptions.None to make sure the bitmap begins to load immediately.&lt;/li&gt;

&lt;li&gt;Subscribing to events to know when the image has actually loaded.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;However the proposed solution did not work in my case because the image fails to load with an AG_E_NETWORK_ERROR error.&lt;/p&gt;

&lt;p&gt;In the end I took a different approach: I load the resource using Application.GetResourceStream so I’m sure that the data is completely loaded before it is handed to the WriteableBitmap. This is the code:&lt;/p&gt;

&lt;div class=&quot;csharpcode&quot;&gt;
&lt;pre class=&quot;alt&quot;&gt;StreamResourceInfo sri = Application.GetResourceStream(&lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; Uri(&lt;span class=&quot;str&quot;&gt;&quot;Images/Texture.jpg&quot;&lt;/span&gt;, UriKind.Relative));&lt;/pre&gt;

&lt;pre&gt; &lt;/pre&gt;

&lt;pre class=&quot;alt&quot;&gt;BitmapImage src = &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; BitmapImage();&lt;/pre&gt;

&lt;pre&gt;src.SetSource(sri.Stream);&lt;/pre&gt;

&lt;pre class=&quot;alt&quot;&gt;WriteableBitmap image = &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; WriteableBitmap(src);&lt;/pre&gt;
&lt;/div&gt;
&lt;style type=&quot;text/css&quot;&gt;
.csharpcode, .csharpcode pre
{
 font-size: small;
 color: black;
 font-family: consolas, &quot;Courier New&quot;, courier, monospace;
 background-color: #ffffff;
 /*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
 background-color: #f4f4f4;
 width: 100%;
 margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;



&lt;p&gt;In my case it has the advantage of being synchronous which makes the overall code much simpler (but I understand that asynchronous behavior may often be better in a web-context). &lt;/p&gt;

&lt;p&gt;When adding your image to your Silverlight project, &lt;strong&gt;the build action should be set to ‘Content’ instead of ‘Resource’&lt;/strong&gt;. This means that the image will not be included in the dll, but it will be stored in the xap-file. You can check this by renaming the xap-file to zip and looking at the content.&lt;/p&gt;

&lt;p&gt;So there are some non-intuitive steps involved when trying to accomplish this simple task. Hopefully this post can be helpful if someone else has this problem.&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://kristofverbiest.blogspot.com/feeds/2940932157868775913/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8259613/2940932157868775913' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8259613/posts/default/2940932157868775913'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8259613/posts/default/2940932157868775913'/><link rel='alternate' type='text/html' href='http://kristofverbiest.blogspot.com/2010/12/silverlight-loading-writeablebitmap.html' title='Silverlight: loading a WriteableBitmap from file'/><author><name>Kristof</name><uri>http://www.blogger.com/profile/01727380410232817527</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8259613.post-2621772275824084548</id><published>2008-12-05T16:18:00.001+01:00</published><updated>2008-12-05T16:18:55.300+01:00</updated><title type='text'>Saving and restoring GDI+ state</title><content type='html'>&lt;p&gt;When you create complex drawing code, you probably will split this over multiple methods and pass the Graphics-object between these methods. But because the Graphics-object contains state, all changes that you apply in a method will be propagated to the other methods as well. Often this is not what you want.&lt;/p&gt;  &lt;p&gt;The common way to work around this is to reset all the changes that you made at the end of the method:&lt;/p&gt;  &lt;div style=&quot;border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; background-color: #f4f4f4; max-height: 200px&quot;&gt;   &lt;div style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;     &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   1:&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;void&lt;/span&gt; PaintBackGround(Graphics g)&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   2:&lt;/span&gt; {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   3:&lt;/span&gt;     SmoothingMode oldSmoothingMode = g.SmoothingMode;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   4:&lt;/span&gt;     &lt;span style=&quot;color: #0000ff&quot;&gt;try&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   5:&lt;/span&gt;     {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   6:&lt;/span&gt;         g.SmoothingMode = SmoothingMode.AntiAlias;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   7:&lt;/span&gt;         ...&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   8:&lt;/span&gt;     }&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   9:&lt;/span&gt;     &lt;span style=&quot;color: #0000ff&quot;&gt;finally&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  10:&lt;/span&gt;     {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  11:&lt;/span&gt;         g.SmoothingMode = oldSmoothingMode;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  12:&lt;/span&gt;     }&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  13:&lt;/span&gt; }&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;But if your code changes a lot of properties this will become cumbersome. In these cases it is simpler to save and restore the entire Graphics-state like this:&lt;/p&gt;

&lt;div style=&quot;border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; background-color: #f4f4f4; max-height: 200px&quot;&gt;
  &lt;div style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;
    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   1:&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;void&lt;/span&gt; PaintBackGround(Graphics g)&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   2:&lt;/span&gt; {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   3:&lt;/span&gt;     GraphicsState oldState = g.Save();&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   4:&lt;/span&gt;     &lt;span style=&quot;color: #0000ff&quot;&gt;try&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   5:&lt;/span&gt;     {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   6:&lt;/span&gt;         g.SmoothingMode = SmoothingMode.AntiAlias;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   7:&lt;/span&gt;         ...&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   8:&lt;/span&gt;     }&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   9:&lt;/span&gt;     &lt;span style=&quot;color: #0000ff&quot;&gt;finally&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  10:&lt;/span&gt;     {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  11:&lt;/span&gt;         g.Restore(oldState);&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  12:&lt;/span&gt;     }&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  13:&lt;/span&gt; }&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;  </content><link rel='replies' type='application/atom+xml' href='http://kristofverbiest.blogspot.com/feeds/2621772275824084548/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8259613/2621772275824084548' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8259613/posts/default/2621772275824084548'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8259613/posts/default/2621772275824084548'/><link rel='alternate' type='text/html' href='http://kristofverbiest.blogspot.com/2008/12/saving-and-restoring-gdi-state.html' title='Saving and restoring GDI+ state'/><author><name>Kristof</name><uri>http://www.blogger.com/profile/01727380410232817527</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8259613.post-8103101028497827063</id><published>2008-11-26T13:08:00.001+01:00</published><updated>2008-11-26T13:08:48.301+01:00</updated><title type='text'>Creating a single-instance application</title><content type='html'>&lt;p&gt;A lot has been written on the internet on how to create a single-instance application in C#. The proposed implementations fall into three categories:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Check if a process with the same name is already running (using &lt;a href=&quot;System.Diagnostics.Process.GetProcessesByName()&quot; target=&quot;_blank&quot;&gt;GetProcessesByName&lt;/a&gt;). This is plain wrong because it needs administrator privileges. Furthermore it will not work if there is another process that happens to have the same name as your process. &lt;/li&gt;    &lt;li&gt;Using the Microsoft.VisualBasic namespace (as explained &lt;a href=&quot;http://www.codeproject.com/KB/cs/CSSIApp.aspx&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;). This probably works OK (I have never done this), but it feels a bit wrong. Note that this approach is promoted in the book ‘&lt;a href=&quot;http://www.amazon.com/Windows-Forms-Programming-Microsoft-Development/dp/0321267966/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1224598087&amp;amp;sr=8-1&quot; target=&quot;_blank&quot;&gt;Windows Forms 2.0 Programming&lt;/a&gt;’. &lt;/li&gt;    &lt;li&gt;Using a Mutex. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Personally I think that the last approach is best, but there are some details that you need to get correct if you want to rely on this. This is the pattern that I propose:&lt;/p&gt;  &lt;div style=&quot;border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; background-color: #f4f4f4; max-height: 200px&quot;&gt;   &lt;div style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;     &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   1:&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;class&lt;/span&gt; Program&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   2:&lt;/span&gt; {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   3:&lt;/span&gt;     &lt;span style=&quot;color: #0000ff&quot;&gt;static&lt;/span&gt; Mutex s_Mutex;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   4:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   5:&lt;/span&gt;     &lt;span style=&quot;color: #008000&quot;&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   6:&lt;/span&gt;     &lt;span style=&quot;color: #008000&quot;&gt;/// The main entry point for the application.&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   7:&lt;/span&gt;     &lt;span style=&quot;color: #008000&quot;&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   8:&lt;/span&gt;     [STAThread]&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   9:&lt;/span&gt;     &lt;span style=&quot;color: #0000ff&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;void&lt;/span&gt; Main()&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  10:&lt;/span&gt;     {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  11:&lt;/span&gt;         &lt;span style=&quot;color: #0000ff&quot;&gt;bool&lt;/span&gt; instantiated;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  12:&lt;/span&gt;         &lt;span style=&quot;color: #008000&quot;&gt;// WARNING: you need to replace the GUID in this string!&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  13:&lt;/span&gt;         s_Mutex = &lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; Mutex(&lt;span style=&quot;color: #0000ff&quot;&gt;false&lt;/span&gt;, &lt;span style=&quot;color: #006080&quot;&gt;&amp;quot;Local\\MyAppName{A6F214AB-669D-41B2-9F30-8DD5E5AC9AE1}&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #0000ff&quot;&gt;out&lt;/span&gt; instantiated);&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  14:&lt;/span&gt;         &lt;span style=&quot;color: #0000ff&quot;&gt;if&lt;/span&gt; (!instantiated)&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  15:&lt;/span&gt;         {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  16:&lt;/span&gt;             MessageBox.Show(&lt;span style=&quot;color: #006080&quot;&gt;&amp;quot;The application is already running&amp;quot;&lt;/span&gt;);&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  17:&lt;/span&gt;             &lt;span style=&quot;color: #0000ff&quot;&gt;return&lt;/span&gt;;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  18:&lt;/span&gt;         }&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  19:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  20:&lt;/span&gt;         Application.EnableVisualStyles();&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  21:&lt;/span&gt;         Application.SetCompatibleTextRenderingDefault(&lt;span style=&quot;color: #0000ff&quot;&gt;false&lt;/span&gt;);&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  22:&lt;/span&gt;         Application.Run(&lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; Form1());&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  23:&lt;/span&gt;     }&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  24:&lt;/span&gt; }&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;The pattern is applied to a Winforms application, but of course it can be applied to any type of application.&lt;/p&gt;

&lt;p&gt;For those who don’t know what a &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/system.threading.mutex.aspx&quot; target=&quot;_blank&quot;&gt;mutex&lt;/a&gt; is: it is a Windows kernel-object that can be used for synchronization between threads and processes. The proposed pattern tries to create a mutex with a certain name and checks if this mutex already existed. If it did, this means that our process was already started.&lt;/p&gt;

&lt;p&gt;Here are the details that are important:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;The mutex must be declared static, because we have to make sure that the garbage collector will never dispose it. &lt;/li&gt;

  &lt;li&gt;The name of the mutex should contain a GUID, which ensures that no other application uses the same name. If you don’t know how to create a GUID, click &lt;a href=&quot;http://createguid.com/&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;. &lt;/li&gt;

  &lt;li&gt;The mutex is local, which means that every user that is logged on can create his own instance. If you don’t want this, simply ommit the ‘Local\\’. &lt;/li&gt;

  &lt;li&gt;The mutex contains the name of the application. As a general guideline, I think that named kernel objects should have a descriptive name. This makes it easier when debugging problems: using tools such as SysInternal’s &lt;a href=&quot;http://technet.microsoft.com/en-us/sysinternals/bb896657.aspx&quot; target=&quot;_blank&quot;&gt;WinObj&lt;/a&gt;, you can look at the state of all the kernel objects. If your object has a proper name this will be much simpler. &lt;/li&gt;
&lt;/ul&gt;  </content><link rel='replies' type='application/atom+xml' href='http://kristofverbiest.blogspot.com/feeds/8103101028497827063/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8259613/8103101028497827063' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8259613/posts/default/8103101028497827063'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8259613/posts/default/8103101028497827063'/><link rel='alternate' type='text/html' href='http://kristofverbiest.blogspot.com/2008/11/creating-single-instance-application.html' title='Creating a single-instance application'/><author><name>Kristof</name><uri>http://www.blogger.com/profile/01727380410232817527</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8259613.post-4224657694208178932</id><published>2008-11-19T08:47:00.001+01:00</published><updated>2008-11-19T08:47:24.132+01:00</updated><title type='text'>GDI+ objects should be disposed</title><content type='html'>&lt;p&gt;When a Winforms-control needs to do custom painting, this is typically done by overriding the OnPaint-method like this:&lt;/p&gt;  &lt;div style=&quot;border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; background-color: #f4f4f4; max-height: 200px&quot;&gt;   &lt;div style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;     &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   1:&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;protected&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;override&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;void&lt;/span&gt; OnPaint(PaintEventArgs e)&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   2:&lt;/span&gt; {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   3:&lt;/span&gt;     &lt;span style=&quot;color: #0000ff&quot;&gt;base&lt;/span&gt;.OnPaint(e);&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   4:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   5:&lt;/span&gt;     Graphics g = e.Graphics;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   6:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   7:&lt;/span&gt;     &lt;span style=&quot;color: #008000&quot;&gt;// WARNING: this code should be avoided because it does not dispose the GDI+ objects&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   8:&lt;/span&gt;     g.FillRectangle(&lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; SolidBrush(Color.Black), &lt;span style=&quot;color: #0000ff&quot;&gt;this&lt;/span&gt;.ClientRectangle);&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   9:&lt;/span&gt;     TextRenderer.DrawText(g, &lt;span style=&quot;color: #006080&quot;&gt;&amp;quot;Hello world!&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; Font(FontFamily.GenericSansSerif, 10f), &lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; Point(10, 10), Color.White);&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  10:&lt;/span&gt;     g.DrawLine(&lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; Pen(Color.White), &lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; Point(0, 0), &lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; Point(100,100));&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  11:&lt;/span&gt; }&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;On the internet you will find lots of example code that does custom drawing like this. The problem is that every time this code is executed (and in GUI this can be a lot!), a bunch of GDI+ objects are created that are not disposed. Eventually the garbage collector will kick in and will dispose all these objects. This is to be avoided however:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;This puts a lot of stress on the garbage collector. &lt;/li&gt;

  &lt;li&gt;These objects will be cleaned up in two fazes which will impact performance even more. &lt;/li&gt;

  &lt;li&gt;GDI+ handles are limited resources, but the garbage collector is only invoked if your application needs more memory. It is not unlikely that your code uses a lot of GDI+ handles but does not consume a lot of memory. This may mean that the garbage collector never executes, and your code may consume a huge amount of GDI+ handles. 
    &lt;br /&gt;This issue was especially problematic before &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/system.gc.addmemorypressure.aspx&quot; target=&quot;_blank&quot;&gt;GC.AddMemoryPressure()&lt;/a&gt; was added in .NET 2. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The obvious solution is to dispose the GDI+ objects when they are not needed anymore:&lt;/p&gt;

&lt;div style=&quot;border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; background-color: #f4f4f4; max-height: 200px&quot;&gt;
  &lt;div style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;
    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   1:&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;protected&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;override&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;void&lt;/span&gt; OnPaint(PaintEventArgs e)&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   2:&lt;/span&gt; {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   3:&lt;/span&gt;     &lt;span style=&quot;color: #0000ff&quot;&gt;base&lt;/span&gt;.OnPaint(e);&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   4:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   5:&lt;/span&gt;     Graphics g = e.Graphics;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   6:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   7:&lt;/span&gt;     &lt;span style=&quot;color: #008000&quot;&gt;// Much better: less stress on the garbage collector&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   8:&lt;/span&gt;     &lt;span style=&quot;color: #0000ff&quot;&gt;using&lt;/span&gt; (SolidBrush brush = &lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; SolidBrush(Color.Black))&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   9:&lt;/span&gt;     &lt;span style=&quot;color: #0000ff&quot;&gt;using&lt;/span&gt; (Font font = &lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; Font(FontFamily.GenericSansSerif, 10f))&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  10:&lt;/span&gt;     &lt;span style=&quot;color: #0000ff&quot;&gt;using&lt;/span&gt; (Pen pen = &lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; Pen(Color.White))&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  11:&lt;/span&gt;     {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  12:&lt;/span&gt;         g.FillRectangle(brush, &lt;span style=&quot;color: #0000ff&quot;&gt;this&lt;/span&gt;.ClientRectangle);&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  13:&lt;/span&gt;         TextRenderer.DrawText(g, &lt;span style=&quot;color: #006080&quot;&gt;&amp;quot;Hello world!&amp;quot;&lt;/span&gt;, font, &lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; Point(10, 10), Color.White);&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  14:&lt;/span&gt;         g.DrawLine(pen, &lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; Point(0, 0), &lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; Point(100,100));&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  15:&lt;/span&gt;     }&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  16:&lt;/span&gt; }&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;This code is good but we can make it even better. Note that every time the code is executed, new GDI+ objects are created and disposed. In this particular case this is a waste because the objects will be identical every time. It would be better to create the objects only once, and reuse them every time.&lt;/p&gt;

&lt;p&gt;It seems that the Winforms-designers at Microsoft also had this idea, because they already cache the commonly used brushes and pens in the &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/system.drawing.brushes.aspx&quot; target=&quot;_blank&quot;&gt;Brushes&lt;/a&gt; and &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/system.drawing.pens.aspx&quot; target=&quot;_blank&quot;&gt;Pens&lt;/a&gt; classes. For the Font-object we need to do this ourselves:&lt;/p&gt;

&lt;div style=&quot;border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; background-color: #f4f4f4; max-height: 200px&quot;&gt;
  &lt;div style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;
    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   1:&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;readonly&lt;/span&gt; Font s_Font = &lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; Font(FontFamily.GenericSansSerif, 10f);&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   2:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   3:&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;protected&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;override&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;void&lt;/span&gt; OnPaint(PaintEventArgs e)&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   4:&lt;/span&gt; {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   5:&lt;/span&gt;     &lt;span style=&quot;color: #0000ff&quot;&gt;base&lt;/span&gt;.OnPaint(e);&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   6:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   7:&lt;/span&gt;     Graphics g = e.Graphics;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   8:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   9:&lt;/span&gt;     g.FillRectangle(&lt;font color=&quot;#ff0000&quot;&gt;Brushes.Black&lt;/font&gt;, &lt;span style=&quot;color: #0000ff&quot;&gt;this&lt;/span&gt;.ClientRectangle);&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  10:&lt;/span&gt;     TextRenderer.DrawText(g, &lt;span style=&quot;color: #006080&quot;&gt;&amp;quot;Hello world!&amp;quot;&lt;/span&gt;, &lt;font color=&quot;#ff0000&quot;&gt;s_Font&lt;/font&gt;, &lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; Point(10, 10), Color.White);&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  11:&lt;/span&gt;     g.DrawLine(&lt;font color=&quot;#ff0000&quot;&gt;Pens.White&lt;/font&gt;, &lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; Point(0, 0), &lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; Point(100,100));&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  12:&lt;/span&gt; }&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;Notice that our Font-instance is declared as static because it can be reused when there are many instances of our control.&lt;/p&gt;

&lt;p&gt;Of course this technique can only be used if the GDI+ objects are the same every time. If they are highly dynamic then you will have to create a new object every time (this may occur if you are drawing a part of an animation). But don’t forget to dispose these objects with the using-statement.&lt;/p&gt;  </content><link rel='replies' type='application/atom+xml' href='http://kristofverbiest.blogspot.com/feeds/4224657694208178932/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8259613/4224657694208178932' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8259613/posts/default/4224657694208178932'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8259613/posts/default/4224657694208178932'/><link rel='alternate' type='text/html' href='http://kristofverbiest.blogspot.com/2008/11/gdi-objects-should-be-disposed.html' title='GDI+ objects should be disposed'/><author><name>Kristof</name><uri>http://www.blogger.com/profile/01727380410232817527</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8259613.post-6339827343455939713</id><published>2008-11-12T08:01:00.001+01:00</published><updated>2008-11-14T13:39:04.605+01:00</updated><title type='text'>A Winforms-control should not subscribe to its own events</title><content type='html'>&lt;p&gt;Each Winforms-control defines many events that can be used to receive all kinds of notifications. Often I see that a control subscribes to its own events, for example:&lt;/p&gt;  &lt;div style=&quot;border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; background-color: #f4f4f4; max-height: 200px&quot;&gt;   &lt;div style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;     &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   1:&lt;/span&gt; &lt;span style=&quot;color: #008000&quot;&gt;// this code is added by the designer in the InitializeComponent() method&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   2:&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;this&lt;/span&gt;.Load += &lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; System.EventHandler(&lt;span style=&quot;color: #0000ff&quot;&gt;this&lt;/span&gt;.Form1_Load);&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   3:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   4:&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;void&lt;/span&gt; Form1_Load(&lt;span style=&quot;color: #0000ff&quot;&gt;object&lt;/span&gt; sender, EventArgs e)&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   5:&lt;/span&gt; {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   6:&lt;/span&gt;     ...&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   7:&lt;/span&gt; }&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;This will work OK, but events are conceptually &lt;a href=&quot;http://kristofverbiest.blogspot.com/2008/08/event-subscriptions-may-lead-to-memory.html&quot; target=&quot;_blank&quot;&gt;not so simple&lt;/a&gt;. They are needed when you need a loose coupling between the publisher and the subscriber. This makes no sense if the publisher and the subscriber are the same control.&lt;/p&gt;

&lt;p&gt;There is an alternative that is much simpler: for each event, there is also a virtual method that can be overridden:&lt;/p&gt;

&lt;div style=&quot;border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; background-color: #f4f4f4; max-height: 200px&quot;&gt;
  &lt;div style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;
    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   1:&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;protected&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;override&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;void&lt;/span&gt; OnLoad(EventArgs e)&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   2:&lt;/span&gt; {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   3:&lt;/span&gt;     &lt;span style=&quot;color: #0000ff&quot;&gt;base&lt;/span&gt;.OnLoad(e);&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   4:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   5:&lt;/span&gt;     ...&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   6:&lt;/span&gt; }&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;This alternative will also have a slightly better performance. However, you should not forget to call the base-implementation (luckily, this call will be added automatically by IntelliSense). The base-implementation will raise the event, so if you omit this then the other parts of the code will not receive this event from your control.&lt;/p&gt;  </content><link rel='replies' type='application/atom+xml' href='http://kristofverbiest.blogspot.com/feeds/6339827343455939713/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8259613/6339827343455939713' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8259613/posts/default/6339827343455939713'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8259613/posts/default/6339827343455939713'/><link rel='alternate' type='text/html' href='http://kristofverbiest.blogspot.com/2008/11/winforms-control-should-not-subscribe.html' title='A Winforms-control should not subscribe to its own events'/><author><name>Kristof</name><uri>http://www.blogger.com/profile/01727380410232817527</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8259613.post-484146152820877313</id><published>2008-11-05T10:32:00.001+01:00</published><updated>2008-11-14T13:42:09.074+01:00</updated><title type='text'>Casting an array of value types</title><content type='html'>&lt;p&gt;&lt;em&gt;Warning ahead: this article explains a dirty trick that should only be used in &lt;u&gt;very&lt;/u&gt; specific circumstances (when memory conditions are low). I’ve personally seen this used when loading a large image from file (as a byte array) and needing to pass this array to a piece of legacy code that only accepts an array of shorts. Due to the size of the image, it was not feasible to copy the data.&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;Casting arrays of value types can be problematic. Image that you need to cast a large array of unsigned integers to an array of signed integers.&lt;/p&gt;  &lt;p&gt;Our first experiment might be like this:&lt;/p&gt;  &lt;div style=&quot;border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; background-color: #f4f4f4; max-height: 200px&quot;&gt;   &lt;div style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;     &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   1:&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt;[] Cast(&lt;span style=&quot;color: #0000ff&quot;&gt;uint&lt;/span&gt;[] input)&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   2:&lt;/span&gt; {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   3:&lt;/span&gt;     &lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt;[] result = &lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt;[input.Length];&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   4:&lt;/span&gt;     &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   5:&lt;/span&gt;     Array.Copy(input, result, input.Length);    &lt;span style=&quot;color: #008000&quot;&gt;&lt;font color=&quot;#ff0000&quot;&gt;// throws ArrayTypeMismatchException&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   6:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   7:&lt;/span&gt;     &lt;span style=&quot;color: #0000ff&quot;&gt;return&lt;/span&gt; result;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   8:&lt;/span&gt; }&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;This does not work. We get an &lt;em&gt;ArrayTypeMismatchException&lt;/em&gt; because the two types are not compatible (the rules for compatible types are explained &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/k4yx47a1.aspx&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;The solution is to use the &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/ms146634.aspx&quot; target=&quot;_blank&quot;&gt;Marshal.Copy()&lt;/a&gt; method. This requires unsafe code however. 

  &lt;br /&gt;Here is an example:&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;div style=&quot;border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; background-color: #f4f4f4; max-height: 200px&quot;&gt;
  &lt;div style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;
    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   1:&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;unsafe&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt;[] Cast(&lt;span style=&quot;color: #0000ff&quot;&gt;uint&lt;/span&gt;[] input)&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   2:&lt;/span&gt; {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   3:&lt;/span&gt;     &lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt;[] result = &lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt;[input.Length];&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   4:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   5:&lt;/span&gt;     &lt;span style=&quot;color: #0000ff&quot;&gt;fixed&lt;/span&gt;(&lt;span style=&quot;color: #0000ff&quot;&gt;uint&lt;/span&gt;* pInput = input)&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   6:&lt;/span&gt;     {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   7:&lt;/span&gt;         Marshal.Copy(&lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; IntPtr(pInput),result,0, input.Length);&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   8:&lt;/span&gt;     }&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   9:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  10:&lt;/span&gt;     &lt;span style=&quot;color: #0000ff&quot;&gt;return&lt;/span&gt; result;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  11:&lt;/span&gt; }&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;This seems to work, but instead of &lt;em&gt;casting&lt;/em&gt; the array we really made a &lt;em&gt;copy&lt;/em&gt; of the array. The differences are subtle but can be important:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;the copy requires its own memory. If the array is very large, this can be a problem. I’ve encountered situations (e.g. in image processing) where this was not acceptable. &lt;/li&gt;

  &lt;li&gt;when the copy is modified, the original array is left untouched. This is not what should happen when we cast a variable. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;Using a dirty trick it is possible to cast an array of value types &lt;em&gt;without copying the data&lt;/em&gt;. Look at this sample code:&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;div style=&quot;border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; background-color: #f4f4f4; max-height: 200px&quot;&gt;
  &lt;div style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;
    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   1:&lt;/span&gt; &lt;span style=&quot;color: #008000&quot;&gt;// WARNING: this is a dirty trick. Use at your own risk!&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   2:&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt;[] Convert(&lt;span style=&quot;color: #0000ff&quot;&gt;uint&lt;/span&gt;[] input)&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   3:&lt;/span&gt; {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   4:&lt;/span&gt;     Converter converter = &lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; Converter();&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   5:&lt;/span&gt;     converter.UInts = input;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   6:&lt;/span&gt;     &lt;span style=&quot;color: #0000ff&quot;&gt;return&lt;/span&gt; converter.Ints;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   7:&lt;/span&gt; }&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   8:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   9:&lt;/span&gt; [StructLayout(LayoutKind.Explicit)]&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  10:&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;struct&lt;/span&gt; Converter&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  11:&lt;/span&gt; {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  12:&lt;/span&gt;     [FieldOffset(0)]&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  13:&lt;/span&gt;     &lt;span style=&quot;color: #0000ff&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt;[] Ints;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  14:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  15:&lt;/span&gt;     [FieldOffset(0)]&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  16:&lt;/span&gt;     &lt;span style=&quot;color: #0000ff&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;uint&lt;/span&gt;[] UInts;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  17:&lt;/span&gt; }&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;We’re using a &lt;em&gt;Converter&lt;/em&gt;-struct that uses some interop-magic to define &lt;strong&gt;two fields that map to the same location in memory&lt;/strong&gt;. Now we can access the same data as an int[] or as an uint[]. The data is not copied in any way!&lt;/p&gt;

&lt;p&gt;Note that this is a dirty trick: we fool the compiler into thinking that the type of the array is changed but it really is not. You can verify this by calling &lt;em&gt;GetType()&lt;/em&gt; on the returned array: it will return &lt;em&gt;System.UInt32[]&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;This trick can be used in other situations as well, for instance to access an array of integers as an array of bytes (without copying the data).&lt;/p&gt;

&lt;p&gt;Warning: if the size of the array to be converted is reasonably small, you should just copy the data! Only use this trick if you really cannot afford to copy the data! This trick violates the type-safety of the runtime and is not guarantee to work in all situations or in future versions of .NET! &lt;/p&gt;  </content><link rel='replies' type='application/atom+xml' href='http://kristofverbiest.blogspot.com/feeds/484146152820877313/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8259613/484146152820877313' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8259613/posts/default/484146152820877313'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8259613/posts/default/484146152820877313'/><link rel='alternate' type='text/html' href='http://kristofverbiest.blogspot.com/2008/11/casting-array-of-value-types.html' title='Casting an array of value types'/><author><name>Kristof</name><uri>http://www.blogger.com/profile/01727380410232817527</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8259613.post-6137710550110165679</id><published>2008-10-29T08:11:00.001+01:00</published><updated>2008-11-14T13:42:49.192+01:00</updated><title type='text'>Removing items from a collection</title><content type='html'>&lt;p&gt;A common coding pattern is removing items from a collection based on the value of the items. The simplest way to do this is to enumerate the collection, and remove all the items that you don&#39;t want. &lt;/p&gt;  &lt;p&gt;Code example: &lt;/p&gt;  &lt;div style=&quot;border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; background-color: #f4f4f4; max-height: 200px&quot;&gt;   &lt;div style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;     &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   1:&lt;/span&gt; List&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt; colors = &lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; List&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt;();&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   2:&lt;/span&gt; colors.Add(&lt;span style=&quot;color: #006080&quot;&gt;&amp;quot;red&amp;quot;&lt;/span&gt;);&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   3:&lt;/span&gt; colors.Add(&lt;span style=&quot;color: #006080&quot;&gt;&amp;quot;green&amp;quot;&lt;/span&gt;);&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   4:&lt;/span&gt; colors.Add(&lt;span style=&quot;color: #006080&quot;&gt;&amp;quot;blue&amp;quot;&lt;/span&gt;);&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   5:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   6:&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;foreach&lt;/span&gt; (&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt; color &lt;span style=&quot;color: #0000ff&quot;&gt;in&lt;/span&gt; colors) &lt;font color=&quot;#ff0000&quot;&gt;// WARNING: this code will throw an exception&lt;/font&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   7:&lt;/span&gt; {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   8:&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #0000ff&quot;&gt;if&lt;/span&gt; (color == &lt;span style=&quot;color: #006080&quot;&gt;&amp;quot;green&amp;quot;&lt;/span&gt;) colors.Remove(&lt;span style=&quot;color: #006080&quot;&gt;&amp;quot;green&amp;quot;&lt;/span&gt;);&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   9:&lt;/span&gt; }&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;However, doing this will throw this exception: &lt;em&gt;System.InvalidOperationException: Collection was modified; enumeration operation may not execute&lt;/em&gt;&lt;strong&gt;.&lt;/strong&gt; It seems that the enumerator is invalid after the collection has been modified.&lt;/p&gt;

&lt;p&gt;There are several ways to work around this behavior.&lt;/p&gt;

&lt;h3&gt;1 Store the items to be removed in a temporary collection&lt;/h3&gt;

&lt;p&gt;This approach is really simple: instead of immedeately removing the items, we keep them in a separate collection. In the end, after the original list has been enumerated, we remove all these items.&lt;/p&gt;

&lt;p&gt;Code example:&lt;/p&gt;

&lt;div style=&quot;border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; background-color: #f4f4f4; max-height: 200px&quot;&gt;
  &lt;div style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;
    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   1:&lt;/span&gt; List&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt; colors = &lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; List&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt;();&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   2:&lt;/span&gt; colors.Add(&lt;span style=&quot;color: #006080&quot;&gt;&amp;quot;red&amp;quot;&lt;/span&gt;);&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   3:&lt;/span&gt; colors.Add(&lt;span style=&quot;color: #006080&quot;&gt;&amp;quot;green&amp;quot;&lt;/span&gt;);&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   4:&lt;/span&gt; colors.Add(&lt;span style=&quot;color: #006080&quot;&gt;&amp;quot;blue&amp;quot;&lt;/span&gt;);&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   5:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   6:&lt;/span&gt; List&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt; itemsToRemove = &lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; List&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt;();&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   7:&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;foreach&lt;/span&gt; (&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt; color &lt;span style=&quot;color: #0000ff&quot;&gt;in&lt;/span&gt; colors)&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   8:&lt;/span&gt; {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   9:&lt;/span&gt;     &lt;span style=&quot;color: #0000ff&quot;&gt;if&lt;/span&gt; (color == &lt;span style=&quot;color: #006080&quot;&gt;&amp;quot;green&amp;quot;&lt;/span&gt;) itemsToRemove.Add(&lt;span style=&quot;color: #006080&quot;&gt;&amp;quot;green&amp;quot;&lt;/span&gt;);&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  10:&lt;/span&gt; }&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  11:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  12:&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;foreach&lt;/span&gt; (&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt; itemToRemove &lt;span style=&quot;color: #0000ff&quot;&gt;in&lt;/span&gt; itemsToRemove) colors.Remove(itemToRemove);&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;In most situations this approach works very well. Only in very large collections, the &lt;em&gt;itemsToRemove&lt;/em&gt; collection may become very big and consume a lot of memory. In my experience this is seldom the case.&lt;/p&gt;

&lt;h3&gt;2 Iterate the collection from back to front&lt;/h3&gt;

&lt;p&gt;This alternative uses a for-loop to iterate the collection from the back to the front.&lt;/p&gt;

&lt;p&gt;Code example:&lt;/p&gt;

&lt;div style=&quot;border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; background-color: #f4f4f4; max-height: 200px&quot;&gt;
  &lt;div style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;
    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   1:&lt;/span&gt; List&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt; colors = &lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; List&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt;();&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   2:&lt;/span&gt; colors.Add(&lt;span style=&quot;color: #006080&quot;&gt;&amp;quot;red&amp;quot;&lt;/span&gt;);&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   3:&lt;/span&gt; colors.Add(&lt;span style=&quot;color: #006080&quot;&gt;&amp;quot;green&amp;quot;&lt;/span&gt;);&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   4:&lt;/span&gt; colors.Add(&lt;span style=&quot;color: #006080&quot;&gt;&amp;quot;blue&amp;quot;&lt;/span&gt;);&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   5:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   6:&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt; counter = colors.Count - 1; counter &amp;gt;= 0; counter--)&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   7:&lt;/span&gt; {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   8:&lt;/span&gt;     &lt;span style=&quot;color: #0000ff&quot;&gt;if&lt;/span&gt; (colors[counter] == &lt;span style=&quot;color: #006080&quot;&gt;&amp;quot;green&amp;quot;&lt;/span&gt;) colors.RemoveAt(counter);&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   9:&lt;/span&gt; }&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;h3&gt;&lt;/h3&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;Here we do not need a temporary collection. It is also slightly more efficient because we can remove the item based on its index. This means that the collection does not have to search for the item that has to be removed (as was the case in the first approach). &lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;h3&gt;3 Using a predicate&lt;/h3&gt;

&lt;p&gt;The third alternative is only available from .NET 2 onwards. The List-collection implements a &lt;em&gt;&lt;a href=&quot;http://msdn.microsoft.com/en-us/library/wdka673a.aspx&quot; target=&quot;_blank&quot;&gt;RemoveAll()&lt;/a&gt;&lt;/em&gt; method that takes a &lt;em&gt;&lt;a href=&quot;http://msdn.microsoft.com/en-us/library/bfcke1bz.aspx&quot; target=&quot;_blank&quot;&gt;predicate&lt;/a&gt;&lt;/em&gt; as a parameter. Simply put, a predicate is a delegate that is used for filtering. In this case, the predicate returns true for each item that needs to be removed.&lt;/p&gt;

&lt;p&gt;In this code example, the predicate is implemented as an anonymous method:&lt;/p&gt;

&lt;div style=&quot;border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; background-color: #f4f4f4; max-height: 200px&quot;&gt;
  &lt;div style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;
    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   1:&lt;/span&gt; List&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt; colors = &lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; List&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt;();&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   2:&lt;/span&gt; colors.Add(&lt;span style=&quot;color: #006080&quot;&gt;&amp;quot;red&amp;quot;&lt;/span&gt;);&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   3:&lt;/span&gt; colors.Add(&lt;span style=&quot;color: #006080&quot;&gt;&amp;quot;green&amp;quot;&lt;/span&gt;);&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   4:&lt;/span&gt; colors.Add(&lt;span style=&quot;color: #006080&quot;&gt;&amp;quot;blue&amp;quot;&lt;/span&gt;);&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   5:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   6:&lt;/span&gt; colors.RemoveAll(&lt;span style=&quot;color: #0000ff&quot;&gt;delegate&lt;/span&gt;(&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt; color)&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   7:&lt;/span&gt; {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   8:&lt;/span&gt;     &lt;span style=&quot;color: #0000ff&quot;&gt;return&lt;/span&gt; color == &lt;span style=&quot;color: #006080&quot;&gt;&amp;quot;green&amp;quot;&lt;/span&gt;;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   9:&lt;/span&gt; });&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;This approach has some benefits:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;It is &lt;a href=&quot;http://kristofverbiest.blogspot.com/2008/08/good-code-is-self-documenting.html&quot; target=&quot;_blank&quot;&gt;self-documenting&lt;/a&gt;. &lt;/li&gt;

  &lt;li&gt;We don’t have to worry about the performance, we can assume that the RemoveAll() method is implemented in the most efficient way. If the internals of the collection-class would later change, the RemoveAll() method will likely be changed as well to keep the best performance. &lt;/li&gt;
&lt;/ul&gt;  </content><link rel='replies' type='application/atom+xml' href='http://kristofverbiest.blogspot.com/feeds/6137710550110165679/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8259613/6137710550110165679' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8259613/posts/default/6137710550110165679'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8259613/posts/default/6137710550110165679'/><link rel='alternate' type='text/html' href='http://kristofverbiest.blogspot.com/2008/10/removing-items-from-collection.html' title='Removing items from a collection'/><author><name>Kristof</name><uri>http://www.blogger.com/profile/01727380410232817527</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8259613.post-8226908569784344509</id><published>2008-10-22T08:03:00.001+02:00</published><updated>2008-11-14T13:43:15.085+01:00</updated><title type='text'>A type-safe pattern to implement ICloneable and similar interfaces</title><content type='html'>&lt;p&gt;The &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/system.icloneable.aspx&quot; target=&quot;_blank&quot;&gt;ICloneable interface&lt;/a&gt; contains a single method &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/system.icloneable.clone.aspx&quot; target=&quot;_blank&quot;&gt;Clone()&lt;/a&gt; that returns a clone of the object.&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;However the return value is defined as type ‘System.Object’, which means that the user still has to cast this to the actual type:&lt;/p&gt;  &lt;div style=&quot;border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; background-color: #f4f4f4; max-height: 200px&quot;&gt;   &lt;div style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;     &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   1:&lt;/span&gt; Test t = &lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; Test();&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   2:&lt;/span&gt; Test t2 = t.Clone() &lt;span style=&quot;color: #0000ff&quot;&gt;as&lt;/span&gt; Test;    &lt;font color=&quot;#ff0000&quot;&gt;// cast needed&lt;/font&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;This is cumbersome and error-prone because the cast may fail at runtime.&lt;/p&gt;

&lt;p&gt;Use the following pattern to provide a type-safe implementation of this interface:&lt;/p&gt;

&lt;div style=&quot;border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; background-color: #f4f4f4; max-height: 200px&quot;&gt;
  &lt;div style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;
    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   1:&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;class&lt;/span&gt; Test : ICloneable&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   2:&lt;/span&gt; {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   3:&lt;/span&gt;     &lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt; m_Amount;&amp;#160; &lt;font color=&quot;#008000&quot;&gt;// just some example data...&lt;/font&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   4:&lt;/span&gt;     Font m_Font;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   5:&lt;/span&gt;     &lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt; m_Name;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   6:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   7:&lt;/span&gt;     &lt;span style=&quot;color: #cc6633&quot;&gt;#region&lt;/span&gt; ICloneable Members&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   8:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   9:&lt;/span&gt;     &lt;span style=&quot;color: #008000&quot;&gt;// Provide a type-safe implementation&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  10:&lt;/span&gt;     &lt;span style=&quot;color: #0000ff&quot;&gt;public&lt;/span&gt; Test Clone()&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  11:&lt;/span&gt;     {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  12:&lt;/span&gt;         Test clone = &lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; Test();&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  13:&lt;/span&gt;         clone.m_Amount = m_Amount;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  14:&lt;/span&gt;         clone.m_Font = m_Font.Clone() &lt;span style=&quot;color: #0000ff&quot;&gt;as&lt;/span&gt; Font;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  15:&lt;/span&gt;         clone.m_Name = m_Name;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  16:&lt;/span&gt;         &lt;span style=&quot;color: #0000ff&quot;&gt;return&lt;/span&gt; clone;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  17:&lt;/span&gt;     }&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  18:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  19:&lt;/span&gt;     &lt;span style=&quot;color: #008000&quot;&gt;// Provide an &amp;quot;Explicit Interface Method Implementation&amp;quot;&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  20:&lt;/span&gt;     &lt;span style=&quot;color: #0000ff&quot;&gt;object&lt;/span&gt; ICloneable.Clone()&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  21:&lt;/span&gt;     {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  22:&lt;/span&gt;         &lt;span style=&quot;color: #0000ff&quot;&gt;return&lt;/span&gt; Clone();&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  23:&lt;/span&gt;     }&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  24:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  25:&lt;/span&gt;     &lt;span style=&quot;color: #cc6633&quot;&gt;#endregion&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  26:&lt;/span&gt; }&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;This example implements a type-safe Clone() method, so the user does not have to cast the result. But we still have to implement the interface-method that returns a System.Object. This is done through &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/ms173157.aspx&quot; target=&quot;_blank&quot;&gt;explicit interface method implementation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;This pattern is easier for the user of the class, and it has the benefit that the compiler will now guarantee that the returned object is always of the correct type.&lt;/p&gt;  </content><link rel='replies' type='application/atom+xml' href='http://kristofverbiest.blogspot.com/feeds/8226908569784344509/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8259613/8226908569784344509' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8259613/posts/default/8226908569784344509'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8259613/posts/default/8226908569784344509'/><link rel='alternate' type='text/html' href='http://kristofverbiest.blogspot.com/2008/10/type-safe-pattern-to-implement.html' title='A type-safe pattern to implement ICloneable and similar interfaces'/><author><name>Kristof</name><uri>http://www.blogger.com/profile/01727380410232817527</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8259613.post-8589518295527307746</id><published>2008-10-15T08:31:00.001+02:00</published><updated>2008-11-14T13:43:38.918+01:00</updated><title type='text'>Beware of the stopwatch</title><content type='html'>&lt;p&gt;In a comment on my &lt;a href=&quot;http://kristofverbiest.blogspot.com/2008/10/sometimes-it-is-better-to-use.html&quot; target=&quot;_blank&quot;&gt;last post&lt;/a&gt;, &lt;a href=&quot;http://craniac.myopenid.com/&quot; target=&quot;_blank&quot;&gt;craniac&lt;/a&gt; argued that the &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx&quot; target=&quot;_blank&quot;&gt;StopWatch&lt;/a&gt;-class should be used for measuring time intervals.&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;Sadly, measuring a time interval is not as easy as it sounds. There are several mechanisms, I will explain 3 of them and point out their strengths and weaknesses.&lt;/p&gt;  &lt;h2&gt;1. DateTime.UtcNow&lt;/h2&gt;  &lt;div style=&quot;border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; background-color: #f4f4f4; max-height: 200px&quot;&gt;   &lt;div style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;     &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   1:&lt;/span&gt; DateTime begin = DateTime.UtcNow;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   2:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   3:&lt;/span&gt; ...&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   4:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   5:&lt;/span&gt; DateTime end = DateTime.UtcNow;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   6:&lt;/span&gt; Console.WriteLine(&lt;span style=&quot;color: #006080&quot;&gt;&amp;quot;Measured time: &amp;quot;&lt;/span&gt; + (end-begin).TotalMilliseconds + &lt;span style=&quot;color: #006080&quot;&gt;&amp;quot; ms.&amp;quot;&lt;/span&gt;);&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;This is the fastest mechanism because under the hood it only reads a counter from memory (the code above takes only 76 nanoseconds to execute on my machine). However the resolution is not so great: only 10 milliseconds on recent version of Windows. If you want to measure a piece of code that takes shorter to execute, you will have to execute it e.g. 1000 times and measure how long this takes, then divide the result by 1000.&lt;/p&gt;

&lt;p&gt;Another disadvantage is that this is not reliable if the system-time changes. This should be a very rare situation, but could for instance happen through synchronization with a time server.&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;h2&gt;2. Stopwatch&lt;/h2&gt;

&lt;div style=&quot;border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; background-color: #f4f4f4; max-height: 200px&quot;&gt;
  &lt;div style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;
    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   1:&lt;/span&gt; Stopwatch watch = &lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; Stopwatch();&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   2:&lt;/span&gt; watch.Start();&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   3:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   4:&lt;/span&gt; ...&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   5:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   6:&lt;/span&gt; watch.Stop();&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   7:&lt;/span&gt; Console.WriteLine(&lt;span style=&quot;color: #006080&quot;&gt;&amp;quot;Measured time: &amp;quot;&lt;/span&gt; + watch.Elapsed.TotalMilliseconds + &lt;span style=&quot;color: #006080&quot;&gt;&amp;quot; ms.&amp;quot;&lt;/span&gt;);&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;This mechanism is a bit slower than the previous one (10 times slower on my machine) so for short intervals this may have an impact on the result. However there are some serious issues:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;This can be unreliable on a PC with multiple processors. Due to a bug in the BIOS, Start() and Stop() must be executed on the same processor to get a correct result. &lt;/li&gt;

  &lt;li&gt;This is unreliable on processors that do not have a constant clock speed (most processors can reduce the clock speed to conserve energy). This is explained in detail &lt;a href=&quot;http://www.virtualdub.org/blog/pivot/entry.php?id=106&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I suspect that you can get a reliable result if you run it on a single-processor machine and disable any power-saving options in the BIOS. I haven’t tested this though.&lt;/p&gt;

&lt;p&gt;On the upside, it has the hightest possible resolution (which depends on the hardware it runs on).&lt;/p&gt;

&lt;h2&gt;3. Process.TotalProcessorTime&lt;/h2&gt;

&lt;div style=&quot;border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; background-color: #f4f4f4; max-height: 200px&quot;&gt;
  &lt;div style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;
    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   1:&lt;/span&gt; TimeSpan begin = Process.GetCurrentProcess().TotalProcessorTime;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   2:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   3:&lt;/span&gt; ...&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   4:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   5:&lt;/span&gt; TimeSpan end = Process.GetCurrentProcess().TotalProcessorTime;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   6:&lt;/span&gt; Console.WriteLine(&lt;span style=&quot;color: #006080&quot;&gt;&amp;quot;Measured time: &amp;quot;&lt;/span&gt; + (end - begin).TotalMilliseconds + &lt;span style=&quot;color: #006080&quot;&gt;&amp;quot; ms.&amp;quot;&lt;/span&gt;);&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;This mechanism is different from the previous ones because it does not measure how much time has passed, but it measures how long your process has kept the CPU busy. This is great for performance measurements:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;The timings are not distorted by other processes that consume a lot of CPU. &lt;/li&gt;

  &lt;li&gt;You can measure the impact that your code has on the overall performance of the system. On laptops, this also gives an indication towards the battery-power that is consumed by your process. This can be important for applications that run for a long time (such as services and other background tasks). &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To interpret the measured time correctly, you should realize that time that is spent while your code is waiting (e.g. in a Sleep) will not be counted. On the other hand, if your process is keeping multiple processors busy, the time of each processor will be added (if a dual-core processor is kept 100% busy, the ‘TotalProcessorTime’ will increment with 2 each second!).&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;Note that this mechanism has the worst performance: the code above takes 19264 nanoseconds on my PC. This is 250 times slower than using DateTime.UtcNow!&lt;/p&gt;

&lt;p&gt;So there is not one-size-fits-all solution to measure a time interval. Personally, if I want to measure how long a piece of code takes to execute, I run it in a loop (e.g. one million times) and measure it using DateTime.UtcNow.&lt;/p&gt;  </content><link rel='replies' type='application/atom+xml' href='http://kristofverbiest.blogspot.com/feeds/8589518295527307746/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8259613/8589518295527307746' title='10 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8259613/posts/default/8589518295527307746'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8259613/posts/default/8589518295527307746'/><link rel='alternate' type='text/html' href='http://kristofverbiest.blogspot.com/2008/10/beware-of-stopwatch.html' title='Beware of the stopwatch'/><author><name>Kristof</name><uri>http://www.blogger.com/profile/01727380410232817527</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>10</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8259613.post-3295053173421724038</id><published>2008-10-08T09:03:00.001+02:00</published><updated>2008-11-14T13:44:09.559+01:00</updated><title type='text'>Sometimes it is better to use DateTime.UtcNow instead of DateTime.Now</title><content type='html'>&lt;p&gt;When you want to measure how long a certain action takes, you should use &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/system.datetime.utcnow.aspx&quot; target=&quot;_blank&quot;&gt;DateTime.UtcNow&lt;/a&gt; instead of &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/system.datetime.now.aspx&quot; target=&quot;_blank&quot;&gt;DateTime.Now&lt;/a&gt;:&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div style=&quot;border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; background-color: #f4f4f4; max-height: 200px&quot;&gt;   &lt;div style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;     &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   1:&lt;/span&gt; DateTime begin = DateTime.UtcNow;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   2:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   3:&lt;/span&gt; ...&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   4:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   5:&lt;/span&gt; DateTime end = DateTime.UtcNow;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   6:&lt;/span&gt; MessageBox.Show(&lt;span style=&quot;color: #006080&quot;&gt;&amp;quot;Time taken: &amp;quot;&lt;/span&gt; + (end-begin).TotalMilliseconds);&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;Reasons:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;DateTime.UtcNow is more efficient than DateTime.Now (on my PC: 21 nanoseconds versus 575 nanoseconds). &lt;/li&gt;

  &lt;li&gt;More importantly: DateTime.Now will get you into trouble if &lt;a href=&quot;http://en.wikipedia.org/wiki/Daylight_saving_time&quot; target=&quot;_blank&quot;&gt;Daylight Saving Time&lt;/a&gt; is enabled. For instance, it is possible that &lt;em&gt;begin &lt;/em&gt;contains 02:59:00 and &lt;em&gt;end&lt;/em&gt; contains 02:01:00 (while only two minutes have elapsed). &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Of course, &lt;em&gt;both &lt;/em&gt;mechanisms are incorrect if the user changes his clock (manually or through synchronziation with a &lt;a href=&quot;http://en.wikipedia.org/wiki/Time_server&quot; target=&quot;_blank&quot;&gt;time server&lt;/a&gt;).&lt;/p&gt;  </content><link rel='replies' type='application/atom+xml' href='http://kristofverbiest.blogspot.com/feeds/3295053173421724038/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8259613/3295053173421724038' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8259613/posts/default/3295053173421724038'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8259613/posts/default/3295053173421724038'/><link rel='alternate' type='text/html' href='http://kristofverbiest.blogspot.com/2008/10/sometimes-it-is-better-to-use.html' title='Sometimes it is better to use DateTime.UtcNow instead of DateTime.Now'/><author><name>Kristof</name><uri>http://www.blogger.com/profile/01727380410232817527</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8259613.post-7994449523167275014</id><published>2008-10-01T10:08:00.001+02:00</published><updated>2008-11-14T13:44:35.404+01:00</updated><title type='text'>Avoid invoking a virtual method in a constructor</title><content type='html'>&lt;p&gt;You should be very careful when invoking a virtual method in the constructor of a class that is not sealed.&lt;/p&gt;  &lt;p&gt;Reason: when the virtual method is overriden in a derived class, this &lt;strong&gt;method will be invoked even before the constructor&lt;/strong&gt; of the derived class has been invoked! This probably was not anticipated by the developer of the derived class!&lt;/p&gt;  &lt;p&gt;Take for instance this example:&lt;/p&gt;  &lt;div style=&quot;border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; background-color: #f4f4f4; max-height: 200px&quot;&gt;   &lt;div style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;     &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   1:&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;abstract&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;class&lt;/span&gt; Base&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   2:&lt;/span&gt; {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   3:&lt;/span&gt;     &lt;span style=&quot;color: #0000ff&quot;&gt;public&lt;/span&gt; Base() &lt;font color=&quot;#008000&quot;&gt;// This constructor calls a virtual method&lt;/font&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   4:&lt;/span&gt;     {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   5:&lt;/span&gt;         Initialize();&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   6:&lt;/span&gt;     }&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   7:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   8:&lt;/span&gt;     &lt;span style=&quot;color: #0000ff&quot;&gt;protected&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;abstract&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;void&lt;/span&gt; Initialize();&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   9:&lt;/span&gt; }&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  10:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  11:&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;class&lt;/span&gt; Derived : Base&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  12:&lt;/span&gt; {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  13:&lt;/span&gt;     FileStream m_File;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  14:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  15:&lt;/span&gt;     &lt;span style=&quot;color: #0000ff&quot;&gt;public&lt;/span&gt; Derived()&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  16:&lt;/span&gt;     {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  17:&lt;/span&gt;         m_File = &lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; FileStream(&lt;span style=&quot;color: #006080&quot;&gt;@&amp;quot;c:\temp\test.txt&amp;quot;&lt;/span&gt;, FileMode.Open);&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  18:&lt;/span&gt;     }&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  19:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  20:&lt;/span&gt;     &lt;span style=&quot;color: #0000ff&quot;&gt;protected&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;override&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;void&lt;/span&gt; Initialize()&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  21:&lt;/span&gt;     {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  22:&lt;/span&gt;         MessageBox.Show(m_File.Length.ToString());    &lt;span style=&quot;color: #008000&quot;&gt;&lt;font color=&quot;#ff0000&quot;&gt;// NullReferenceException! m_File has not been constructed yet!&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  23:&lt;/span&gt;     }&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  24:&lt;/span&gt; }&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;When an instance of &lt;em&gt;Derived&lt;/em&gt; is created, first the constructor of the base class is called. This will result in &lt;em&gt;Derived.Initialize()&lt;/em&gt; being called before the constructor of &lt;em&gt;Derived&lt;/em&gt; has executed!&lt;/p&gt;

&lt;p&gt;Because this is very contra-intuitive, it is best not to invoke virtual methods from a constructor altogether. Except when the class is sealed of course, because then there can be no derived class.&lt;/p&gt;  </content><link rel='replies' type='application/atom+xml' href='http://kristofverbiest.blogspot.com/feeds/7994449523167275014/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8259613/7994449523167275014' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8259613/posts/default/7994449523167275014'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8259613/posts/default/7994449523167275014'/><link rel='alternate' type='text/html' href='http://kristofverbiest.blogspot.com/2008/10/avoid-invoking-virtual-method-in.html' title='Avoid invoking a virtual method in a constructor'/><author><name>Kristof</name><uri>http://www.blogger.com/profile/01727380410232817527</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8259613.post-1537336371479849920</id><published>2008-09-24T08:15:00.001+02:00</published><updated>2008-11-14T13:44:57.475+01:00</updated><title type='text'>Prevent indentation by combining using-statements</title><content type='html'>&lt;p&gt;C# code tends to be heavily indented:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;All code is indented at least three levels deep (namespace, class, method). &lt;/li&gt;    &lt;li&gt;The usage of constructs such as &lt;em&gt;using&lt;/em&gt; and &lt;em&gt;try-finally&lt;/em&gt; adds further indentation. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Especially in GDI+ drawing code, the usage of using-statements can make the code much more difficult to read:&lt;/p&gt;  &lt;div style=&quot;border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; background-color: #f4f4f4; max-height: 200px&quot;&gt;   &lt;div style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;     &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   1:&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;protected&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;override&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;void&lt;/span&gt; OnPaint(PaintEventArgs e)&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   2:&lt;/span&gt; {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   3:&lt;/span&gt;     &lt;span style=&quot;color: #0000ff&quot;&gt;base&lt;/span&gt;.OnPaint(e);&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   4:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   5:&lt;/span&gt;     &lt;span style=&quot;color: #0000ff&quot;&gt;using&lt;/span&gt; (Pen thickBlack = &lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; Pen(Color.Black, 5f))&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   6:&lt;/span&gt;     {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   7:&lt;/span&gt;         &lt;span style=&quot;color: #0000ff&quot;&gt;using&lt;/span&gt; (Pen thickRed = &lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; Pen(Color.Red, 5f))&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   8:&lt;/span&gt;         {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   9:&lt;/span&gt;             &lt;span style=&quot;color: #0000ff&quot;&gt;using&lt;/span&gt; (LinearGradientBrush backGround = &lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; LinearGradientBrush(...)&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  10:&lt;/span&gt;             {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  11:&lt;/span&gt;                 &lt;span style=&quot;color: #008000&quot;&gt;// start painting here...&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  12:&lt;/span&gt;             }&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  13:&lt;/span&gt;         }&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  14:&lt;/span&gt;     }&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  15:&lt;/span&gt; }&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;This can be simplified by combining the using-statements like this:&lt;/p&gt;

&lt;div style=&quot;border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; background-color: #f4f4f4; max-height: 200px&quot;&gt;
  &lt;div style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;
    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   1:&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;protected&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;override&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;void&lt;/span&gt; OnPaint(PaintEventArgs e)&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   2:&lt;/span&gt; {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   3:&lt;/span&gt;     &lt;span style=&quot;color: #0000ff&quot;&gt;base&lt;/span&gt;.OnPaint(e);&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   4:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   5:&lt;/span&gt;     &lt;span style=&quot;color: #0000ff&quot;&gt;using&lt;/span&gt; (Pen thickBlack = &lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; Pen(Color.Black, 5f))&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   6:&lt;/span&gt;     &lt;span style=&quot;color: #0000ff&quot;&gt;using&lt;/span&gt; (Pen thickRed = &lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; Pen(Color.Red, 5f))&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   7:&lt;/span&gt;     &lt;span style=&quot;color: #0000ff&quot;&gt;using&lt;/span&gt; (LinearGradientBrush backGround = &lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; LinearGradientBrush(...)&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   8:&lt;/span&gt;     {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   9:&lt;/span&gt;         &lt;span style=&quot;color: #008000&quot;&gt;// start painting here...&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  10:&lt;/span&gt;     }&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  11:&lt;/span&gt; }&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;As you can see this reduces indentation, making the code easier to read and understand.&lt;/p&gt;  </content><link rel='replies' type='application/atom+xml' href='http://kristofverbiest.blogspot.com/feeds/1537336371479849920/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8259613/1537336371479849920' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8259613/posts/default/1537336371479849920'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8259613/posts/default/1537336371479849920'/><link rel='alternate' type='text/html' href='http://kristofverbiest.blogspot.com/2008/09/prevent-indentation-by-combining-using.html' title='Prevent indentation by combining using-statements'/><author><name>Kristof</name><uri>http://www.blogger.com/profile/01727380410232817527</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8259613.post-1683752132811311916</id><published>2008-09-17T08:44:00.001+02:00</published><updated>2008-11-14T13:45:19.103+01:00</updated><title type='text'>Prevent unnecessary indentation</title><content type='html'>&lt;p&gt;Code that is heavily indented is difficult to read (especially if the method does not fit on a single screen anymore).&lt;/p&gt;  &lt;p&gt;Instead of this code …&lt;/p&gt;  &lt;div style=&quot;border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; background-color: #f4f4f4; max-height: 200px&quot;&gt;   &lt;div style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;     &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   1:&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;void&lt;/span&gt; Test(&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt; input)&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   2:&lt;/span&gt; {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   3:&lt;/span&gt;   &lt;span style=&quot;color: #0000ff&quot;&gt;if&lt;/span&gt; (input != &lt;span style=&quot;color: #0000ff&quot;&gt;null&lt;/span&gt;)&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   4:&lt;/span&gt;   {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   5:&lt;/span&gt;     &lt;span style=&quot;color: #0000ff&quot;&gt;if&lt;/span&gt; (input.Length &amp;gt; 0)&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   6:&lt;/span&gt;     {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   7:&lt;/span&gt;       …&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   8:&lt;/span&gt;     }&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   9:&lt;/span&gt;   }&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  10:&lt;/span&gt; }&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;… write this code:&lt;/p&gt;

&lt;div style=&quot;border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; background-color: #f4f4f4; max-height: 200px&quot;&gt;
  &lt;div style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;
    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   1:&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;void&lt;/span&gt; Test(&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt; input)&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   2:&lt;/span&gt; {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   3:&lt;/span&gt;   &lt;span style=&quot;color: #0000ff&quot;&gt;if&lt;/span&gt; (input == &lt;span style=&quot;color: #0000ff&quot;&gt;null&lt;/span&gt;) &lt;span style=&quot;color: #0000ff&quot;&gt;return&lt;/span&gt;;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   4:&lt;/span&gt;   &lt;span style=&quot;color: #0000ff&quot;&gt;if&lt;/span&gt; (input.Length == 0) &lt;span style=&quot;color: #0000ff&quot;&gt;return&lt;/span&gt;;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   5:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   6:&lt;/span&gt;   …&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   7:&lt;/span&gt; }&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;  </content><link rel='replies' type='application/atom+xml' href='http://kristofverbiest.blogspot.com/feeds/1683752132811311916/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8259613/1683752132811311916' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8259613/posts/default/1683752132811311916'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8259613/posts/default/1683752132811311916'/><link rel='alternate' type='text/html' href='http://kristofverbiest.blogspot.com/2008/09/prevent-unnecessary-indentation.html' title='Prevent unnecessary indentation'/><author><name>Kristof</name><uri>http://www.blogger.com/profile/01727380410232817527</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8259613.post-7637212774506719403</id><published>2008-09-05T15:47:00.001+02:00</published><updated>2008-11-14T13:45:45.492+01:00</updated><title type='text'>How to use consistent version numbering across multiple projects</title><content type='html'>&lt;p&gt;If you have many C# projects, you may want all your assemblies to have the same version numbers. Remember that the version numbers are defined in the ‘AssemblyInfo.cs’ file in each project.&lt;/p&gt;  &lt;p&gt;There are several mechanisms to achieve this:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Programmatically change all the ‘AssemblyInfo.cs’-files to update the version number.      &lt;br /&gt;You could write a tool that updates the version number of all your projects. This tool could then be integrated in your build process. This is cumbersome and often not so simple if your projects are checked in a &lt;a href=&quot;http://en.wikipedia.org/wiki/Revision_control&quot; target=&quot;_blank&quot;&gt;source control system&lt;/a&gt;. &lt;/li&gt;    &lt;li&gt;Define the version-numbers in a single file that is used by all your projects. This technique is explained &lt;a href=&quot;http://www.codeproject.com/KB/cs/Sync_Version_Numbers.aspx&quot; target=&quot;_blank&quot;&gt;in this article&lt;/a&gt;. &lt;/li&gt;    &lt;li&gt;Define the version-numbers in a separate assembly that is referenced by all projects. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;In this post I will elaborate on the third mechanism (because I’ve never seen this explained anywhere else).&lt;/p&gt;  &lt;p&gt;Create a new C# project (called Metadata.csproj) that contains all the metadata that is shared across your projects. This project is very simple and contains only 1 class:&lt;/p&gt;  &lt;div style=&quot;border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; background-color: #f4f4f4; max-height: 200px&quot;&gt;   &lt;div style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;     &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   1:&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;namespace&lt;/span&gt; KristofVerbiest&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   2:&lt;/span&gt; {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   3:&lt;/span&gt;     &lt;span style=&quot;color: #0000ff&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;class&lt;/span&gt; ProjectMetadata&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   4:&lt;/span&gt;     {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   5:&lt;/span&gt;         &lt;span style=&quot;color: #0000ff&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt; FileVersion = &lt;span style=&quot;color: #006080&quot;&gt;&amp;quot;1.5.0.1&amp;quot;&lt;/span&gt;;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   6:&lt;/span&gt;         &lt;span style=&quot;color: #0000ff&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt; CompanyName = &lt;span style=&quot;color: #006080&quot;&gt;&amp;quot;Kristof Verbiest&amp;quot;&lt;/span&gt;;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   7:&lt;/span&gt;         &lt;span style=&quot;color: #0000ff&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt; Copyright = &lt;span style=&quot;color: #006080&quot;&gt;&amp;quot;Copyright © Kristof Verbiest 2008&amp;quot;&lt;/span&gt;;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   8:&lt;/span&gt;     }&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   9:&lt;/span&gt; }&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;Now you can reference this assembly from all your other C# projects, and you can use the data from the ‘AssemblyInfo.cs’ files like this: &lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;div style=&quot;border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; background-color: #f4f4f4; max-height: 200px&quot;&gt;
  &lt;div style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;
    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   1:&lt;/span&gt; [assembly: AssemblyFileVersion(ProjectMetadata.FileVersion)]&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   2:&lt;/span&gt; [assembly: AssemblyCompany(ProjectMetadata.CompanyName)]&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   3:&lt;/span&gt; [assembly: AssemblyCopyright(ProjectMetadata.CopyRight)]&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;Note that the ‘Metadata.dll’ assembly is used by the compiler to fill in the correct metadata. However this assembly is not needed at runtime! So you don’t need to deploy this assembly to your users, it is only needed by the compiler.&lt;/p&gt;  </content><link rel='replies' type='application/atom+xml' href='http://kristofverbiest.blogspot.com/feeds/7637212774506719403/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8259613/7637212774506719403' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8259613/posts/default/7637212774506719403'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8259613/posts/default/7637212774506719403'/><link rel='alternate' type='text/html' href='http://kristofverbiest.blogspot.com/2008/09/how-to-use-consistent-version-numbering.html' title='How to use consistent version numbering across multiple projects'/><author><name>Kristof</name><uri>http://www.blogger.com/profile/01727380410232817527</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8259613.post-2553913524939913254</id><published>2008-09-03T11:29:00.001+02:00</published><updated>2008-11-14T13:46:02.001+01:00</updated><title type='text'>Assume that someone who is less experienced than you needs to understand your code</title><content type='html'>&lt;p&gt;Transparency is an important aspect in a project where many developers are working on the same software.&lt;/p&gt;  &lt;p&gt;Some developers like to show of how smart they are using complex constructs that are difficult to understand. This is unnecessary and not fair towards their colleagues.&lt;/p&gt;  &lt;p&gt;Some examples:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Unnecessary usage of the &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/ty67wk28(VS.80).aspx&quot; target=&quot;_blank&quot;&gt;?: operator&lt;/a&gt; and the &lt;a href=&quot;http://www.alteridem.net/2007/08/17/null-coalescing-operator/&quot; target=&quot;_blank&quot;&gt;null coalescing operator&lt;/a&gt;. These operators are not commonly known and should only be used if they improve the readability of the code. &lt;/li&gt;    &lt;li&gt;Complex expressions whose output depends on the operator precedence rules. &lt;/li&gt;    &lt;li&gt;Usage of &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/0yw3tz5k(VS.80).aspx&quot; target=&quot;_blank&quot;&gt;anonymous methods&lt;/a&gt;. &lt;/li&gt;    &lt;li&gt;Usage of &lt;a href=&quot;http://en.wikipedia.org/wiki/Regular_expression&quot; target=&quot;_blank&quot;&gt;regular expressions&lt;/a&gt;. Regular expressions can be very powerful, but they have the nasty habbit that they tend to be &lt;em&gt;easier to write than to read&lt;/em&gt;.       &lt;br /&gt;Consider rewriting your logic using simple string actions. If you really need to use regular expressions, you should document them very well. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;If you encounter a situation where one of these techniques really makes sense then of course you should use it. But you should also add some documentation why you are doing it (and maybe include a link to a website that explains the technique).&lt;/p&gt;  </content><link rel='replies' type='application/atom+xml' href='http://kristofverbiest.blogspot.com/feeds/2553913524939913254/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8259613/2553913524939913254' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8259613/posts/default/2553913524939913254'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8259613/posts/default/2553913524939913254'/><link rel='alternate' type='text/html' href='http://kristofverbiest.blogspot.com/2008/09/assume-that-someone-who-is-less.html' title='Assume that someone who is less experienced than you needs to understand your code'/><author><name>Kristof</name><uri>http://www.blogger.com/profile/01727380410232817527</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8259613.post-2628504941296048025</id><published>2008-08-27T09:33:00.001+02:00</published><updated>2008-11-14T13:46:24.193+01:00</updated><title type='text'>Good code is self-documenting</title><content type='html'>&lt;p&gt;&lt;a href=&quot;http://www.codinghorror.com/blog/archives/000553.html&quot; target=&quot;_blank&quot;&gt;Picking a good name for a class/method is often very difficult&lt;/a&gt;. But if the names are chosen carefully the code often needs no further documentation.&lt;/p&gt;  &lt;p&gt;For instance compare these two simple examples (they are functionally equivalent):&lt;/p&gt;  &lt;div style=&quot;border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; background-color: #f4f4f4; max-height: 200px&quot;&gt;   &lt;div style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;     &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   1:&lt;/span&gt; &lt;span style=&quot;color: #008000&quot;&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   2:&lt;/span&gt; &lt;span style=&quot;color: #008000&quot;&gt;/// Summary for Bounds class&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   3:&lt;/span&gt; &lt;span style=&quot;color: #008000&quot;&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   4:&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;class&lt;/span&gt; Bounds&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   5:&lt;/span&gt; {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   6:&lt;/span&gt;   &lt;span style=&quot;color: #0000ff&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt; m_A;    &lt;span style=&quot;color: #008000&quot;&gt;// lower bound&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   7:&lt;/span&gt;   &lt;span style=&quot;color: #0000ff&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt; m_B;    &lt;span style=&quot;color: #008000&quot;&gt;// upper bound&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   8:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   9:&lt;/span&gt;   &lt;span style=&quot;color: #008000&quot;&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  10:&lt;/span&gt;   &lt;span style=&quot;color: #008000&quot;&gt;/// Creates a new Class1 instance&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  11:&lt;/span&gt;   &lt;span style=&quot;color: #008000&quot;&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  12:&lt;/span&gt;   &lt;span style=&quot;color: #008000&quot;&gt;/// &amp;lt;param name=&amp;quot;a&amp;quot;&amp;gt;the lower bound&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  13:&lt;/span&gt;   &lt;span style=&quot;color: #008000&quot;&gt;/// &amp;lt;param name=&amp;quot;b&amp;quot;&amp;gt;the upper bound&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  14:&lt;/span&gt;   &lt;span style=&quot;color: #0000ff&quot;&gt;public&lt;/span&gt; Bounds(&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt; a, &lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt; b)&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  15:&lt;/span&gt;   {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  16:&lt;/span&gt;     &lt;span style=&quot;color: #008000&quot;&gt;// m_A should be the smallest value&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  17:&lt;/span&gt;     m_A = Math.Min(a, b);&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  18:&lt;/span&gt;     &lt;span style=&quot;color: #008000&quot;&gt;// m_B should be the biggest value&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  19:&lt;/span&gt;     m_B = Math.Max(a, b);&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  20:&lt;/span&gt;   }&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  21:&lt;/span&gt;   …&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  22:&lt;/span&gt; }&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;div style=&quot;border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; background-color: #f4f4f4; max-height: 200px&quot;&gt;
  &lt;div style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;
    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   1:&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;class&lt;/span&gt; Bounds&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   2:&lt;/span&gt; {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   3:&lt;/span&gt;   &lt;span style=&quot;color: #0000ff&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt; m_LowerBound;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   4:&lt;/span&gt;   &lt;span style=&quot;color: #0000ff&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt; m_UpperBound;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   5:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   6:&lt;/span&gt;   &lt;span style=&quot;color: #008000&quot;&gt;/// &amp;lt;remarks&amp;gt;It is OK to provide a &amp;amp; b in the wrong order.&amp;lt;/remarks&amp;gt;&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   7:&lt;/span&gt;   &lt;span style=&quot;color: #0000ff&quot;&gt;public&lt;/span&gt; Bounds(&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt; a, &lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt; b)&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   8:&lt;/span&gt;   {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   9:&lt;/span&gt;     &lt;span style=&quot;color: #008000&quot;&gt;// The rest of this class assumes that m_Lowerbound &amp;lt;= m_Upperbound,&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  10:&lt;/span&gt;     &lt;span style=&quot;color: #008000&quot;&gt;// so we fix it here&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  11:&lt;/span&gt;     m_LowerBound = Math.Min(lowerBound, upperBound);&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  12:&lt;/span&gt;     m_UpperBound = Math.Max(lowerBound, upperBound);&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  13:&lt;/span&gt;   }&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  14:&lt;/span&gt;   …&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  15:&lt;/span&gt; }&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;Although the second example has less comments, I feel that it is better documented because it is self-describing:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;The default class-comment that is inserted by Visual Studio is useless. The name of the class is already sufficient documentation. &lt;/li&gt;

  &lt;li&gt;The second example has good names for the fields; further comments are not necessary. &lt;/li&gt;

  &lt;li&gt;The first example has comments about stuff that is very obvious (every developer should already know what a constructor does). &lt;/li&gt;

  &lt;li&gt;The first example explains the sorting of the parameters but every developer can see what is going on. The second example instead explains &lt;em&gt;why&lt;/em&gt; the parameters need to be sorted. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If your code is written cleanly then you don’t have to document &lt;em&gt;what&lt;/em&gt; it is doing. But you may have to document &lt;em&gt;why&lt;/em&gt; it is doing it.&lt;/p&gt;  </content><link rel='replies' type='application/atom+xml' href='http://kristofverbiest.blogspot.com/feeds/2628504941296048025/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8259613/2628504941296048025' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8259613/posts/default/2628504941296048025'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8259613/posts/default/2628504941296048025'/><link rel='alternate' type='text/html' href='http://kristofverbiest.blogspot.com/2008/08/good-code-is-self-documenting.html' title='Good code is self-documenting'/><author><name>Kristof</name><uri>http://www.blogger.com/profile/01727380410232817527</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8259613.post-6648338743467520529</id><published>2008-08-20T09:51:00.001+02:00</published><updated>2008-11-14T13:46:46.461+01:00</updated><title type='text'>Don’t use a dedicated thread to wait on a kernel object</title><content type='html'>&lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;One of the code patterns in the &lt;a href=&quot;http://kristofverbiest.blogspot.com/2008/08/timers-executing-task-at-regular.html&quot; target=&quot;_blank&quot;&gt;previous post&lt;/a&gt; showed that you don’t need a dedicated thread to execute a piece of code at regular intervals.&lt;/p&gt;  &lt;p&gt;Likewise you don’t need a dedicated thread to wait on a kernel object (such as a mutex or an event). You can use the &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/system.threading.threadpool.aspx&quot; target=&quot;_blank&quot;&gt;threadpool&lt;/a&gt; to do this:&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div style=&quot;border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; background-color: #f4f4f4; max-height: 200px&quot;&gt;   &lt;div style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;     &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   1:&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;class&lt;/span&gt; Test&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   2:&lt;/span&gt; {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   3:&lt;/span&gt;   &lt;span style=&quot;color: #008000&quot;&gt;// This method returns an event. Whenever the event will be set (by the caller of this &lt;/span&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   4:&lt;/span&gt;   &lt;span style=&quot;color: #008000&quot;&gt;// method), the &#39;EventWasSet&#39; method will be executed.&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   5:&lt;/span&gt;   &lt;span style=&quot;color: #0000ff&quot;&gt;public&lt;/span&gt; WaitHandle GetEvent()&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   6:&lt;/span&gt;   {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   7:&lt;/span&gt;     AutoResetEvent m_Event = &lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; AutoResetEvent(&lt;span style=&quot;color: #0000ff&quot;&gt;false&lt;/span&gt;);&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   8:&lt;/span&gt;     &lt;font color=&quot;#ff0000&quot;&gt;ThreadPool.RegisterWaitForSingleObject&lt;/font&gt;(m_Event, EventWasSet, &lt;span style=&quot;color: #0000ff&quot;&gt;null&lt;/span&gt;, Timeout.Infinite,&lt;span style=&quot;color: #0000ff&quot;&gt;false&lt;/span&gt;);&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   9:&lt;/span&gt;     &lt;span style=&quot;color: #0000ff&quot;&gt;return&lt;/span&gt; m_Event;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  10:&lt;/span&gt;   }&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  11:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  12:&lt;/span&gt;   &lt;span style=&quot;color: #0000ff&quot;&gt;void&lt;/span&gt; EventWasSet(&lt;span style=&quot;color: #0000ff&quot;&gt;object&lt;/span&gt; state, &lt;span style=&quot;color: #0000ff&quot;&gt;bool&lt;/span&gt; timeOut)&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  13:&lt;/span&gt;   {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  14:&lt;/span&gt;     &lt;span style=&quot;color: #008000&quot;&gt;// This code will be executed on the threadpool when the event is set&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  15:&lt;/span&gt;   }&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  16:&lt;/span&gt; }&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;The &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/system.threading.threadpool.registerwaitforsingleobject.aspx&quot; target=&quot;_blank&quot;&gt;RegisterWaitForSingleObject()&lt;/a&gt; method makes sure that the given method will be executed on a threadpool thread when the kernel object is signaled.&lt;/p&gt;  </content><link rel='replies' type='application/atom+xml' href='http://kristofverbiest.blogspot.com/feeds/6648338743467520529/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8259613/6648338743467520529' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8259613/posts/default/6648338743467520529'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8259613/posts/default/6648338743467520529'/><link rel='alternate' type='text/html' href='http://kristofverbiest.blogspot.com/2008/08/dont-use-dedicated-thread-to-wait-on.html' title='Don’t use a dedicated thread to wait on a kernel object'/><author><name>Kristof</name><uri>http://www.blogger.com/profile/01727380410232817527</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8259613.post-651597923631215965</id><published>2008-08-13T09:22:00.001+02:00</published><updated>2008-11-14T13:47:08.547+01:00</updated><title type='text'>Timers: executing a task at regular intervals</title><content type='html'>&lt;p&gt;When you want to execute a piece of code at regular intervals, you should not start a dedicated thread to do this. Instead you should use a timer.&lt;/p&gt;  &lt;p&gt;If the task you want to perform involves GUI interaction, it is best to use the &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.aspx&quot; target=&quot;_blank&quot;&gt;Windows.Forms.Timer&lt;/a&gt; class. An example:&lt;/p&gt;  &lt;div style=&quot;border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; background-color: #f4f4f4; max-height: 200px&quot;&gt;   &lt;div style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;     &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   1:&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;class&lt;/span&gt; Test&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   2:&lt;/span&gt; {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   3:&lt;/span&gt;   System.Windows.Forms.Timer m_Timer = &lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; System.Windows.Forms.Timer();&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   4:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   5:&lt;/span&gt;   &lt;span style=&quot;color: #0000ff&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;void&lt;/span&gt; StartTimer()&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   6:&lt;/span&gt;   {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   7:&lt;/span&gt;     m_Timer.Interval = 1000;    &lt;span style=&quot;color: #008000&quot;&gt;// tick every 1000 ms&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   8:&lt;/span&gt;     m_Timer.Tick += Timer_Tick;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   9:&lt;/span&gt;     m_Timer.Start();&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  10:&lt;/span&gt;   }&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  11:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  12:&lt;/span&gt;   &lt;span style=&quot;color: #0000ff&quot;&gt;void&lt;/span&gt; Timer_Tick(&lt;span style=&quot;color: #0000ff&quot;&gt;object&lt;/span&gt; sender, EventArgs e)&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  13:&lt;/span&gt;   {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  14:&lt;/span&gt;     &lt;span style=&quot;color: #008000&quot;&gt;// This code will execute every second on the GUI thread&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  15:&lt;/span&gt;   }&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  16:&lt;/span&gt; }&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;If the code does not involve GUI interaction, it is best to use a &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/system.threading.timer.aspx&quot; target=&quot;_blank&quot;&gt;threadpool-timer&lt;/a&gt;. However you need to be careful: if your code takes longer to execute than the interval of the timer, you will cause threadpool starvation! This occurs if the maximum amount of threadpool-threads is reached. This will cause serious problems for the complete process because all code that uses the threadpool (including .NET Remoting) will be blocked.&lt;/p&gt;

&lt;p&gt;As a workaround you can set the timer to execute only once, and reset it every time it has executed:&lt;/p&gt;

&lt;div style=&quot;border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; background-color: #f4f4f4; max-height: 200px&quot;&gt;
  &lt;div style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;
    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   1:&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;class&lt;/span&gt; Test&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   2:&lt;/span&gt; {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   3:&lt;/span&gt;   System.Threading.Timer m_Timer;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   4:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   5:&lt;/span&gt;   &lt;span style=&quot;color: #0000ff&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;void&lt;/span&gt; StartTimer()&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   6:&lt;/span&gt;   {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   7:&lt;/span&gt;     &lt;span style=&quot;color: #008000&quot;&gt;// set timer to be executed only once&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   8:&lt;/span&gt;     m_Timer = &lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; System.Threading.Timer(Timer_Tick,&lt;span style=&quot;color: #0000ff&quot;&gt;null&lt;/span&gt;,1000,0);&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   9:&lt;/span&gt;   }&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  10:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  11:&lt;/span&gt;   &lt;span style=&quot;color: #0000ff&quot;&gt;void&lt;/span&gt; Timer_Tick(&lt;span style=&quot;color: #0000ff&quot;&gt;object&lt;/span&gt; state)&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  12:&lt;/span&gt;   {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  13:&lt;/span&gt;     &lt;span style=&quot;color: #0000ff&quot;&gt;try&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  14:&lt;/span&gt;     {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  15:&lt;/span&gt;       &lt;span style=&quot;color: #008000&quot;&gt;// This code will execute every second on a threadpool thread&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  16:&lt;/span&gt;     }&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  17:&lt;/span&gt;     &lt;span style=&quot;color: #0000ff&quot;&gt;finally&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  18:&lt;/span&gt;     {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  19:&lt;/span&gt;       &lt;span style=&quot;color: #008000&quot;&gt;// reset the timer&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  20:&lt;/span&gt;       m_Timer.Change(1000, 0);&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  21:&lt;/span&gt;     }&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  22:&lt;/span&gt;   }&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  23:&lt;/span&gt; }&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;So there is no need to start a dedicated thread just to perform a piece of code at regular intervals.&lt;/p&gt;  </content><link rel='replies' type='application/atom+xml' href='http://kristofverbiest.blogspot.com/feeds/651597923631215965/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8259613/651597923631215965' title='9 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8259613/posts/default/651597923631215965'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8259613/posts/default/651597923631215965'/><link rel='alternate' type='text/html' href='http://kristofverbiest.blogspot.com/2008/08/timers-executing-task-at-regular.html' title='Timers: executing a task at regular intervals'/><author><name>Kristof</name><uri>http://www.blogger.com/profile/01727380410232817527</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>9</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8259613.post-1883305479031295283</id><published>2008-08-05T16:35:00.001+02:00</published><updated>2008-11-14T13:47:58.932+01:00</updated><title type='text'>Event subscriptions may lead to memory leaks</title><content type='html'>&lt;p&gt;Even though the .NET runtime has a garbage collector that cleans up unused objects and frees the memory, it is still possible to write code that contains a memory leak. Often this is very subtle and the memory leak is not apparent at first sight.&lt;/p&gt;  &lt;p&gt;One of these subtle memory-leaks may occur if your code subscribes to an event, but never unsubscribes. Whenever a subscriber subscribes to an event that is published by the publisher, the publisher will hold a reference to the subscriber.&lt;/p&gt;  &lt;p&gt;I tried to visualize this in this diagram:&lt;/p&gt;  &lt;p&gt;&lt;a href=&quot;http://lh5.ggpht.com/verbiest/SJhlKNNMsXI/AAAAAAAAAKQ/cdhrJOy4h5I/s1600-h/SubscriberAndPublisher%5B3%5D.gif&quot;&gt;&lt;img title=&quot;SubscriberAndPublisher&quot; height=&quot;81&quot; alt=&quot;SubscriberAndPublisher&quot; src=&quot;http://lh6.ggpht.com/verbiest/SJhlKXjkpnI/AAAAAAAAAKU/yiWPvOuKCAU/SubscriberAndPublisher_thumb%5B1%5D.gif?imgmax=800&quot; width=&quot;335&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;The publisher will hold this reference as long as the event is not unsubscribed. So as long as the publiser is alive, the garbage collector will not collect the subscriber-objects. This leads to a memory leak (or sometimes worse, if the subscriber holds other resources such as files or threads).&lt;/p&gt;  &lt;p&gt;Typically this occurs when the publisher is a static object (which lives as long as the process lives).&lt;/p&gt;  &lt;p&gt;So whenever you subscribe to an event, think whether it is necessary to unsubscribe from this event (especially if the publisher has a long lifetime).&lt;/p&gt;  &lt;p&gt;Of course the same thing occurs in every situation where a static (or long-lived) object has a reference to other (short-lived) objects. Always consider when this reference should be removed again!&lt;/p&gt;  </content><link rel='replies' type='application/atom+xml' href='http://kristofverbiest.blogspot.com/feeds/1883305479031295283/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8259613/1883305479031295283' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8259613/posts/default/1883305479031295283'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8259613/posts/default/1883305479031295283'/><link rel='alternate' type='text/html' href='http://kristofverbiest.blogspot.com/2008/08/event-subscriptions-may-lead-to-memory.html' title='Event subscriptions may lead to memory leaks'/><author><name>Kristof</name><uri>http://www.blogger.com/profile/01727380410232817527</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh6.ggpht.com/verbiest/SJhlKXjkpnI/AAAAAAAAAKU/yiWPvOuKCAU/s72-c/SubscriberAndPublisher_thumb%5B1%5D.gif?imgmax=800" height="72" width="72"/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8259613.post-3092120352586201774</id><published>2008-08-05T16:06:00.001+02:00</published><updated>2008-11-14T13:48:20.382+01:00</updated><title type='text'>Don’t start a dedicated thread to perform a simple task</title><content type='html'>&lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;When you want to perform a piece of code asynchronously it is tempting to start a dedicated thread to execute this code. However, starting a new thread has a big performance overhead. For simple tasks this overhead cannot be justified (the CPU-time needed to start the thread might be much bigger than the actual processing that will occur in the thread). Microsoft recognizes this and provides the &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/system.threading.threadpool.aspx&quot; target=&quot;_blank&quot;&gt;Threadpool&lt;/a&gt; which is much better for executing tasks asynchronously.&lt;/p&gt;  &lt;p&gt;I decided to write a small benchmark to find out just how bad it is to start a dedicated thread for a simple task. The code of the task is very simple: increment a counter and check the value of this counter. This task has to be executed 1,000,000 times. Of course this is not actual code that you would write; it is just the simplest task that I could think of to execute asynchronously.    &lt;br /&gt;Because the counter will be accessed from multiple threads it needs to be protected by a lock. In this particular code it is possible to avoid the lock using the System.Threading.Interlocked class. I have decided not to do this because the use of a normal lock makes the benchmark much more relevant towards real-world scenarios.&lt;/p&gt;  &lt;p&gt;Here is the version of the code that uses a dedicated thread for each task (error-handling code and the code that was used to measure the performance has been removed for clarity):&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div style=&quot;border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; background-color: #f4f4f4; max-height: 200px&quot;&gt;   &lt;div style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;     &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   1:&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;class&lt;/span&gt; Program &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   2:&lt;/span&gt; { &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   3:&lt;/span&gt;         &lt;span style=&quot;color: #0000ff&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt; c_Loops = 1000000; &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   4:&lt;/span&gt;         &lt;span style=&quot;color: #0000ff&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;uint&lt;/span&gt; s_Counter;        &lt;span style=&quot;color: #008000&quot;&gt;// the counter that will be incremented by every task &lt;/span&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   5:&lt;/span&gt;         &lt;span style=&quot;color: #0000ff&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;object&lt;/span&gt; s_Lock = &lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;object&lt;/span&gt;();        &lt;span style=&quot;color: #008000&quot;&gt;// needed because the counter is accessed from multiple threads &lt;/span&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   6:&lt;/span&gt;         &lt;span style=&quot;color: #0000ff&quot;&gt;static&lt;/span&gt; AutoResetEvent s_TestFinished = &lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; AutoResetEvent(&lt;span style=&quot;color: #0000ff&quot;&gt;false&lt;/span&gt;);        &lt;span style=&quot;color: #008000&quot;&gt;// signals when the test is finished &lt;/span&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   7:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   8:&lt;/span&gt;         &lt;span style=&quot;color: #0000ff&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;void&lt;/span&gt; Main(&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;[] args) &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   9:&lt;/span&gt;         { &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  10:&lt;/span&gt;                 &lt;span style=&quot;color: #0000ff&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt; counter = 0; counter &amp;lt; c_Loops; counter++) &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  11:&lt;/span&gt;                 { &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  12:&lt;/span&gt;                         Thread t = &lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; Thread(&lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; ThreadStart(SmallTask)); &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  13:&lt;/span&gt;                         t.Start(); &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  14:&lt;/span&gt;                 } &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  15:&lt;/span&gt;                 s_TestFinished.WaitOne();        &lt;span style=&quot;color: #008000&quot;&gt;// we wait until the last task has finished &lt;/span&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  16:&lt;/span&gt;         } &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  17:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  18:&lt;/span&gt;         &lt;span style=&quot;color: #0000ff&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;void&lt;/span&gt; SmallTask() &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  19:&lt;/span&gt;         { &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  20:&lt;/span&gt;                 &lt;span style=&quot;color: #008000&quot;&gt;// This method executes on a dedicated thread &lt;/span&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  21:&lt;/span&gt;                 &lt;span style=&quot;color: #0000ff&quot;&gt;lock&lt;/span&gt; (s_Lock) &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  22:&lt;/span&gt;                 { &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  23:&lt;/span&gt;                         s_Counter++; &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  24:&lt;/span&gt;                         &lt;span style=&quot;color: #0000ff&quot;&gt;if&lt;/span&gt; (s_Counter == c_Loops) s_TestFinished.Set();        &lt;span style=&quot;color: #008000&quot;&gt;// notify the main thread that the test is finished &lt;/span&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  25:&lt;/span&gt;                 } &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  26:&lt;/span&gt;         } &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  27:&lt;/span&gt; } &lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;These are the performance measurements (taken on a dual-core processor):&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Total time to execute all the tasks: 18312 ms &lt;/li&gt;

  &lt;li&gt;% time in user-mode: 47% &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As you can see it takes quite some time to execute all the tasks. Only about half of this time is spent in user mode, the rest is spent in kernel mode.&lt;/p&gt;

&lt;p&gt;Now let&#39;s rewrite this code using the threadpool. Only minimal changes are needed (they are shown in red):&lt;/p&gt;

&lt;div style=&quot;border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; background-color: #f4f4f4; max-height: 200px&quot;&gt;
  &lt;div style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;
    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   1:&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;class&lt;/span&gt; Program &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   2:&lt;/span&gt; { &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   3:&lt;/span&gt;         &lt;span style=&quot;color: #0000ff&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt; c_Loops = 100000; &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   4:&lt;/span&gt;         &lt;span style=&quot;color: #0000ff&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;uint&lt;/span&gt; s_Counter;        &lt;span style=&quot;color: #008000&quot;&gt;// the counter that will be incremented by every task &lt;/span&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   5:&lt;/span&gt;         &lt;span style=&quot;color: #0000ff&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;object&lt;/span&gt; s_Lock = &lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;object&lt;/span&gt;();        &lt;span style=&quot;color: #008000&quot;&gt;// needed because the counter is accessed from multiple threads &lt;/span&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   6:&lt;/span&gt;         &lt;span style=&quot;color: #0000ff&quot;&gt;static&lt;/span&gt; AutoResetEvent s_TestFinished = &lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; AutoResetEvent(&lt;span style=&quot;color: #0000ff&quot;&gt;false&lt;/span&gt;);        &lt;span style=&quot;color: #008000&quot;&gt;// signals when the test is finished &lt;/span&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   7:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   8:&lt;/span&gt;         &lt;span style=&quot;color: #0000ff&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;void&lt;/span&gt; Main(&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;[] args) &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   9:&lt;/span&gt;         { &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  10:&lt;/span&gt;                 &lt;span style=&quot;color: #0000ff&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt; counter = 0; counter &amp;lt; c_Loops; counter++) &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  11:&lt;/span&gt;                 { &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  12:&lt;/span&gt;                        &lt;font color=&quot;#ff0000&quot;&gt; ThreadPool.QueueUserWorkItem(new WaitCallback(SmallTask));&lt;/font&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  13:&lt;/span&gt;                 } &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  14:&lt;/span&gt;                 s_TestFinished.WaitOne();        &lt;span style=&quot;color: #008000&quot;&gt;// we wait until the last task has finished &lt;/span&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  15:&lt;/span&gt;         } &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  16:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  17:&lt;/span&gt;         &lt;span style=&quot;color: #0000ff&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;void&lt;/span&gt; SmallTask(&lt;font color=&quot;#ff0000&quot;&gt;object state&lt;/font&gt;) &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  18:&lt;/span&gt;         { &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  19:&lt;/span&gt;                 &lt;span style=&quot;color: #008000&quot;&gt;// This method executes on a threadpool thread &lt;/span&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  20:&lt;/span&gt;                 &lt;span style=&quot;color: #0000ff&quot;&gt;lock&lt;/span&gt; (s_Lock) &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  21:&lt;/span&gt;                 { &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  22:&lt;/span&gt;                         s_Counter++; &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  23:&lt;/span&gt;                         &lt;span style=&quot;color: #0000ff&quot;&gt;if&lt;/span&gt; (s_Counter == c_Loops) s_TestFinished.Set(); &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  24:&lt;/span&gt;                 } &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  25:&lt;/span&gt;         } &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;  26:&lt;/span&gt; } &lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;These are the performance measurements:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Total time to execute all the tasks: 218 ms. &lt;/li&gt;

  &lt;li&gt;% time in user-mode: 98% &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So we see that by using the Treadpool we have increased the performance 82 times! Also, almost all time is spent in user-mode.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion: when executing simple tasks asynchronously, starting a dedicated thread for each task has a very big performance impact.&lt;/strong&gt;&lt;/p&gt;  </content><link rel='replies' type='application/atom+xml' href='http://kristofverbiest.blogspot.com/feeds/3092120352586201774/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8259613/3092120352586201774' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8259613/posts/default/3092120352586201774'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8259613/posts/default/3092120352586201774'/><link rel='alternate' type='text/html' href='http://kristofverbiest.blogspot.com/2008/08/dont-start-dedicated-thread-to-perform.html' title='Don’t start a dedicated thread to perform a simple task'/><author><name>Kristof</name><uri>http://www.blogger.com/profile/01727380410232817527</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8259613.post-2103734166958075823</id><published>2008-08-05T15:44:00.001+02:00</published><updated>2008-11-14T13:48:40.203+01:00</updated><title type='text'>An even better pattern to raise events</title><content type='html'>&lt;p&gt;In a previous post, I explained a subtle bug in the way that events are raised in most code. This suble bug might cause a race condition that results in unexpected behavior when the code is executed in multiple threads.&lt;/p&gt;  &lt;p&gt;You can read the details &lt;a href=&quot;http://kristofverbiest.blogspot.com/2006/08/better-way-to-raise-events.html&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;From .NET 2 onwards, there is an even simple pattern to solve this problem:&lt;/p&gt;  &lt;div style=&quot;border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; background-color: #f4f4f4; max-height: 200px&quot;&gt;   &lt;div style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;     &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   1:&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;class&lt;/span&gt; Publisher&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   2:&lt;/span&gt; {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   3:&lt;/span&gt;   &lt;span style=&quot;color: #0000ff&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;event&lt;/span&gt; EventHandler SomethingHappened = &lt;span style=&quot;color: #0000ff&quot;&gt;delegate&lt;/span&gt; { }; &lt;span style=&quot;color: #008000&quot;&gt;// cannot be null, important for thread safety&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   4:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   5:&lt;/span&gt;   &lt;span style=&quot;color: #0000ff&quot;&gt;void&lt;/span&gt; Test()&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   6:&lt;/span&gt;   {&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   7:&lt;/span&gt;     SomethingHappened(&lt;span style=&quot;color: #0000ff&quot;&gt;this&lt;/span&gt;, &lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; EventArgs());&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   8:&lt;/span&gt;   }&lt;/pre&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #606060&quot;&gt;   9:&lt;/span&gt; }&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;The trick is that the event is initialized to an empty delegate (this was not possible before .NET 2.0). When invoked, this empty delegate does nothing. However it guarantees that the event will never become null, so the race condition cannot occur.&lt;/p&gt;

&lt;p&gt;So using this pattern it is always safe to raise the event and it is even not necessary anymore to check if the event is null.&lt;/p&gt;  </content><link rel='replies' type='application/atom+xml' href='http://kristofverbiest.blogspot.com/feeds/2103734166958075823/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8259613/2103734166958075823' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8259613/posts/default/2103734166958075823'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8259613/posts/default/2103734166958075823'/><link rel='alternate' type='text/html' href='http://kristofverbiest.blogspot.com/2008/08/even-better-pattern-to-raise-events.html' title='An even better pattern to raise events'/><author><name>Kristof</name><uri>http://www.blogger.com/profile/01727380410232817527</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8259613.post-7949509998309011667</id><published>2007-02-27T14:50:00.001+01:00</published><updated>2008-11-14T13:49:31.659+01:00</updated><title type='text'>Avoid Invoke(), prefer BeginInvoke()</title><content type='html'>&lt;p&gt;This is another post in a series that deals with asynchronous behaviour (see also &lt;a href=&quot;http://kristofverbiest.blogspot.com/2007/02/simple-pattern-to-invoke-gui-from.html&quot;&gt;here&lt;/a&gt; and &lt;a href=&quot;http://kristofverbiest.blogspot.com/2007/02/don-confuse-controlbegininvoke-with.html&quot;&gt;here&lt;/a&gt;).&lt;/p&gt;  &lt;p&gt;The difference between Control.Invoke() and Control.BeginInvoke() is:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;BeginInvoke() will &lt;em&gt;schedule&lt;/em&gt; the asynchronous action on the GUI thread. When the asynchronous action is scheduled, your code continues. Some time later (you don&#39;t know exactly when) your asynchronous action will be executed. &lt;/li&gt;    &lt;li&gt;Invoke() will &lt;em&gt;execute&lt;/em&gt; your asynchronous action (on the GUI thread) and &lt;strong&gt;wait until your action has completed&lt;/strong&gt;. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;A logical conclusion is that a delegate you pass to Invoke() can have out-parameters or a return-value, while a delegate you pass to BeginInvoke cannot (you have to use EndInvoke to retrieve the results).&lt;/p&gt;  &lt;p&gt;My advice is: &lt;strong&gt;always use BeginInvoke() if you can&lt;/strong&gt;. Reasons:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Because Invoke() causes one thread to wait for another thread, it is likely to cause &lt;a href=&quot;http://en.wikipedia.org/wiki/Deadlock&quot;&gt;deadlocks&lt;/a&gt;. Imagine that your application has a dedicated worker thread that uses Invoke() to execute some work on the GUI thread. Now image that during this work, your GUI code needs to execute something on the worker-thread. It will be blocked forever because your worker-thread is already busy waiting for your GUI-thread.       &lt;br /&gt;In this simple example it might be easy to spot the problem. In a big application developed by many people it will not always be this easy. Therefore it is better &lt;strong&gt;to find a pattern that avoids blocking altogethe&lt;/strong&gt;r.       &lt;br /&gt;      &lt;br /&gt;Note that in this example you might think that a threadpool solves your problem, because then you have multiple workerthreads. However the problem will still occur under high load, this is called threadpool starvation and it is a very complex problem. Because it only occurs under high load, it is a very dangerous thing that is very difficult to debug.       &lt;br /&gt;&lt;/li&gt;    &lt;li&gt;Invoke() will cause forced threadswitches that will decrease the scalability of your application.      &lt;br /&gt;      &lt;br /&gt;First consider BeginInvoke: when it is called, a delegate is &lt;em&gt;scheduled&lt;/em&gt; to be executed on the other thread. However your current thread will continue until the operating system decides that it&#39;s done enough work. Only then will the other thread (GUI thread) be executed, and your delegate will be run. If other delegates were also scheduled using BeginInvoke(), they will be executed as well.       &lt;br /&gt;      &lt;br /&gt;Now consider Invoke: when you call it, the OS &lt;em&gt;immedeately&lt;/em&gt; has to stop your current thread and schedule the other thread.       &lt;br /&gt;When more than one Invoke()-statement is done, a threadswitch will be needed every single time (whereas multiple BeginInvoke-requests can be bundled in one threadswitch).       &lt;br /&gt;Excessive threadswitches will seriously hurt performance when your application is under a high load. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Conclusion: avoid using Invoke().&lt;/p&gt;  </content><link rel='replies' type='application/atom+xml' href='http://kristofverbiest.blogspot.com/feeds/7949509998309011667/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8259613/7949509998309011667' title='20 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8259613/posts/default/7949509998309011667'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8259613/posts/default/7949509998309011667'/><link rel='alternate' type='text/html' href='http://kristofverbiest.blogspot.com/2007/02/avoid-invoke-prefer-begininvoke.html' title='Avoid Invoke(), prefer BeginInvoke()'/><author><name>Kristof</name><uri>http://www.blogger.com/profile/01727380410232817527</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>20</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8259613.post-632274858682728489</id><published>2007-02-15T13:25:00.001+01:00</published><updated>2008-11-14T13:49:49.889+01:00</updated><title type='text'>Don&amp;#39;t confuse Control.BeginInvoke() with Delegate.BeginInvoke()</title><content type='html'>&lt;p&gt;In my &lt;a href=&quot;http://kristofverbiest.blogspot.com/2007/02/simple-pattern-to-invoke-gui-from.html&quot;&gt;last article&lt;/a&gt; I used the BeginInvoke() method to execute a delegate on the GUI thread.&lt;/p&gt;  &lt;p&gt;Here is the definition of the delegate (this is just an example):&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div class=&quot;wlWriterSmartContent&quot; id=&quot;57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:f5f48fb1-6305-4de1-b9cd-0e84bcaa0ab8&quot; style=&quot;padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px&quot;&gt;   &lt;pre style=&quot;background-color: silver&quot;&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;delegate&lt;/span&gt;&lt;span style=&quot;color: #000000&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: #000000&quot;&gt; Invoker(&lt;/span&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color: #000000&quot;&gt; parameter);&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;And here you see where the delegate is used:&lt;/p&gt;

&lt;div class=&quot;wlWriterSmartContent&quot; id=&quot;57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:78eb14b0-8940-40ec-bed5-099cf1089c40&quot; style=&quot;padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px&quot;&gt;
  &lt;pre style=&quot;background-color: silver&quot;&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style=&quot;color: #008000&quot;&gt;//&lt;/span&gt;&lt;span style=&quot;color: #008000&quot;&gt; Execute on the GUI thread&lt;/span&gt;&lt;span style=&quot;color: #008000&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;color: #000000&quot;&gt;.BeginInvoke(&lt;/span&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt;&lt;span style=&quot;color: #000000&quot;&gt; Invoker(SetTitleSafe), title);&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Some people may confuse this with the BeginInvoke-method on a delegate, like this:&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;div class=&quot;wlWriterSmartContent&quot; id=&quot;57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:1381870a-2249-4e4b-a6c2-00fdc296948c&quot; style=&quot;padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px&quot;&gt;
  &lt;pre style=&quot;background-color: silver&quot;&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style=&quot;color: #000000&quot;&gt;Invoker i &lt;/span&gt;&lt;span style=&quot;color: #000000&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #000000&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt;&lt;span style=&quot;color: #000000&quot;&gt; Invoker(SetTitleSafe);
&lt;/span&gt;&lt;span style=&quot;color: #008000&quot;&gt;//&lt;/span&gt;&lt;span style=&quot;color: #008000&quot;&gt; Execute on a threadpool thread&lt;/span&gt;&lt;span style=&quot;color: #008000&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: #000000&quot;&gt;i.BeginInvoke();&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note however that these are two completely different concepts!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Control.BeginInvoke is used to execute code &lt;em&gt;on the GUI thread&lt;/em&gt;, while Delegate.BeginInvoke is used to execute code &lt;em&gt;on a threadpool thread&lt;/em&gt;. For more information, click &lt;a href=&quot;http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpovrasynchronousprogrammingoverview.asp&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;One important difference is that when you invoked &lt;a href=&quot;http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.begininvoke.aspx&quot;&gt;Control.BeginInvoke&lt;/a&gt; you are not obliged to call &lt;a href=&quot;http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.endinvoke.aspx&quot;&gt;Control.EndInvoke&lt;/a&gt;. &lt;strong&gt;However, when you call Delegate.BeginInvoke you should also call Delegate.EndInvoke, otherwise you will have a memory leak!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This obligation to call EndInvoke may likely complicate your code. Mike Woodring has written a &lt;a href=&quot;http://www.bearcanyon.com/dotnet/#FireAndForget&quot;&gt;nice helper-class&lt;/a&gt; that will help you with this. Or you can also use &lt;a href=&quot;http://msdn2.microsoft.com/en-us/library/system.threading.threadpool.queueuserworkitem.aspx&quot;&gt;Threadpool.QueueUserWorkItem&lt;/a&gt; (this is also used in the back by Delegate.BeginInvoke).&lt;/p&gt;  </content><link rel='replies' type='application/atom+xml' href='http://kristofverbiest.blogspot.com/feeds/632274858682728489/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8259613/632274858682728489' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8259613/posts/default/632274858682728489'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8259613/posts/default/632274858682728489'/><link rel='alternate' type='text/html' href='http://kristofverbiest.blogspot.com/2007/02/don-confuse-controlbegininvoke-with.html' title='Don&amp;#39;t confuse Control.BeginInvoke() with Delegate.BeginInvoke()'/><author><name>Kristof</name><uri>http://www.blogger.com/profile/01727380410232817527</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8259613.post-7306217815258567125</id><published>2007-02-13T14:56:00.001+01:00</published><updated>2008-11-14T13:50:09.647+01:00</updated><title type='text'>Simple pattern to invoke GUI from another thread</title><content type='html'>&lt;p&gt;&lt;/p&gt; &lt;h5&gt;If you are making a GUI application and you are using multiple threads, there is one very important rule: &lt;b&gt;GUI controls can only be accessed from the GUI thread&lt;/b&gt;. This is inherent to Windows development and is not a limitation of .NET.&lt;/h5&gt; &lt;p&gt;Let me first say that although this seems to be an annoyance for the developer, this is actually a great thing! It means that while you are developing a GUI application you generally don&#39;t have to worry about threading issues (locks, deadlocks) because you know that all GUI-access is done from a single thread.  &lt;p&gt;But of course there are situations where you want to start background processing on a seperate thread and access the GUI from this thread. Take for instance&amp;nbsp;this simple example:  &lt;p&gt;&lt;/p&gt; &lt;div class=&quot;wlWriterSmartContent&quot; id=&quot;57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:3900deb5-1ac3-4393-a569-c1c69ad97414&quot; contenteditable=&quot;false&quot; style=&quot;padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px&quot;&gt;&lt;pre style=&quot;background-color:Silver;&quot;&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style=&quot;color: #0000FF; &quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color: #000000; &quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #0000FF; &quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: #000000; &quot;&gt; SetTitleUnsafe(&lt;/span&gt;&lt;span style=&quot;color: #0000FF; &quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color: #000000; &quot;&gt; title)
{
    &lt;/span&gt;&lt;span style=&quot;color: #008000; &quot;&gt;//&lt;/span&gt;&lt;span style=&quot;color: #008000; &quot;&gt; When invoked from another thread, this next statement is illegal:&lt;/span&gt;&lt;span style=&quot;color: #008000; &quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: #000000; &quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #0000FF; &quot;&gt;this&lt;/span&gt;&lt;span style=&quot;color: #000000; &quot;&gt;.Text &lt;/span&gt;&lt;span style=&quot;color: #000000; &quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #000000; &quot;&gt; title;
}
&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;If you call this method from a thread that is not a GUI thread, everything may seem to go well at first sight. But because we violated&amp;nbsp;the very important rule, the behaviour of our application is now undefined. Things may (and will) start to go wrong very unpredictably, sometimes much later when there is no obvious relationship with the violation that was made. This makes this&amp;nbsp;problem very hard to find. Among the things that could happen is GUI-events to become &#39;lost&#39; and the GUI to become unresponsive. 
&lt;p&gt;Luckily, form Visual Studio 2005 onward you get a nice error-message when you violate this rule while the debugger is attached: 
&lt;p&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh95OGFD55Y7SngFP44MBbUhCyxohYW_5KQmP93xYlhTWsit9OWlvwwgh9fQoKFqbuf56r6T-bCMCyfWAXHOGcJXVGF6rI_bljwQbBbLW9aa2vOHb5Ep1HCT3-oCTHxS5jwk5BH/s320/exception.JPG&quot;&gt; 
&lt;p&gt;So now you at least get an immedeate notification that you made a mistake. This one of the reasons why I advise to run your code from Visual Studio (using F5) while you are developing.
&lt;p&gt;To get around our threading-violation, Winforms provides these helper-methods: &lt;a href=&quot;http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.begininvoke.aspx&quot;&gt;BeginInvoke&lt;/a&gt;, &lt;a href=&quot;http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.endinvoke.aspx&quot;&gt;EndInvoke&lt;/a&gt;, &lt;a href=&quot;http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.invoke.aspx&quot;&gt;Invoke&lt;/a&gt;, &lt;a href=&quot;http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.invokerequired.aspx&quot;&gt;InvokeRequired&lt;/a&gt;. But even with these methods available it may not be obvious how to use them in a correct and simple way.&lt;/p&gt;
&lt;p&gt;That&#39;s why I present this pattern:&lt;/p&gt;
&lt;p&gt;
&lt;div class=&quot;wlWriterSmartContent&quot; id=&quot;57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:4830936f-685a-41c2-82b4-3afb2c99eda8&quot; contenteditable=&quot;false&quot; style=&quot;padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px&quot;&gt;&lt;pre style=&quot;background-color:Silver;&quot;&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style=&quot;color: #0000FF; &quot;&gt;delegate&lt;/span&gt;&lt;span style=&quot;color: #000000; &quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #0000FF; &quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: #000000; &quot;&gt; Invoker(&lt;/span&gt;&lt;span style=&quot;color: #0000FF; &quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color: #000000; &quot;&gt; parameter);

&lt;/span&gt;&lt;span style=&quot;color: #0000FF; &quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color: #000000; &quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #0000FF; &quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: #000000; &quot;&gt; SetTitleSafe(&lt;/span&gt;&lt;span style=&quot;color: #0000FF; &quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color: #000000; &quot;&gt; title)
{
    &lt;/span&gt;&lt;span style=&quot;color: #0000FF; &quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #000000; &quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color: #0000FF; &quot;&gt;this&lt;/span&gt;&lt;span style=&quot;color: #000000; &quot;&gt;.InvokeRequired)
    {
        &lt;/span&gt;&lt;span style=&quot;color: #008000; &quot;&gt;//&lt;/span&gt;&lt;span style=&quot;color: #008000; &quot;&gt; Execute the same method, but this time on the GUI thread&lt;/span&gt;&lt;span style=&quot;color: #008000; &quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: #000000; &quot;&gt;        &lt;/span&gt;&lt;span style=&quot;color: #0000FF; &quot;&gt;this&lt;/span&gt;&lt;span style=&quot;color: #000000; &quot;&gt;.BeginInvoke(&lt;/span&gt;&lt;span style=&quot;color: #0000FF; &quot;&gt;new&lt;/span&gt;&lt;span style=&quot;color: #000000; &quot;&gt; Invoker(SetTitleSafe), title);

        &lt;/span&gt;&lt;span style=&quot;color: #008000; &quot;&gt;//&lt;/span&gt;&lt;span style=&quot;color: #008000; &quot;&gt; we return immedeately&lt;/span&gt;&lt;span style=&quot;color: #008000; &quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: #000000; &quot;&gt;        &lt;/span&gt;&lt;span style=&quot;color: #0000FF; &quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: #000000; &quot;&gt;;
    }

    &lt;/span&gt;&lt;span style=&quot;color: #008000; &quot;&gt;//&lt;/span&gt;&lt;span style=&quot;color: #008000; &quot;&gt; From here on it is safe to access methods and properties on the GUI
    &lt;/span&gt;&lt;span style=&quot;color: #008000; &quot;&gt;//&lt;/span&gt;&lt;span style=&quot;color: #008000; &quot;&gt; For example:&lt;/span&gt;&lt;span style=&quot;color: #008000; &quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: #000000; &quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #0000FF; &quot;&gt;this&lt;/span&gt;&lt;span style=&quot;color: #000000; &quot;&gt;.Text &lt;/span&gt;&lt;span style=&quot;color: #000000; &quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #000000; &quot;&gt; title;
}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;
&lt;p&gt;As you can see, you will need to define a delegate that matches your method (or use an existing delegate that is defined elsewhere, such as the System.Windows.Forms.MethodInvoker). This is how the pattern works:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;When the method is called from a thread that is not the GUI thread, InvokeRequired will be true. The method will be wrapped in a delegate and passed to the BeginInvoke method (together with the parameters). We return immedeately. BeginInvoke guarantees that some time later our method will be called on the GUI thread.&lt;/li&gt;
&lt;li&gt;When the method is called on the GUI thread, InvokeRequired returns false so we just go forward and access the GUI in any way we like.&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;This is in my opinion the simplest and shortest pattern that is always correct.&lt;/p&gt;
&lt;p&gt;From&amp;nbsp;.NET 2.0 onwards the same pattern can be written using an anonymous method but that is less readable in my opinion, so I prefer to keep this pattern.&lt;/p&gt;
&lt;p&gt;Also in .NET 2.0 you can use the &lt;a href=&quot;http://msdn2.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx&quot;&gt;BackgroundWorker&lt;/a&gt;&amp;nbsp;class that handles all the details behind your back. But the same principle still applies: never access GUI from another thread!&lt;/p&gt;  </content><link rel='replies' type='application/atom+xml' href='http://kristofverbiest.blogspot.com/feeds/7306217815258567125/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8259613/7306217815258567125' title='11 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8259613/posts/default/7306217815258567125'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8259613/posts/default/7306217815258567125'/><link rel='alternate' type='text/html' href='http://kristofverbiest.blogspot.com/2007/02/simple-pattern-to-invoke-gui-from.html' title='Simple pattern to invoke GUI from another thread'/><author><name>Kristof</name><uri>http://www.blogger.com/profile/01727380410232817527</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh95OGFD55Y7SngFP44MBbUhCyxohYW_5KQmP93xYlhTWsit9OWlvwwgh9fQoKFqbuf56r6T-bCMCyfWAXHOGcJXVGF6rI_bljwQbBbLW9aa2vOHb5Ep1HCT3-oCTHxS5jwk5BH/s72-c/exception.JPG" height="72" width="72"/><thr:total>11</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8259613.post-117043292502514006</id><published>2007-02-02T17:15:00.000+01:00</published><updated>2007-02-02T17:15:25.056+01:00</updated><title type='text'>Rethrow an exception using throw;</title><content type='html'>&lt;p&gt;It is not uncommon that you want to rethrow an exception in a catch-block. This happens if you need to cleanup some state in the catch-handler, but you still want to inform the application that something has gone wrong.&lt;/p&gt; &lt;p&gt;You often see this implemented as such:&lt;/p&gt; &lt;p&gt;&lt;font face=&quot;Courier New&quot;&gt;&lt;font color=&quot;#0000ff&quot;&gt;static void&lt;/font&gt; SomeMethod()&lt;br&gt;{&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=&quot;#0000ff&quot;&gt;try&lt;/font&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; MethodThatCanThrowAnException();&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=&quot;#0000ff&quot;&gt;catch&lt;/font&gt; (Exception ex)&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=&quot;#004000&quot;&gt;&lt;font color=&quot;#008000&quot;&gt;// Do stuff (cleanup)&lt;/font&gt;&lt;br&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=&quot;#0000ff&quot;&gt;throw&lt;/font&gt; ex; &lt;font color=&quot;#004000&quot;&gt;&lt;font color=&quot;#008000&quot;&gt;// WRONG - DO NOT USE THIS!&lt;/font&gt;&lt;br&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;}&lt;/font&gt;&lt;/p&gt; &lt;p&gt;There is a subtle problem with this: rethrowing the existing exception-object will reset its stacktrace. Whoever catches this exception and examines the stacktrace will think that it was caused by your code. &lt;/p&gt; &lt;p&gt;It is better to do this:&lt;/p&gt; &lt;p&gt;&lt;font face=&quot;Courier New&quot;&gt;&lt;font color=&quot;#0000ff&quot;&gt;static void&lt;/font&gt; SomeMethod()&lt;br&gt;{&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=&quot;#0000ff&quot;&gt;try&lt;/font&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; MethodThatCanThrowAnException();&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=&quot;#0000ff&quot;&gt;catch&lt;/font&gt; (Exception ex)&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=&quot;#004000&quot;&gt;&lt;font color=&quot;#008000&quot;&gt;// Do stuff (cleanup)&lt;/font&gt;&lt;br&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=&quot;#0000ff&quot;&gt;throw&lt;/font&gt;; &lt;font color=&quot;#004000&quot;&gt;&lt;font color=&quot;#008000&quot;&gt;//&amp;nbsp;does not reset the stacktrace&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face=&quot;Courier New&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;}&lt;/font&gt;&lt;/p&gt; &lt;p&gt;Now whoever catches this exception will see the complete stacktrace and can see the originator who caused the exception. This might save a lot of time for whoever has to debug the problem.&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;Another valid&amp;nbsp;possibility is to throw a new exception, but set its InnerException propery to the original exception like this:&lt;/p&gt; &lt;p&gt;&lt;font face=&quot;Courier New&quot;&gt;&lt;font color=&quot;#0000ff&quot;&gt;catch&lt;/font&gt; (NullReferenceException ex)&lt;br&gt;{&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=&quot;#004000&quot;&gt;&lt;font color=&quot;#008000&quot;&gt;// Do stuff (cleanup)&lt;/font&gt;&lt;br&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=&quot;#0000ff&quot;&gt;throw new &lt;/font&gt;&lt;font color=&quot;#000000&quot;&gt;ObjectNotInitializedException(ex)&lt;/font&gt;;&lt;font color=&quot;#004000&quot;&gt;&lt;font color=&quot;#008000&quot;&gt;&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face=&quot;Courier New&quot;&gt;}&lt;br&gt;&lt;/font&gt;&lt;font face=&quot;Courier New&quot;&gt;&lt;font face=&quot;Trebuchet MS&quot;&gt;Whoever catches this exception can view a separate stacktrace for your exception and the InnerException. I would use this second pattern&amp;nbsp;if the&lt;/font&gt;&lt;/font&gt;&lt;font face=&quot;Courier New&quot;&gt;&lt;font face=&quot;Trebuchet MS&quot;&gt;&amp;nbsp;new exception that you throw contains more information about the cause of the problem than the inner-exception. For instance if the inner-exception is a NullReferenceException, your exception can give more context (e.g. the object is not yet initialized).&lt;/font&gt;&lt;/font&gt;&lt;/p&gt; &lt;p&gt;Kristof&lt;font face=&quot;Courier New&quot;&gt;&lt;/p&gt;&lt;/font&gt;</content><link rel='replies' type='application/atom+xml' href='http://kristofverbiest.blogspot.com/feeds/117043292502514006/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8259613/117043292502514006' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8259613/posts/default/117043292502514006'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8259613/posts/default/117043292502514006'/><link rel='alternate' type='text/html' href='http://kristofverbiest.blogspot.com/2007/02/rethrow-exception-using-throw.html' title='Rethrow an exception using throw;'/><author><name>Kristof</name><uri>http://www.blogger.com/profile/01727380410232817527</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>6</thr:total></entry></feed>