<?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>Coding Friends &#187; C Sharp (c#)</title>
	<atom:link href="http://www.codingfriends.com/index.php/category/programming/c_sharp/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codingfriends.com</link>
	<description>Coding Friends, place for developers.</description>
	<lastBuildDate>Sun, 04 Dec 2011 21:11:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Moonlight</title>
		<link>http://www.codingfriends.com/index.php/2011/03/08/moonlight/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=moonlight</link>
		<comments>http://www.codingfriends.com/index.php/2011/03/08/moonlight/#comments</comments>
		<pubDate>Tue, 08 Mar 2011 22:53:15 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[Silverlight / moonlight]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=1566</guid>
		<description><![CDATA[Within kubuntu (or ubuntu derived setups) you can have a moonlight development .. aka the silverlight open source version that will run on linux. if you install the monodevelop-moonlight and also the monodoc-moonlight-manual packages, the last one is the manual if you need some help, but the first one will allow the monodevelop development environment [...]]]></description>
			<content:encoded><![CDATA[<p>Within kubuntu (or ubuntu derived setups) you can have a moonlight development .. aka the silverlight open source version that will run on linux.</p>
<p>if you install the</p>

<div class="wp_syntax"><div class="code"><pre class="script" style="font-family:monospace;">monodevelop-moonlight</pre></div></div>

<p>and also the</p>

<div class="wp_syntax"><div class="code"><pre class="script" style="font-family:monospace;">monodoc-moonlight-manual</pre></div></div>

<p>packages, the last one is the manual if you need some help, but the first one will allow the monodevelop development environment have the moonlight plugin which creates a project to develop silverlight applications in.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2011/03/08/moonlight/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>threads &#8211; singleton</title>
		<link>http://www.codingfriends.com/index.php/2010/08/17/threads-singleton/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=threads-singleton</link>
		<comments>http://www.codingfriends.com/index.php/2010/08/17/threads-singleton/#comments</comments>
		<pubDate>Tue, 17 Aug 2010 08:31:57 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[C Sharp (c#)]]></category>
		<category><![CDATA[singleton]]></category>
		<category><![CDATA[Thread]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=1164</guid>
		<description><![CDATA[As from one of my previous posts about threading in csharp (c#), well with using Singletons you can use similar data between two different threads running. In basics a singleton is when you want to have a single class instance so that you can share this class so that every time that you use it [...]]]></description>
			<content:encoded><![CDATA[<p>As from one of my previous posts about <a href="http://www.codingfriends.com/index.php/2010/05/07/threading-running-different-parts-of-the-program-at-once/">threading in csharp</a> (c#), well with using <a href="http://en.wikipedia.org/wiki/Singleton_pattern">Singletons</a> you can use similar data between two different threads running.</p>
<p>In basics a singleton is when you want to have a single class instance so that you can share this class so that every time that you use it (even across threads) will only access the same class, it is very useful for when you have a printer spool so that you do not want to have x amount of printer spools (spool is when you have a list of print tasks waiting to print) and thus you only want to have one instance of a printer spool !!. </p>
<p>I have used the <a href="http://msdn.microsoft.com/en-us/library/ff650316.aspx">singleton creation from Microsoft</a> website, that creates a singleton class that is thread safe which means that I am using the lock method that will lock on a object to stop thread contention and thus only creates a new instance of a Singleton class so that each thread will only get access to a single instance of that class.</p>
<p>So when you want to gain access to that single instance, you just call the</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">Singleton theSingleton <span style="color: #008000;">=</span> Singleton<span style="color: #008000;">.</span><span style="color: #0000FF;">Instance</span><span style="color: #008000;">;</span></pre></div></div>

<p>So here is the full source code, and below is the output where the output is displaying the value is incrementing otherwise if is was not a singleton class, the main class would print out 0-4 and also the runthismethod would output 0-9 instead!.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Threading</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">namespace</span> monotestproject
<span style="color: #008000;">&#123;</span>
	<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">sealed</span> <span style="color: #6666cc; font-weight: bold;">class</span> Singleton
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">int</span> _value<span style="color: #008000;">;</span>
		<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> volatile Singleton instance<span style="color: #008000;">;</span>
		<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">object</span> syncRoot <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #6666cc; font-weight: bold;">Object</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #0600FF; font-weight: bold;">public</span> Singleton<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> _value <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span><span style="color: #008000;">&#125;</span>
&nbsp;
		<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> Singleton Instance
		<span style="color: #008000;">&#123;</span>
			get <span style="color: #008000;">&#123;</span> 
				<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>instance <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
				<span style="color: #008000;">&#123;</span>
					<span style="color: #0600FF; font-weight: bold;">lock</span><span style="color: #008000;">&#40;</span>syncRoot<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>instance <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
							instance <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Singleton<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
					<span style="color: #008000;">&#125;</span>
				<span style="color: #008000;">&#125;</span>
				<span style="color: #0600FF; font-weight: bold;">return</span> instance<span style="color: #008000;">;</span>
			<span style="color: #008000;">&#125;</span>
			<span style="color: #0600FF; font-weight: bold;">private</span> set <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span>
		<span style="color: #008000;">&#125;</span>
&nbsp;
		<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">int</span> theValue 
		<span style="color: #008000;">&#123;</span>
			get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> _value<span style="color: #008000;">;</span><span style="color: #008000;">&#125;</span>
			set <span style="color: #008000;">&#123;</span> _value <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span><span style="color: #008000;">&#125;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #6666cc; font-weight: bold;">class</span> MainClass
	<span style="color: #008000;">&#123;</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;">void</span> Main <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> args<span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			Singleton theSingleton <span style="color: #008000;">=</span> Singleton<span style="color: #008000;">.</span><span style="color: #0000FF;">Instance</span><span style="color: #008000;">;</span>
			<span style="color: #008080; font-style: italic;">// initialize the RunThisMethod as a thread</span>
			Thread theThread <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Thread<span style="color: #008000;">&#40;</span>RunThisMethod<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			theThread<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>
&nbsp;
			<span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> j <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> j <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">5</span><span style="color: #008000;">;</span> j<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span>
			<span style="color: #008000;">&#123;</span>
				theSingleton<span style="color: #008000;">.</span><span style="color: #0000FF;">theValue</span><span style="color: #008000;">++;</span>
				Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Main Singleton value &quot;</span> <span style="color: #008000;">+</span> theSingleton<span style="color: #008000;">.</span><span style="color: #0000FF;">theValue</span><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;">100</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			<span style="color: #008000;">&#125;</span>
		<span style="color: #008000;">&#125;</span>
&nbsp;
		<span style="color: #008080; font-style: italic;">// the method to create as a threadable method</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;">void</span> RunThisMethod<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			Singleton runsSingleton <span style="color: #008000;">=</span> Singleton<span style="color: #008000;">.</span><span style="color: #0000FF;">Instance</span><span style="color: #008000;">;</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;">10</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span>
			<span style="color: #008000;">&#123;</span>
				runsSingleton<span style="color: #008000;">.</span><span style="color: #0000FF;">theValue</span><span style="color: #008000;">++;</span>
				Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;RunThisMethod Singleton value &quot;</span> <span style="color: #008000;">+</span> runsSingleton<span style="color: #008000;">.</span><span style="color: #0000FF;">theValue</span><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;">45</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			<span style="color: #008000;">&#125;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>here is my output, as you can I am getting the singleton value incrementing, which is what should be happening.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">Main Singleton value <span style="color: #FF0000;">1</span>
RunThisMethod Singleton value <span style="color: #FF0000;">2</span>
RunThisMethod Singleton value <span style="color: #FF0000;">3</span>
RunThisMethod Singleton value <span style="color: #FF0000;">4</span>
Main Singleton value <span style="color: #FF0000;">5</span>
RunThisMethod Singleton value <span style="color: #FF0000;">6</span>
RunThisMethod Singleton value <span style="color: #FF0000;">7</span>
Main Singleton value <span style="color: #FF0000;">8</span>
RunThisMethod Singleton value <span style="color: #FF0000;">9</span>
RunThisMethod Singleton value <span style="color: #FF0000;">10</span>
Main Singleton value <span style="color: #FF0000;">11</span>
RunThisMethod Singleton value <span style="color: #FF0000;">12</span>
RunThisMethod Singleton value <span style="color: #FF0000;">13</span>
Main Singleton value <span style="color: #FF0000;">14</span>
RunThisMethod Singleton value <span style="color: #FF0000;">15</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2010/08/17/threads-singleton/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>unsafe &#8211; pointers in the code</title>
		<link>http://www.codingfriends.com/index.php/2010/08/13/unsafe-pointers-in-the-code/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=unsafe-pointers-in-the-code</link>
		<comments>http://www.codingfriends.com/index.php/2010/08/13/unsafe-pointers-in-the-code/#comments</comments>
		<pubDate>Fri, 13 Aug 2010 10:16:15 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[C Sharp (c#)]]></category>
		<category><![CDATA[unsafe]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=1161</guid>
		<description><![CDATA[Sometimes you do miss that pointer part of c++ coding within c#, well you can get around the restriction with using the unsafe syntax which basically means that you are doing some coding that please do not use the restrictions of the virtual machine and just allow direct access to the variable/memory as such. So [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes you do miss that pointer part of c++ coding within c#, well you can get around the restriction with using the <a href="http://msdn.microsoft.com/en-us/library/chfa2zb8(VS.71).aspx">unsafe</a> syntax which basically means that you are doing some coding that please do not use the restrictions of the virtual machine and just allow direct access to the variable/memory as such.</p>
<p>So the basics is the</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// for a block code</span>
<span style="color: #0600FF; font-weight: bold;">unsafe</span> <span style="color: #008000;">&#123;</span>
code here<span style="color: #008000;">..</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #008080; font-style: italic;">// or for the whole method.</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">unsafe</span> <span style="color: #008000;">&lt;</span>returntype<span style="color: #008000;">&gt;</span> <span style="color: #008000;">&lt;</span>methodname<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&#123;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>So to show the code in real code, this will setup a int(eger) value to 50 and then point to the data via a pointer (*) and then you can see that the actual value within the pointer (*) is not the same and also will change every time since the int(eger) value of 50 memory location will most probably be different every time.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">namespace</span> unsafetest
<span style="color: #008000;">&#123;</span>
    <span style="color: #6666cc; font-weight: bold;">class</span> Program
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Main<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> args<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">unsafe</span>
            <span style="color: #008000;">&#123;</span>
                <span style="color: #6666cc; font-weight: bold;">int</span> intValue <span style="color: #008000;">=</span> <span style="color: #FF0000;">50</span><span style="color: #008000;">;</span>
                Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;integer value is : &quot;</span> <span style="color: #008000;">+</span> intValue<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">*</span> pointerInt <span style="color: #008000;">=</span> <span style="color: #008000;">&amp;</span>intValue<span style="color: #008000;">;</span>
                Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Pointer to the data is : &quot;</span> <span style="color: #008000;">+</span> <span style="color: #008000;">*</span>pointerInt<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Pointed to the memory value of data is : &quot;</span> <span style="color: #008000;">+</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&#41;</span>pointerInt<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
            Console<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadLine</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>the output would be similar to, I am using a (int) cast on the memory location, which since the memory location is very big then it would be better to use something like a hex value output, but this just shows that the value will be different on different runs, if you run it again and again, that will be different values.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">integer value <span style="color: #008000;">is</span> <span style="color: #008000;">:</span> <span style="color: #FF0000;">50</span>
Pointer to the data <span style="color: #008000;">is</span> <span style="color: #008000;">:</span> <span style="color: #FF0000;">50</span>
Pointed to the memory value of data <span style="color: #008000;">is</span> <span style="color: #008000;">:</span> <span style="color: #FF0000;">98691076</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2010/08/13/unsafe-pointers-in-the-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>delegates &#8211; settings function on the fly</title>
		<link>http://www.codingfriends.com/index.php/2010/06/02/delegates-settings-function-on-the-fly/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=delegates-settings-function-on-the-fly</link>
		<comments>http://www.codingfriends.com/index.php/2010/06/02/delegates-settings-function-on-the-fly/#comments</comments>
		<pubDate>Wed, 02 Jun 2010 09:29:49 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[C Sharp (c#)]]></category>
		<category><![CDATA[delegates]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=1042</guid>
		<description><![CDATA[Delegates allow a virtual method/function (static method) to be called via a link (which is the delegated variable). I have done a post before about delegates but kinder think and also have been asked how do you link one to a class and also change the delegated function due to a user input, so here [...]]]></description>
			<content:encoded><![CDATA[<p>Delegates allow a virtual method/function (static method) to be called via a link (which is the delegated variable).  I have done a post before about <a href="http://www.codingfriends.com/index.php/2010/01/18/delegates-csharp/">delegates</a> but kinder think and also have been asked how do you link one to a class and also change the delegated function due to a user input, so here goes.</p>
<p>I have started with setting up what type of method that I want to be associated with the delegated variable, this will be a mathematical function of either addition or subtraction with using integer values, so we need to return a integer value and also pass in two integer parameters, so a new delegated virtual method would be</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">delegate</span> <span style="color: #6666cc; font-weight: bold;">int</span> mathsOp<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> value1, <span style="color: #6666cc; font-weight: bold;">int</span> value2<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>and here would be a example of a method that the delegate is able to link to, because it takes 2 integer parameters and returns a integer value</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">int</span> add<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> value1, <span style="color: #6666cc; font-weight: bold;">int</span> value2<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0600FF; font-weight: bold;">return</span> value1 <span style="color: #008000;">+</span> value2<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>so we now have the delegate declaration and also a method to be able to point to, so we now need to setup the variables, one for the actual functions (MathClass that is holding the subtraction and addition methods) and also the delegated variable theMathOp that is a delegated type.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">MathClass mathFunc <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MathClass<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
mathsOp theMathOp<span style="color: #008000;">;</span></pre></div></div>

<p>to actually set the method up on the fly, you just need to tell it where you want the delegated type to point to</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">theMathOp <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> mathsOp<span style="color: #008000;">&#40;</span>mathFunc<span style="color: #008000;">.</span><span style="color: #0000FF;">add</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>and all is needed to call the delegated type variable, well you just call it like any other method</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">theMathOp<span style="color: #008000;">&#40;</span>inputValue1, inputValue2<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>that is about it, here is the code in full that will take in some values from the user and also the user is able to choose between addition and subtraction methods</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">namespace</span> newDelegates
<span style="color: #008000;">&#123;</span>
	<span style="color: #008080; font-style: italic;">// a maths holding delegated function</span>
	<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">delegate</span> <span style="color: #6666cc; font-weight: bold;">int</span> mathsOp<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> value1, <span style="color: #6666cc; font-weight: bold;">int</span> value2<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #6666cc; font-weight: bold;">class</span> MathClass
	<span style="color: #008000;">&#123;</span>
		<span style="color: #008080; font-style: italic;">// the functions to call within the class</span>
		<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">int</span> add<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> value1, <span style="color: #6666cc; font-weight: bold;">int</span> value2<span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			<span style="color: #0600FF; font-weight: bold;">return</span> value1 <span style="color: #008000;">+</span> value2<span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
&nbsp;
		<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">int</span> sub<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> value1, <span style="color: #6666cc; font-weight: bold;">int</span> value2<span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			<span style="color: #0600FF; font-weight: bold;">return</span> value1 <span style="color: #008000;">-</span> value2<span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #6666cc; font-weight: bold;">class</span> MainClass
	<span style="color: #008000;">&#123;</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;">void</span> Main <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> args<span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
&nbsp;
			<span style="color: #008080; font-style: italic;">// setup the maths class which has the functions inside to call</span>
			MathClass mathFunc <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MathClass<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
			mathsOp theMathOp<span style="color: #008000;">;</span>
&nbsp;
			<span style="color: #008080; font-style: italic;">// there is no error checking in the inputs!!</span>
&nbsp;
			Console<span style="color: #008000;">.</span><span style="color: #0000FF;">Write</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Please enter value 1 : &quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			<span style="color: #6666cc; font-weight: bold;">int</span> inputValue1 <span style="color: #008000;">=</span> Convert<span style="color: #008000;">.</span><span style="color: #0000FF;">ToInt16</span><span style="color: #008000;">&#40;</span>Console<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadLine</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			Console<span style="color: #008000;">.</span><span style="color: #0000FF;">Write</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Please enter value 2 : &quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			<span style="color: #6666cc; font-weight: bold;">int</span> inputValue2 <span style="color: #008000;">=</span> Convert<span style="color: #008000;">.</span><span style="color: #0000FF;">ToInt16</span><span style="color: #008000;">&#40;</span>Console<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadLine</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><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;Please enter maths function :&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;1 : add&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;2 : sub&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			<span style="color: #6666cc; font-weight: bold;">int</span> mathsInputFun <span style="color: #008000;">=</span> Convert<span style="color: #008000;">.</span><span style="color: #0000FF;">ToInt16</span><span style="color: #008000;">&#40;</span>Console<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadLine</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;">// setup the virtual function to which ever the user wants</span>
			<span style="color: #0600FF; font-weight: bold;">switch</span> <span style="color: #008000;">&#40;</span>mathsInputFun<span style="color: #008000;">&#41;</span>
			<span style="color: #008000;">&#123;</span>
				<span style="color: #0600FF; font-weight: bold;">case</span> <span style="color: #FF0000;">1</span> <span style="color: #008000;">:</span> 
					theMathOp <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> mathsOp<span style="color: #008000;">&#40;</span>mathFunc<span style="color: #008000;">.</span><span style="color: #0000FF;">add</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> 
					<span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span>
				<span style="color: #0600FF; font-weight: bold;">case</span> <span style="color: #FF0000;">2</span> <span style="color: #008000;">:</span> 
					theMathOp <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> mathsOp<span style="color: #008000;">&#40;</span>mathFunc<span style="color: #008000;">.</span><span style="color: #0000FF;">sub</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> 
					<span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span>
				<span style="color: #0600FF; font-weight: bold;">default</span> <span style="color: #008000;">:</span>
					Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Settings to add&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
					theMathOp <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> mathsOp<span style="color: #008000;">&#40;</span>mathFunc<span style="color: #008000;">.</span><span style="color: #0000FF;">add</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> 
					<span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span>
			<span style="color: #008000;">&#125;</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;Value 1= &quot;</span> <span style="color: #008000;">+</span> inputValue1 <span style="color: #008000;">+</span> <span style="color: #008000;">&#40;</span>mathsInputFun <span style="color: #008000;">==</span> <span style="color: #FF0000;">1</span> <span style="color: #008000;">?</span> <span style="color: #666666;">&quot; + &quot;</span> <span style="color: #008000;">:</span> <span style="color: #666666;">&quot; - &quot;</span><span style="color: #008000;">&#41;</span> 
			                  <span style="color: #008000;">+</span> <span style="color: #666666;">&quot; value 2 = &quot;</span> <span style="color: #008000;">+</span> inputValue2 <span style="color: #008000;">+</span> <span style="color: #666666;">&quot; = &quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
			<span style="color: #008080; font-style: italic;">// here we call the virtual function that was setup</span>
			Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>theMathOp<span style="color: #008000;">&#40;</span>inputValue1, inputValue2<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>and below is the output of two runs of the program, one using addition and the other using subtraction</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">Please enter value <span style="color: #000000;">1</span> : <span style="color: #000000;">3</span>
Please enter value <span style="color: #000000;">2</span> : <span style="color: #000000;">2</span>
Please enter maths <span style="color: #000000; font-weight: bold;">function</span> :
<span style="color: #000000;">1</span> : add
<span style="color: #000000;">2</span> : sub
<span style="color: #000000;">1</span>
Value <span style="color: #000000;">1</span>= <span style="color: #000000;">3</span> +  value <span style="color: #000000;">2</span> = <span style="color: #000000;">2</span> = <span style="color: #000000;">5</span>
&nbsp;
second run through
&nbsp;
Please enter value <span style="color: #000000;">1</span> : <span style="color: #000000;">5</span>
Please enter value <span style="color: #000000;">2</span> : <span style="color: #000000;">2</span>
Please enter maths <span style="color: #000000; font-weight: bold;">function</span> :
<span style="color: #000000;">1</span> : add
<span style="color: #000000;">2</span> : sub
<span style="color: #000000;">2</span>
Value <span style="color: #000000;">1</span>= <span style="color: #000000;">5</span> -  value <span style="color: #000000;">2</span> = <span style="color: #000000;">2</span> = <span style="color: #000000;">3</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2010/06/02/delegates-settings-function-on-the-fly/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C# &#8211; Console Parameters test</title>
		<link>http://www.codingfriends.com/index.php/2010/05/21/c-console-parameters-test/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=c-console-parameters-test</link>
		<comments>http://www.codingfriends.com/index.php/2010/05/21/c-console-parameters-test/#comments</comments>
		<pubDate>Fri, 21 May 2010 09:08:10 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[C Sharp (c#)]]></category>
		<category><![CDATA[console parameters]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=1016</guid>
		<description><![CDATA[As from my previous post on c++ console parameters, I thought that I would do one with c# (c sharp) as well, just to show the difference in the languages as well. Compared to c++ where you cannot use the &#8220;==&#8221; (is equal to) operation because in c++ that is comparing two different memory locations [...]]]></description>
			<content:encoded><![CDATA[<p>As from my previous post on <a href="http://www.codingfriends.com/index.php/2010/05/20/console-parameters/">c++ console parameters</a>, I thought that I would do one with c# (c sharp) as well, just to show the difference in the languages as well.</p>
<p>Compared to c++ where you cannot use the &#8220;==&#8221; (is equal to) operation because in c++ that is comparing two different memory locations and not the value within the left/right hand variables.  Well in c# you can, there is a Equals method as well that you can use, but either or works fine, so in the example code below here is the main part, where I am comparing against a console parameter equalling -h (normally the help)</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>args<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Equals</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;-h&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;-h option selected(equals)&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>args<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">==</span><span style="color: #666666;">&quot;-h&quot;</span><span style="color: #008000;">&#41;</span> Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;-h option selected (==)&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>both of them are fine to use as you can see from the output at the bottom of the post, both will print out that &#8220;-h option selected&#8221;, best to use the Equals though.</p>
<p>Here is the full source code</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">namespace</span> consoletest
<span style="color: #008000;">&#123;</span>
	<span style="color: #6666cc; font-weight: bold;">class</span> MainClass
	<span style="color: #008000;">&#123;</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;">void</span> Main <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> args<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> args<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span>
			<span style="color: #008000;">&#123;</span>
				Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;args &quot;</span> <span style="color: #008000;">+</span> i <span style="color: #008000;">+</span> <span style="color: #666666;">&quot; : &quot;</span> <span style="color: #008000;">+</span> args<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>								
				<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>args<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Equals</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;-h&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;-h option selected(equals)&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
				<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>args<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">==</span><span style="color: #666666;">&quot;-h&quot;</span><span style="color: #008000;">&#41;</span> Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;-h option selected (==)&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			<span style="color: #008000;">&#125;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>and here is the output using mono as the .Net runtime environment, as you can see both -h has been outputed</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">mono consoletest.exe <span style="color: #660033;">-h</span> here
args <span style="color: #000000;">0</span> : <span style="color: #660033;">-h</span>
<span style="color: #660033;">-h</span> option selected<span style="color: #7a0874; font-weight: bold;">&#40;</span>equals<span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #660033;">-h</span> option selected <span style="color: #7a0874; font-weight: bold;">&#40;</span>==<span style="color: #7a0874; font-weight: bold;">&#41;</span>
args <span style="color: #000000;">1</span> : here</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2010/05/21/c-console-parameters-test/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Threading &#8211; running different parts of the program at &#8220;once&#8221;</title>
		<link>http://www.codingfriends.com/index.php/2010/05/07/threading-running-different-parts-of-the-program-at-once/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=threading-running-different-parts-of-the-program-at-once</link>
		<comments>http://www.codingfriends.com/index.php/2010/05/07/threading-running-different-parts-of-the-program-at-once/#comments</comments>
		<pubDate>Fri, 07 May 2010 19:14:45 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[C Sharp (c#)]]></category>
		<category><![CDATA[start]]></category>
		<category><![CDATA[Thread]]></category>
		<category><![CDATA[Thread.sleep]]></category>
		<category><![CDATA[Thread.start]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=929</guid>
		<description><![CDATA[A thread is when you &#8220;spawn&#8221; off another process (a process is like another program running within itself). The operating system gives the impression that it is running allot of programs at the same time, but what is happening that allot of processes (programs) have access to the CPU for a limited amount of time, [...]]]></description>
			<content:encoded><![CDATA[<p>A thread is when you &#8220;spawn&#8221; off another process (a process is like another program running within itself). </p>
<p>The operating system gives the impression that it is running allot of programs at the same time, but what is happening that allot of processes (programs) have access to the CPU for a limited amount of time, e.g. 10 milliseconds, and then leave the CPU execution stage whilst another process will enter there CPU execution stage (this is the part where the program is actually doing something).</p>
<p>To start with lets first create a method that is run within new thread, here we are just going to output a message and sleep for a bit, loop this over for 10 times.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// the method to create as a threadable method</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;">void</span> RunThisMethod<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;">10</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;RunThisMethod number : &quot;</span><span style="color: #008000;">+</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;">+</span> <span style="color: #666666;">&quot; Sleep for 45&quot;</span><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;">45</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></div></div>

<p>Now lets create a thread, since a Thread has to be a <a href="http://www.codingfriends.com/index.php/2010/01/18/delegates-csharp/">delegated</a> method but with .net 2 and above the .net environment will pick the correct delegate for you, so this means that you can just pass in the method name to the Thread class and that is it</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">	Thread theThread <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Thread<span style="color: #008000;">&#40;</span>RunThisMethod<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>To start the new Thread, it is as easy as start</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">	theThread<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></pre></div></div>

<p>Here is the full source code, for demoing how the main and the runMyMethod flip between each other (e.g. each process has time in the processor(s) to run)</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Threading</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">namespace</span> myThread
<span style="color: #008000;">&#123;</span>
	<span style="color: #6666cc; font-weight: bold;">class</span> MainClass
	<span style="color: #008000;">&#123;</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;">void</span> Main <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> args<span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			<span style="color: #008080; font-style: italic;">// initialize the RunThisMethod as a thread</span>
			Thread theThread <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Thread<span style="color: #008000;">&#40;</span>RunThisMethod<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			theThread<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>
&nbsp;
			<span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> j <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> j <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">5</span><span style="color: #008000;">;</span> j<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span>
			<span style="color: #008000;">&#123;</span>
				Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Main method number : &quot;</span><span style="color: #008000;">+</span>j<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;">+</span><span style="color: #666666;">&quot; Sleep for 100&quot;</span><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;">100</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			<span style="color: #008000;">&#125;</span>
		<span style="color: #008000;">&#125;</span>
&nbsp;
		<span style="color: #008080; font-style: italic;">// the method to create as a threadable method</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;">void</span> RunThisMethod<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;">10</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span>
			<span style="color: #008000;">&#123;</span>
				Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;RunThisMethod number : &quot;</span><span style="color: #008000;">+</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;">+</span> <span style="color: #666666;">&quot; Sleep for 45&quot;</span><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;">45</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			<span style="color: #008000;">&#125;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>and here would be the output</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">Main method number : <span style="color: #000000;">0</span> Sleep <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #000000;">100</span>
RunThisMethod number : <span style="color: #000000;">0</span> Sleep <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #000000;">45</span>
RunThisMethod number : <span style="color: #000000;">1</span> Sleep <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #000000;">45</span>
RunThisMethod number : <span style="color: #000000;">2</span> Sleep <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #000000;">45</span>
Main method number : <span style="color: #000000;">1</span> Sleep <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #000000;">100</span>
RunThisMethod number : <span style="color: #000000;">3</span> Sleep <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #000000;">45</span>
RunThisMethod number : <span style="color: #000000;">4</span> Sleep <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #000000;">45</span>
Main method number : <span style="color: #000000;">2</span> Sleep <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #000000;">100</span>
RunThisMethod number : <span style="color: #000000;">5</span> Sleep <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #000000;">45</span>
RunThisMethod number : <span style="color: #000000;">6</span> Sleep <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #000000;">45</span>
Main method number : <span style="color: #000000;">3</span> Sleep <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #000000;">100</span>
RunThisMethod number : <span style="color: #000000;">7</span> Sleep <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #000000;">45</span>
RunThisMethod number : <span style="color: #000000;">8</span> Sleep <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #000000;">45</span>
Main method number : <span style="color: #000000;">4</span> Sleep <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #000000;">100</span>
RunThisMethod number : <span style="color: #000000;">9</span> Sleep <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #000000;">45</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2010/05/07/threading-running-different-parts-of-the-program-at-once/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>loading images on the fly</title>
		<link>http://www.codingfriends.com/index.php/2010/05/07/loading-images-on-the-fly/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=loading-images-on-the-fly</link>
		<comments>http://www.codingfriends.com/index.php/2010/05/07/loading-images-on-the-fly/#comments</comments>
		<pubDate>Fri, 07 May 2010 13:30:25 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[C Sharp (c#)]]></category>
		<category><![CDATA[Silverlight / moonlight]]></category>
		<category><![CDATA[BitmapImage]]></category>
		<category><![CDATA[loading images]]></category>
		<category><![CDATA[Uri]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=913</guid>
		<description><![CDATA[To load a image from the server, local or remote depending if you are developing locally. I have been using monodevelop with the moonlight addin, this allows you to create silverlight applications with Linux . To start with, if you create a new solution by going to File->New->Solution and then selecting the moonlight Application project [...]]]></description>
			<content:encoded><![CDATA[<p><span id="zipfile"><a href='http://www.codingfriends.com/wp-content/uploads/2010/05/moonlighttest.zip'></a></span>To load a image from the server, local or remote depending if you are developing locally.  I have been using <a href="http://monodevelop.com/">monodevelop</a> with the <a href="http://monodevelop.com/Download/What%27s_new_in_MonoDevelop_2.2#Moonlight_Add-in">moonlight</a> addin, this allows you to create silverlight applications with Linux <img src='http://www.codingfriends.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<p>To start with, if you create a new solution by going to File->New->Solution and then selecting the moonlight Application project as similar to creating any other new project within monodeveloper (or within visual studio if you are using visual studio to create this silver light loading images on the fly).</p>
<p><a href="http://www.codingfriends.com/wp-content/uploads/2010/05/moonlight-solution.png"><img src="http://www.codingfriends.com/wp-content/uploads/2010/05/moonlight-solution.png" alt="Moonlight mono-develop project setup" title="moonlight-solution" class="aligncenter size-full wp-image-915" /></a></p>
<p>And then within your main page.xaml file you need to define where you want the image to be placed.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Image</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;Image1&quot;</span> <span style="color: #000066;">Source</span>=<span style="color: #ff0000;">&quot;dot.jpeg&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/Image<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Button</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;Button1&quot;</span> <span style="color: #000066;">Click</span>=<span style="color: #ff0000;">&quot;ButtonClick&quot;</span> <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;50&quot;</span> <span style="color: #000066;">Content</span>=<span style="color: #ff0000;">&quot;Click&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/Button<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>above also includes the Button that you are going to click onto to load the image, in this case it will call the ButtonClick function (from the above it is the &#8220;click&#8221; method).  The ButtonClick is behind the page.xaml within the page.xaml.cs file (the csharp file).</p>
<p>So once that button has been clicked it will call this function</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">		<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> ButtonClick<span style="color: #008000;">&#40;</span><span style="color: #000000;">System</span><span style="color: #008000;">.</span><span style="color: #6666cc; font-weight: bold;">Object</span> sender,<span style="color: #000000;">System</span><span style="color: #008000;">.</span><span style="color: #0000FF;">EventArgs</span> eventy<span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			<span style="color: #000000;">System</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Windows</span><span style="color: #008000;">.</span><span style="color: #0000FF;">MessageBox</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Show</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;hi there&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			Image1<span style="color: #008000;">.</span><span style="color: #0000FF;">Source</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> BitmapImage<span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> Uri<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;lcd.png&quot;</span>, UriKind<span style="color: #008000;">.</span><span style="color: #0000FF;">Relative</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span></pre></div></div>

<p>I put a little message pop up window to say &#8220;hi there&#8221;, just so that you know it have been clicked encase the lcd.png (as taken from the open clip art website <a href="http://www.openclipart.org/detail/23901">here</a>) is not in the correct place, in this case it is placed within the bin/debug, but if you are using a release edition or using visual studio you may have somewhere else.  </p>
<p>I have included the full source code and image within a zip file above, please feel free to download.</p>
<p>Here is the full page.xaml if you want to look over it.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;UserControl</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;</span> </span>
<span style="color: #009900;">             <span style="color: #000066;">xmlns:x</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;</span> </span>
<span style="color: #009900;">             <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;500&quot;</span> <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;350&quot;</span></span>
<span style="color: #009900;">             <span style="color: #000066;">x:Class</span>=<span style="color: #ff0000;">&quot;moonlighttest.Page&quot;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Grid</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;LayoutRoot&quot;</span> <span style="color: #000066;">Background</span>=<span style="color: #ff0000;">&quot;Black&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Image</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;Image1&quot;</span> <span style="color: #000066;">Source</span>=<span style="color: #ff0000;">&quot;dot.jpeg&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/Image<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Button</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;Button1&quot;</span> <span style="color: #000066;">Click</span>=<span style="color: #ff0000;">&quot;ButtonClick&quot;</span> <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;50&quot;</span> <span style="color: #000066;">Content</span>=<span style="color: #ff0000;">&quot;Click&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/Button<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Grid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/UserControl<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>and here is the full page.xaml.cs file.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Collections.Generic</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Windows</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Windows.Controls</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Windows.Media</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Windows.Media.Imaging</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">namespace</span> moonlighttest
<span style="color: #008000;">&#123;</span>
&nbsp;
	<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">partial</span> <span style="color: #6666cc; font-weight: bold;">class</span> Page <span style="color: #008000;">:</span> UserControl
	<span style="color: #008000;">&#123;</span>
&nbsp;
		<span style="color: #0600FF; font-weight: bold;">public</span> Page <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;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">InitializeComponent</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: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> ButtonClick<span style="color: #008000;">&#40;</span><span style="color: #000000;">System</span><span style="color: #008000;">.</span><span style="color: #6666cc; font-weight: bold;">Object</span> sender,<span style="color: #000000;">System</span><span style="color: #008000;">.</span><span style="color: #0000FF;">EventArgs</span> eventy<span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			<span style="color: #000000;">System</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Windows</span><span style="color: #008000;">.</span><span style="color: #0000FF;">MessageBox</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Show</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;hi there&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
			Image1<span style="color: #008000;">.</span><span style="color: #0000FF;">Source</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> BitmapImage<span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> Uri<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;lcd.png&quot;</span>, UriKind<span style="color: #008000;">.</span><span style="color: #0000FF;">Relative</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2010/05/07/loading-images-on-the-fly/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

