<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>iDevForFun</title>
	<atom:link href="http://www.idevforfun.com/index.php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.idevforfun.com</link>
	<description>.NET Development</description>
	<lastBuildDate>Thu, 10 Jun 2010 20:53:42 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Visual Studio Power Commands</title>
		<link>http://www.idevforfun.com/index.php/2010/06/10/visual-studio-power-commands/</link>
		<comments>http://www.idevforfun.com/index.php/2010/06/10/visual-studio-power-commands/#comments</comments>
		<pubDate>Thu, 10 Jun 2010 20:53:42 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[DotNet]]></category>

		<guid isPermaLink="false">http://www.idevforfun.com/?p=98</guid>
		<description><![CDATA[Power Commands is a free and extremely useful plugin for visual studio. Among its many features is &#8216;Collapse All&#8217; so that you can collapse the solution tree in the solution explorer and &#8216;Open Command Window&#8217; to open a command window in the directory of the selected project. These are the two features I use the ]]></description>
			<content:encoded><![CDATA[<p><a href="http://code.msdn.microsoft.com/PowerCommands">Power Commands</a> is a free and extremely useful plugin for visual studio. Among its many features is &#8216;Collapse All&#8217; so that you can collapse the solution tree in the solution explorer and &#8216;Open Command Window&#8217; to open a command window in the directory of the selected project. These are the two features I use the most and prove invaluable as the solution I use at work has 91 projects attached to it &#8230; this can be really painful to navigate when a number of the projects are expanded. Well worth the download &#8230; </p>
<p><a href="http://code.msdn.microsoft.com/PowerCommands">Download Here</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.idevforfun.com/index.php/2010/06/10/visual-studio-power-commands/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL Search</title>
		<link>http://www.idevforfun.com/index.php/2010/03/30/sql-search/</link>
		<comments>http://www.idevforfun.com/index.php/2010/03/30/sql-search/#comments</comments>
		<pubDate>Tue, 30 Mar 2010 17:55:07 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Sql]]></category>

		<guid isPermaLink="false">http://www.idevforfun.com/?p=91</guid>
		<description><![CDATA[Ever wanted to be able to search your sql databases for procedure names, table names etc as you would with a file search? Well now you can with this handy little tool from Redgate. Its a free download and has to be one of the handiest Sql tools I have come across especially when working ]]></description>
			<content:encoded><![CDATA[<p>Ever wanted to be able to search your sql databases for procedure names, table names etc as you would with a file search? Well now you can with this handy little tool from Redgate. Its a free download and has to be one of the handiest Sql tools I have come across especially when working with multiple large databases. </p>
<p><a href="http://www.red-gate.com/products/SQL_Search/index.htm?utm_source=sqlsearchui&#038;utm_medium=textad&#038;utm_content=recommend_sql_search&#038;utm_campaign=sqlsearch" target="_blank">Download SQL search</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.idevforfun.com/index.php/2010/03/30/sql-search/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Comma Delimited Lists</title>
		<link>http://www.idevforfun.com/index.php/2010/02/07/comma-delimited-lists/</link>
		<comments>http://www.idevforfun.com/index.php/2010/02/07/comma-delimited-lists/#comments</comments>
		<pubDate>Sun, 07 Feb 2010 18:10:28 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[DotNet]]></category>

		<guid isPermaLink="false">http://www.idevforfun.com/blog/?p=77</guid>
		<description><![CDATA[We are commonly required to handle comma delimited lists while developing applications. In the past, I have used for loops or Array.Split() to handle these situations. At least I did until I discovered a handy little class in the .NET framework that is there to handle just such situations. The CommaDelimitedStringCollection class in the System.Configuration ]]></description>
			<content:encoded><![CDATA[<p>We are commonly required to handle comma delimited lists while developing applications. In the past, I have used for loops or Array.Split() to handle these situations. At least I did until I discovered a handy little class in the .NET framework that is there to handle just such situations. The CommaDelimitedStringCollection class in the System.Configuration namespace behaves just like a normal generic list including methods for Add, Remove, IndexOf, Count, etc and can be looped through like any other list. This allows the flexibility of searching and sorting and also simplifies the handling of empty lists and removal of trailing commas when returning the comma delimited string. To convert to the comma delimited string all you have to do is call ToString() and you&#8217;re done. </p>
<p>More information can be found <a href="http://msdn.microsoft.com/en-us/library/system.configuration.commadelimitedstringcollection.aspx">here</a></p>

<div class="wp_codebox"><table><tr id="p772"><td class="code" id="p77code2"><pre class="csharp" style="font-family:monospace;">&nbsp;
<span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
<span style="color: #6666cc; font-weight: bold;">string</span> str1 <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;A String&quot;</span><span style="color: #008000;">;</span>
<span style="color: #6666cc; font-weight: bold;">string</span> str2 <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Another String&quot;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">//assume System.Configuration has been imported</span>
CommaDelimitedStringCollection strList <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> CommaDelimitedStringCollection<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
strList<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>i<span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
strList<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>str1<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
strList<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>str2<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Count = &quot;</span> <span style="color: #008000;">+</span> strList<span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">//output =&gt; Count = 3</span>
Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Index of '1' = &quot;</span> <span style="color: #008000;">+</span> strList<span style="color: #008000;">.</span><span style="color: #0000FF;">IndexOf</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;1&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">//output =&gt; Index of '1' = 0</span>
Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>strList<span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>  <span style="color: #008080; font-style: italic;">//output =&gt; 1,A String,Another String</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.idevforfun.com/index.php/2010/02/07/comma-delimited-lists/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Extension Methods</title>
		<link>http://www.idevforfun.com/index.php/2010/01/16/extension-methods/</link>
		<comments>http://www.idevforfun.com/index.php/2010/01/16/extension-methods/#comments</comments>
		<pubDate>Sat, 16 Jan 2010 21:55:53 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[DotNet]]></category>

		<guid isPermaLink="false">http://www.idevforfun.com/blog/?p=44</guid>
		<description><![CDATA[Extension methods are a handy feature that allows you to extend the functionality of existing classes for which you do not have the source. Extension methods must be static and the type following the &#8216;this&#8217; keyword indicates the type that is extended by this method.
More information on extension methods can be found here
These are just ]]></description>
			<content:encoded><![CDATA[<p>Extension methods are a handy feature that allows you to extend the functionality of existing classes for which you do not have the source. Extension methods must be static and the type following the &#8216;this&#8217; keyword indicates the type that is extended by this method.</p>
<p>More information on extension methods can be found <a href="http://msdn.microsoft.com/en-us/library/bb383977.aspx">here</a></p>
<p>These are just some of the extension methods I commonly use to make everyday coding a little easier and more readable.</p>

<div class="wp_codebox"><table><tr id="p444"><td class="code" id="p44code4"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Reformat a string so that the first letter of every word is capitalised and every other letter is lower case</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;str&quot;&gt;String to format&lt;/param&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;returns&gt;Formatted string&lt;/returns&gt;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">string</span> ToTitleCase<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> <span style="color: #6666cc; font-weight: bold;">string</span> str<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    CultureInfo ci <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> CultureInfo<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;en&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    TextInfo cc <span style="color: #008000;">=</span> ci<span style="color: #008000;">.</span><span style="color: #0000FF;">TextInfo</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> cc<span style="color: #008000;">.</span><span style="color: #0000FF;">ToTitleCase</span><span style="color: #008000;">&#40;</span>str<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Convert this string to its enum type</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;typeparam name=&quot;T&quot;&gt;Type of enum&lt;/typeparam&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;enumValue&quot;&gt;string value to parse&lt;/param&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;returns&gt;Enum type parsed from string&lt;/returns&gt;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> T ConvertToEnum<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> <span style="color: #6666cc; font-weight: bold;">string</span> enumValue<span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">where</span> T <span style="color: #008000;">:</span> <span style="color: #6666cc; font-weight: bold;">struct</span>
<span style="color: #008000;">&#123;</span>
     <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span><a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #008000;">typeof</span></a><span style="color: #008000;">&#40;</span>T<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IsEnum</span><span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">throw</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> InvalidEnumArgumentException<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;T must be an enum&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
     <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">&#40;</span>T<span style="color: #008000;">&#41;</span><span style="color: #6666cc; font-weight: bold;">Enum</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Parse</span><span style="color: #008000;">&#40;</span><a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #008000;">typeof</span></a><span style="color: #008000;">&#40;</span>T<span style="color: #008000;">&#41;</span>, enumValue, <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Get the value of a table column from a reader object</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;typeparam name=&quot;T&quot;&gt;Type  of expected return value&lt;/typeparam&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;reader&quot;&gt;Reader oject to get the value from&lt;/param&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;columnName&quot;&gt;Name of column to retrieve the value from&lt;/param&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;returns&gt;Column Value&lt;/returns&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;exception cref=&quot;NullReferenceException&quot;&gt;If T is not nullable and the column value is null&lt;/exception&gt;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> T GetColumnValue<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> IDataReader reader, <span style="color: #6666cc; font-weight: bold;">string</span> columnName<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
       <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>reader<span style="color: #008000;">.</span><span style="color: #0000FF;">IsDBNull</span><span style="color: #008000;">&#40;</span>reader<span style="color: #008000;">.</span><span style="color: #0000FF;">GetOrdinal</span><span style="color: #008000;">&#40;</span>columnName<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
       <span style="color: #008000;">&#123;</span>
           <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>
               <span style="color: #008000;">&#40;</span><a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #008000;">typeof</span></a><span style="color: #008000;">&#40;</span>T<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IsGenericType</span> <span style="color: #008000;">&amp;&amp;</span> <a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #008000;">typeof</span></a><span style="color: #008000;">&#40;</span>T<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetGenericTypeDefinition</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">==</span> <a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #008000;">typeof</span></a><span style="color: #008000;">&#40;</span>Nullable<span style="color: #008000;">&lt;&gt;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
               <span style="color: #008000;">||</span> <a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #008000;">typeof</span></a><span style="color: #008000;">&#40;</span>T<span style="color: #008000;">&#41;</span> <span style="color: #008000;">==</span> <a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #008000;">typeof</span></a><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#41;</span>
              <span style="color: #008000;">&#41;</span>
           <span style="color: #008000;">&#123;</span>
               <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">default</span><span style="color: #008000;">&#40;</span>T<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
           <span style="color: #008000;">&#125;</span>
           <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">throw</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> NullReferenceException<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;DB Value is null&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
       <span style="color: #008000;">&#125;</span>
       <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">&#40;</span>T<span style="color: #008000;">&#41;</span>reader<span style="color: #008000;">&#91;</span>columnName<span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
 <span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">//The above methods are called like this ...</span>
<span style="color: #6666cc; font-weight: bold;">string</span> myStr <span style="color: #008000;">=</span><span style="color: #666666;">&quot;exAmple sTrinG&quot;</span><span style="color: #008000;">;</span>
Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>myStr<span style="color: #008000;">.</span><span style="color: #0000FF;">ToTitleCase</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">//Output --&gt; Example String</span>
&nbsp;
<span style="color: #008080; font-style: italic;">//assume we have the namespace Microsoft.SqlServer.Management.Smo imported</span>
<span style="color: #6666cc; font-weight: bold;">string</span> varChar <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;VarChar&quot;</span><span style="color: #008000;">;</span>
SqlDataType testVrChr <span style="color: #008000;">=</span> varChar<span style="color: #008000;">.</span><span style="color: #0000FF;">ConvertToEnum</span><span style="color: #008000;">&lt;</span>SqlDataType<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">//where reader is an IDataReader</span>
<span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">?</span> value <span style="color: #008000;">=</span> reader<span style="color: #008000;">.</span><span style="color: #0000FF;">GetColumnValue</span><span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">?&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;ColName&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.idevforfun.com/index.php/2010/01/16/extension-methods/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Windows UI Threading</title>
		<link>http://www.idevforfun.com/index.php/2010/01/10/windows-ui-threading/</link>
		<comments>http://www.idevforfun.com/index.php/2010/01/10/windows-ui-threading/#comments</comments>
		<pubDate>Sun, 10 Jan 2010 22:07:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[DotNet]]></category>

		<guid isPermaLink="false">http://www.idevforfun.com/blog/?p=5</guid>
		<description><![CDATA[Using background threads to handle long running processes such as fetching data from the db should be common practice when developing windows forms. Failure to do so will result in a clunky UI that will lock up and may appear as crashed to the user. It also allows you to do nice things like updating ]]></description>
			<content:encoded><![CDATA[<p>Using background threads to handle long running processes such as fetching data from the db should be common practice when developing windows forms. Failure to do so will result in a clunky UI that will lock up and may appear as crashed to the user. It also allows you to do nice things like updating progress bars etc. </p>
<p>A common trap is trying to call a method which updates a UI control from a background thread. This will result in the &#8220;Cross Thread Call&#8221; exception which can frighten many developers away from threading in the IU. Here is a pattern I tend to follow when threading UI that takes a lot of the mystery out of things and keeps the UI running smoothly. </p>
<p>First we have a method that calls a long running process. In this method we simply create a new thread in which to run the process and get it started.</p>

<div class="wp_codebox"><table><tr id="p57"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code" id="p5code7"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Do some long running process on a background thread</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> Start<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
     lbl<span style="color: #008000;">.</span><span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Empty</span><span style="color: #008000;">;</span>
     Thread worker <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Thread<span style="color: #008000;">&#40;</span>DoSomething<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
     worker<span style="color: #008000;">.</span><span style="color: #0000FF;">Start</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Long running process </span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> DoSomething<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
     <span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;=</span> <span style="color: #FF0000;">100</span><span style="color: #008000;">;</span> i<span style="color: #008000;">+=</span><span style="color: #FF0000;">5</span><span style="color: #008000;">&#41;</span>
     <span style="color: #008000;">&#123;</span>
          UpdateProgressBar<span style="color: #008000;">&#40;</span>i<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
          Thread<span style="color: #008000;">.</span><span style="color: #0000FF;">Sleep</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">500</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>   
     <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>In each iteration of the loop we update the value of a progress bar. This however can only be achieved on the UI thread and this method was called on the background thread. Normally this would throw an exception as the invoke keyword is not used but because of the way the UpdateProgressBar method has been written this is not an issue. </p>
<p>The code below shows how to write any method that updates a UI control so that it can be called safely from any thread and will always execute on the UI thread no matter what the thread the caller is on.</p>

<div class="wp_codebox"><table><tr id="p58"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code" id="p5code8"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Something that updates a UI control that </span>
<span style="color: #008080; font-style: italic;">/// might be called from a background thread </span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;progress&quot;&gt;&lt;/param&gt;</span>
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> UpdateProgressBar<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> progress<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
     <span style="color: #008080; font-style: italic;">//Wrap any code that updates a UI control in this anonymous delegate</span>
     Func<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">int</span>, <span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">&gt;</span> work <span style="color: #008000;">=</span> <span style="color: #6666cc; font-weight: bold;">delegate</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> progressCount<span style="color: #008000;">&#41;</span>
     <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>progressCount <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span> progressCount <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>progressCount <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">100</span><span style="color: #008000;">&#41;</span> progressCount <span style="color: #008000;">=</span> <span style="color: #FF0000;">100</span><span style="color: #008000;">;</span>
        progressBar1<span style="color: #008000;">.</span><span style="color: #0000FF;">Value</span> <span style="color: #008000;">=</span> progressCount<span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>progressCount <span style="color: #008000;">==</span> <span style="color: #FF0000;">100</span><span style="color: #008000;">&#41;</span> lbl<span style="color: #008000;">.</span><span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Done!&quot;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
     <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
     <span style="color: #008080; font-style: italic;">//Check if we are running on a background thread and if so use Invoke</span>
     <span style="color: #008080; font-style: italic;">//otherwise just call the delegate</span>
     <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">InvokeRequired</span><span style="color: #008000;">&#41;</span> Invoke<span style="color: #008000;">&#40;</span>work, progress<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
     <span style="color: #0600FF; font-weight: bold;">else</span> work<span style="color: #008000;">&#40;</span>progress<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>The Func is an anonymous delegate … all logic that affects controls should be wrapped in this. The last two lines handle the differing threads. InvokeRequired determines if we are on the UI thread or the background thread. If we are on a background thread it will return true and we call Invoke on the anonymous method to ensure it is executed on the UI thread. If we are already on the UI thread the method is just called without Invoke.</p>
<p>By structuring the control update methods this way they can be called from any thread and will never throw an error due to cross thread calls. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.idevforfun.com/index.php/2010/01/10/windows-ui-threading/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
