<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Pradip&#039;s Blog</title>
	<atom:link href="http://spradip.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://spradip.wordpress.com</link>
	<description>C#, SharePoint, Design Pattern and more</description>
	<lastBuildDate>Thu, 22 Dec 2011 19:04:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='spradip.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Pradip&#039;s Blog</title>
		<link>http://spradip.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://spradip.wordpress.com/osd.xml" title="Pradip&#039;s Blog" />
	<atom:link rel='hub' href='http://spradip.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Dynamic Pages with ASP.NET</title>
		<link>http://spradip.wordpress.com/2011/05/29/dynamiasppage/</link>
		<comments>http://spradip.wordpress.com/2011/05/29/dynamiasppage/#comments</comments>
		<pubDate>Mon, 30 May 2011 02:53:48 +0000</pubDate>
		<dc:creator>spradip</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[ASP.NET]]></category>

		<guid isPermaLink="false">http://spradip.wordpress.com/?p=284</guid>
		<description><![CDATA[Download complete code from here. Introduction If you are familiar with the SharePoint technologies you might have known that you can edit the appearance of certain portion of the page. Moreover in publishing page you can add and delete as many parts in the page as you like.  Fig: Showing Edit enabled Page Now a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=spradip.wordpress.com&amp;blog=12527361&amp;post=284&amp;subd=spradip&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Download complete code from <a href="http://cid-ed15f52b6d6f6a21.office.live.com/self.aspx/.Public/MyWeb.zip">here</a>.</p>
<h2>Introduction</h2>
<p>If you are familiar with the SharePoint technologies you might have known that you can edit the appearance of certain portion of the page. Moreover in publishing page you can add and delete as many parts in the page as you like.</p>
<p align="center"><a href="http://spradip.files.wordpress.com/2011/05/sharepointpage.png"><img class="aligncenter size-full wp-image-286" title="SharePointPage" src="http://spradip.files.wordpress.com/2011/05/sharepointpage.png?w=600&#038;h=236" alt="" width="600" height="236" /></a> Fig: Showing Edit enabled Page</p>
<p>Now a day web page is not just a static page which just shows the static information to the user. It would be more flexible to give the user to customize the view they want so that they can move the parts around the page, hide the parts and modify the property of parts. Adding such functionality is expensive and requires lot of works of developer and of course more error prone.</p>
<p>In this article I will give you the general overview of using web part manager and web part zone.</p>
<h2>Introducing WebPartManager</h2>
<p>WebPartManager control is an ASP.NET server control introduced from .NET 2.0. There is not any visual effect of this control in the page. But wait. This control can add items to and delete items from each zone of the page. This means there is no meaning for a page to contain only WebPartManager. There should be at least one WebPartZone. WebPartManager is page dependent control and it manages on that particular page. However you can add it in you master page if you require.</p>
<p>After adding the WebPartManager control into the webpage, now it is a time to create a Zone in that page. You can any techniques to create several zones in your page. This is directly dependent on your business logic. For this article I am using Table to create zone. I am creating four zones.</p>
<p>Note that you can drag and drop the control from toolbox into your page or you can write a code.</p>
<p><pre class="brush: xml;">

&lt;asp:WebPartManager ID=&quot;WebPartManagerDefault&quot; runat=&quot;server&quot;&gt;

&lt;/asp:WebPartManager&gt;

&lt;table style=&quot;width: 100%;&quot;&gt;

&lt;tr&gt;

&lt;td colspan=&quot;4&quot;&gt;

&amp;nbsp;

&lt;asp:WebPartZone ID=&quot;WebPartZoneTop&quot; runat=&quot;server&quot;&gt;

&lt;ZoneTemplate&gt;

&lt;/ZoneTemplate&gt;

&lt;/asp:WebPartZone&gt;

&lt;/td&gt;

&lt;/tr&gt;

&lt;tr&gt;

&lt;td&gt;

&amp;nbsp;

&lt;asp:WebPartZone ID=&quot;WebPartZoneLeft&quot; runat=&quot;server&quot;&gt;

&lt;ZoneTemplate&gt;

&lt;/ZoneTemplate&gt;

&lt;/asp:WebPartZone&gt;

&lt;/td&gt;

&lt;td style=&quot;width: 50%;&quot;&gt;

&amp;nbsp;

&lt;asp:WebPartZone ID=&quot;WebPartZoneCenter&quot; runat=&quot;server&quot;&gt;

&lt;ZoneTemplate&gt;

&lt;/ZoneTemplate&gt;

&lt;/asp:WebPartZone&gt;

&lt;/td&gt;

&lt;td&gt;

&amp;nbsp;

&lt;asp:WebPartZone ID=&quot;WebPartZoneRight&quot; runat=&quot;server&quot;&gt;

&lt;/asp:WebPartZone&gt;

&lt;/td&gt;

&lt;td&gt;

&amp;nbsp;

&lt;/td&gt;

&lt;/tr&gt;

&lt;/table&gt;

</pre></p>
<p>Now you have added the WebPartZone in your page. Inside WebPartZone you can add almost anything that can be added in your web form. You can add HTML elements, User controls, and Web parts and so on.</p>
<p>Let’s add two asp controls; image control and calendar control in the page. Now, your code looks like this</p>
<p><pre class="brush: xml;">

&lt;asp:WebPartManager ID=&quot;WebPartManagerDefault&quot; runat=&quot;server&quot;&gt;

&lt;/asp:WebPartManager&gt;

&lt;table style=&quot;width: 100%;&quot;&gt;

&lt;tr&gt;

&lt;td colspan=&quot;4&quot;&gt;

&amp;nbsp;

&lt;asp:WebPartZone ID=&quot;WebPartZoneTop&quot; runat=&quot;server&quot;&gt;

&lt;ZoneTemplate&gt;

&lt;asp:Image ID=&quot;ImageBanner&quot; runat=&quot;server&quot; ImageUrl=&quot;~/Images/Banner.png&quot; Title=&quot;Image Control&quot; /&gt;

&lt;/ZoneTemplate&gt;

&lt;/asp:WebPartZone&gt;

&lt;/td&gt;

&lt;/tr&gt;

&lt;tr&gt;

&lt;td&gt;

&amp;nbsp;

&lt;asp:WebPartZone ID=&quot;WebPartZoneLeft&quot; runat=&quot;server&quot;&gt;

&lt;ZoneTemplate&gt;

&lt;asp:Calendar ID=&quot;CalendarDefault&quot; runat=&quot;server&quot; Title=&quot;Calendar Control&quot;&gt;&lt;/asp:Calendar&gt;

&lt;/ZoneTemplate&gt;

&lt;/asp:WebPartZone&gt;

&lt;/td&gt;

&lt;td style=&quot;width: 50%;&quot;&gt;

&amp;nbsp;

&lt;asp:WebPartZone ID=&quot;WebPartZoneCenter&quot; runat=&quot;server&quot;&gt;

&lt;ZoneTemplate&gt;

&lt;/ZoneTemplate&gt;

&lt;/asp:WebPartZone&gt;

&lt;/td&gt;

&lt;td&gt;

&amp;nbsp;

&lt;asp:WebPartZone ID=&quot;WebPartZoneRight&quot; runat=&quot;server&quot;&gt;

&lt;/asp:WebPartZone&gt;

&lt;/td&gt;

&lt;td&gt;

&amp;nbsp;

&lt;/td&gt;

&lt;/tr&gt;

&lt;/table&gt;

</pre></p>
<p>If you run the project now you will get the output like this</p>
<p style="text-align:center;"> <a href="http://spradip.files.wordpress.com/2011/05/wp_output1.png"><img class="aligncenter size-full wp-image-285" title="WP_Output1" src="http://spradip.files.wordpress.com/2011/05/wp_output1.png?w=600&#038;h=285" alt="" width="600" height="285" /></a></p>
<p style="text-align:center;">                Fig: output 1</p>
<p>You can see the title of each control. They are taking title from the ‘Title’ attribute of control. Even the control doesn’t contain this property by default you can add it so that you can have meaningful title displayed rather than default ‘untitled’. Note that you can customize the control. You can minimize it, restore it or you can close it. If you make some changes for example minimize the asp image control and reopen the page you can see your image control in minimize state. GREAT!!!</p>
<p>Now let try one more thing, let’s close the image control. Now, you can see it is nowhere in the page. Even if you reopen your page there will not be any image control.</p>
<p>Note that the customization information is saved in App_Data folder in ASPNETDB database.</p>
<h2>Introducing different mode of the page</h2>
<p>Now I will give you the description of different mode of the page.  User can add, move or change the page they are in by changing the page mode. For now let’s add the DropDownList in the page and make certain change in the code behind.</p>
<p><pre class="brush: xml;">

&lt;tr&gt;

&lt;td colspan=&quot;4&quot; align=&quot;right&quot;&gt;

&lt;asp:DropDownList ID=&quot;DropDownList1&quot; runat=&quot;server&quot; AutoPostBack=&quot;True&quot;

onselectedindexchanged=&quot;DropDownList1_SelectedIndexChanged&quot;&gt;

&lt;/asp:DropDownList&gt;

&amp;nbsp;&amp;nbsp;&amp;nbsp;

&lt;/td&gt;

&lt;/tr&gt;

</pre></p>
<p>Changes in code-behind.</p>
<p><pre class="brush: csharp;">

protected void Page_Init(object sender, EventArgs e)

{

foreach (WebPartDisplayMode wpMode in WebPartManagerDefault.SupportedDisplayModes)

{

string modeName = wpMode.Name;

ListItem listItem = new ListItem(modeName, modeName);

DropDownList1.Items.Add(listItem);

}

}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)

{

WebPartDisplayMode displayMode = WebPartManagerDefault.SupportedDisplayModes[DropDownList1.SelectedValue];

WebPartManagerDefault.DisplayMode = displayMode;

}

</pre></p>
<p>Open the page. You will see two drop down items. One is browse and one is design. If you change the mode to design mode you can move your web part from one web part zone to another.</p>
<h2>But still where is my closed web part?</h2>
<p>In this section I will show you to add two additional modes of page. One is catalog mode in which you can see your closed web part and you can add web part to any zone. Another is edit mode in which you can edit the property of your web part including display. If you have created your custom web part you can make the property editable when you are in edit mode. (I will show you how to do this on another article.)</p>
<p>For now let’s add the following piece of design code in your working page.</p>
<p><pre class="brush: xml;">

&lt;asp:CatalogZone ID=&quot;CatalogZoneMain&quot; runat=&quot;server&quot;&gt;

&lt;ZoneTemplate&gt;

&lt;asp:PageCatalogPart ID=&quot;PageCatalogPartMain&quot; runat=&quot;server&quot; /&gt;

&lt;asp:DeclarativeCatalogPart ID=&quot;DeclarativeCatalogPartMain&quot; runat=&quot;server&quot;&gt;

&lt;WebPartsTemplate&gt;

&lt;asp:TextBox ID=&quot;TextBox&quot; runat=&quot;server&quot; Title=&quot;Text Box&quot;&gt;&lt;/asp:TextBox&gt;

&lt;/WebPartsTemplate&gt;

&lt;/asp:DeclarativeCatalogPart&gt;

&lt;/ZoneTemplate&gt;

&lt;/asp:CatalogZone&gt;

&lt;asp:EditorZone ID=&quot;EditorZoneMain&quot; runat=&quot;server&quot;&gt;

&lt;ZoneTemplate&gt;

&lt;asp:AppearanceEditorPart ID=&quot;AppearanceEditorPartMain&quot; runat=&quot;server&quot; /&gt;

&lt;asp:BehaviorEditorPart ID=&quot;BehaviorEditorPartMain&quot; runat=&quot;server&quot; /&gt;

&lt;asp:LayoutEditorPart ID=&quot;LayouteEditorPartMain&quot; runat=&quot;server&quot; /&gt;

&lt;asp:PropertyGridEditorPart ID=&quot;PropertyGridEditorPartMain&quot; runat=&quot;server&quot; /&gt;

&lt;/ZoneTemplate&gt;

&lt;/asp:EditorZone&gt;

</pre></p>
<p>Now you can see two additional page modes in your drop down menu. If you select catalog mode you can see your closed web part. You can add this web part in your page. Moreover, if you click on Declarative Catalog link you will get another the list of web part (in this case only one, textbox control). You can add this web part to any web part zone.</p>
<p>If you select edit mode you can now edit the property of web part like Appearance and Behavior. Generally, normal user shouldn’t see this option. There are more than those properties for editing which is still invisible. To view those properties you need to change your web.config file. Add following configuration in System.web.</p>
<p><pre class="brush: xml;">

&lt;webParts&gt;

&lt;personalization&gt;

&lt;authorization&gt;

&lt;allow users=&quot;*&quot; verbs=&quot;enterSharedScope&quot; /&gt;

&lt;/authorization&gt;

&lt;/personalization&gt;

&lt;/webParts&gt;

</pre></p>
<p>And add following piece of code in Page_Load</p>
<p><pre class="brush: csharp;">

if (WebPartManagerDefault.Personalization.Scope == PersonalizationScope.User

&amp;&amp; WebPartManagerDefault.Personalization.CanEnterSharedScope)

{

WebPartManagerDefault.Personalization.ToggleScope();

}

</pre></p>
<p>When you change the mode to edit and chose edit for any web part or control, you should be able to see something like this.</p>
<p align="center"><a href="http://spradip.files.wordpress.com/2011/05/wp_editmode.png"><img class="aligncenter size-full wp-image-287" title="WP_EditMode" src="http://spradip.files.wordpress.com/2011/05/wp_editmode.png?w=600" alt=""   /></a></p>
<p align="center">Fig: Web part in edit mode.</p>
<h2>Modifying Zone property and Verbs in Web Part</h2>
<p>There are many properties those are used to assure certain behavior of web part zone. For example if you don’t want to change the web part of certain zone, then you can use the AllowLayoutChange property. You can use LayoutOrientation property to make sure you add web part in either horizontal or vertical orientation.</p>
<p>Verbs are property of web part which enable/disable the corresponding feature in the web part. The available verbs are CloseVerb, ConnectVerb, DeleteVerb, EditVerb, ExportVerb, HelpVerb, MinimizeVerb and RestoreVerb. They all have their own meaning. For example if you use following code in your page then you will not able to edit your web part. The corresponding menu will be disabled.</p>
<p><pre class="brush: xml;">&lt;/pre&gt;
&lt;asp:WebPartZone ID=&quot;WebPartZoneTop&quot; runat=&quot;server&quot;&gt;

&lt;EditVerb Enabled=&quot;false&quot; /&gt;

&lt;ZoneTemplate&gt;

&lt;asp:Image ID=&quot;ImageBanner&quot; runat=&quot;server&quot; ImageUrl=&quot;~/Images/Banner.png&quot; Title=&quot;Image Control&quot; /&gt;

&lt;/ZoneTemplate&gt;

&lt;/asp:WebPartZone&gt;
&lt;pre&gt;</pre></p>
<p align="center"><a href="http://spradip.files.wordpress.com/2011/05/wp_editmode_disable.png"><img class="size-full wp-image-288 alignnone" title="WP_EditMode_Disable" src="http://spradip.files.wordpress.com/2011/05/wp_editmode_disable.png?w=600" alt=""   /></a></p>
<p align="center">Fig: Disabled edit option</p>
<h2>Conclusion</h2>
<p>There are plenty of other stuffs that are part of web part. I will show you them on another articles step by step. Web part is very vast and powerful section in ASP.NET. Web part helps developers to make dynamic and modular pages. I will show you to create your own custom web part in another article.</p>
<br />Filed under: <a href='http://spradip.wordpress.com/category/net/'>.net</a>, <a href='http://spradip.wordpress.com/category/asp-net/'>ASP.NET</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/spradip.wordpress.com/284/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/spradip.wordpress.com/284/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/spradip.wordpress.com/284/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/spradip.wordpress.com/284/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/spradip.wordpress.com/284/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/spradip.wordpress.com/284/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/spradip.wordpress.com/284/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/spradip.wordpress.com/284/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/spradip.wordpress.com/284/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/spradip.wordpress.com/284/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/spradip.wordpress.com/284/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/spradip.wordpress.com/284/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/spradip.wordpress.com/284/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/spradip.wordpress.com/284/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=spradip.wordpress.com&amp;blog=12527361&amp;post=284&amp;subd=spradip&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://spradip.wordpress.com/2011/05/29/dynamiasppage/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/77ee2068edce2e168333e799492f2f15?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">spradip</media:title>
		</media:content>

		<media:content url="http://spradip.files.wordpress.com/2011/05/sharepointpage.png" medium="image">
			<media:title type="html">SharePointPage</media:title>
		</media:content>

		<media:content url="http://spradip.files.wordpress.com/2011/05/wp_output1.png" medium="image">
			<media:title type="html">WP_Output1</media:title>
		</media:content>

		<media:content url="http://spradip.files.wordpress.com/2011/05/wp_editmode.png" medium="image">
			<media:title type="html">WP_EditMode</media:title>
		</media:content>

		<media:content url="http://spradip.files.wordpress.com/2011/05/wp_editmode_disable.png" medium="image">
			<media:title type="html">WP_EditMode_Disable</media:title>
		</media:content>
	</item>
		<item>
		<title>Dining Philosopher Problem and AutoResetEvent.</title>
		<link>http://spradip.wordpress.com/2011/04/13/dining-philosopher-problem-and-autoresetevent/</link>
		<comments>http://spradip.wordpress.com/2011/04/13/dining-philosopher-problem-and-autoresetevent/#comments</comments>
		<pubDate>Thu, 14 Apr 2011 05:03:21 +0000</pubDate>
		<dc:creator>spradip</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[Programming C#]]></category>
		<category><![CDATA[Threading]]></category>

		<guid isPermaLink="false">http://spradip.wordpress.com/?p=271</guid>
		<description><![CDATA[Introduction Full code can be downloaded from here. I think we all know the famous classic problem of dining philosopher. In this problem, we consider n philosophers sitting in round table and n forks and all of them want to eat the spaghetti in front of each one. To eat spaghetti, philosopher must have two forks. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=spradip.wordpress.com&amp;blog=12527361&amp;post=271&amp;subd=spradip&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><strong>Introduction</strong></p>
<p><strong></strong>Full code can be downloaded from <a href="http://cid-ed15f52b6d6f6a21.office.live.com/self.aspx/.Public/DiningPhilosopherProblem.zip">here</a>.</p>
<p>I think we all know the famous classic problem of dining philosopher. In this problem, we consider n philosophers sitting in round table and n forks and all of them want to eat the spaghetti in front of each one. To eat spaghetti, philosopher must have two forks. The problem is philosopher 1 is taking one fork and waiting philosopher2 for second fork. Similarly, philosopher 2 is taking a fork and waiting philosopher3 for a fork. And philosopher n is taking a fork and waiting philosoper1 for a fork. So, what is actually happening is each philosopher cannot get the two forks at a time because each philosopher is taking a fork and waiting. So, here is the DEADLOCK.</p>
<p>For 5 philosophers we can assume the figure</p>
<p style="text-align:center;"><a href="http://spradip.files.wordpress.com/2011/04/ic338850.png"><img class="aligncenter size-full wp-image-272" title="IC338850" src="http://spradip.files.wordpress.com/2011/04/ic338850.png?w=600" alt=""   /></a><br />
Fig: Dining Philosopher Problem</p>
<p style="text-align:left;">Similar incident can be happens in our program and operating system where many processes are struggling to get the resources. There are certain criteria which must meet to cause the deadlock. They are:</p>
<p>1.	Each resource can be used by only one thread or process.<br />
2.	Thread/Process is holding a resource and waiting for other resource to continue.<br />
3.	Resources those are granted previously cannot be forcibly taken away.<br />
4.	There must be chain of threads/processes (more than two); each is waiting for the resource held by next<br />
thread/process.</p>
<p>To avoid deadlock, we can negate any of the above conditions. For dining philosopher problem, we will negate the second condition to avoid deadlock.</p>
<p><strong>Code</strong><br />
Full code can be downloaded from <a href="http://cid-ed15f52b6d6f6a21.office.live.com/self.aspx/.Public/DiningPhilosopherProblem.zip">here</a>.<br />
I have used <a href="http://msdn.microsoft.com/en-us/library/system.threading.autoresetevent.aspx">AutoResetEvent </a>to implement the dining philosopher problem.  I have used one AutoResetEvent as mutex and n others as philosopher events.<br />
There are three states of philosopher.</p>
<p><pre class="brush: csharp;">
    enum State
    {
        Thinking,
        Eating,
        Hungry
    }
</pre></p>
<p>Let’s look at the code:</p>
<p><pre class="brush: csharp;">
private void TakeForks(int i)
        {
            _mutex.WaitOne();
            _philosophersState[i] = State.Hungry;
            TryGetForks(i);
            _mutex.Set();
            _philosopherEvents[i].WaitOne();
        }

        private void TryGetForks(int i)
        {
            if (_philosophersState[i] == State.Hungry &amp;&amp;
                _philosophersState[right(i)] != State.Eating &amp;&amp;
                _philosophersState[left(i)] != State.Eating)
            {
                _philosophersState[i] = State.Eating;
                _philosopherEvents[i].Set();
            }
        }

        private void PutForks(int i)
        {
            _mutex.WaitOne();
            _philosophersState[i] = State.Thinking;
            TryGetForks(right(i));
            TryGetForks(left(i));
            _mutex.Set();
        }
</pre></p>
<p>First of all philosopher tries to take both fork. If it is not possible, it waits for the signal from its left and right. Whenever it gets the signal, philosopher again tries to acquire both forks and same process repeats.</p>
<p><pre class="brush: csharp;">
void checkForConflict(int i)
        {
            if (_philosophersState[left(i)] == State.Eating)
                Console.WriteLine(&quot;Conflict between &quot; + left(i) + &quot; and &quot; + i);
            if (_philosophersState[right(i)] == State.Eating)
                Console.WriteLine(&quot;Conflict between &quot; + i + &quot; and &quot; + right(i));
        }
</pre></p>
<p>Above code checks for the conflict and displays conflict message whenever two philosophers try to eat using same resource. Our program mustn’t print the conflict messages.</p>
<p><strong>Conclusion</strong><br />
Dining philosopher is a classic deadlock problem of operating system. Every deadlock can be avoided by negating any of the four essential conditions of deadlock.</p>
<p>I have used AutoResetEvent as mutex and for signalling purpose. However, we can use others also.</p>
<br />Filed under: <a href='http://spradip.wordpress.com/category/net/'>.net</a>, <a href='http://spradip.wordpress.com/category/programming-c/'>Programming C#</a>, <a href='http://spradip.wordpress.com/category/net/threading/'>Threading</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/spradip.wordpress.com/271/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/spradip.wordpress.com/271/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/spradip.wordpress.com/271/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/spradip.wordpress.com/271/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/spradip.wordpress.com/271/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/spradip.wordpress.com/271/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/spradip.wordpress.com/271/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/spradip.wordpress.com/271/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/spradip.wordpress.com/271/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/spradip.wordpress.com/271/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/spradip.wordpress.com/271/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/spradip.wordpress.com/271/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/spradip.wordpress.com/271/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/spradip.wordpress.com/271/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=spradip.wordpress.com&amp;blog=12527361&amp;post=271&amp;subd=spradip&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://spradip.wordpress.com/2011/04/13/dining-philosopher-problem-and-autoresetevent/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/77ee2068edce2e168333e799492f2f15?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">spradip</media:title>
		</media:content>

		<media:content url="http://spradip.files.wordpress.com/2011/04/ic338850.png" medium="image">
			<media:title type="html">IC338850</media:title>
		</media:content>
	</item>
		<item>
		<title>C# 4.0 dynamic Keyword</title>
		<link>http://spradip.wordpress.com/2011/02/07/c-4-0-dynamic-keyword/</link>
		<comments>http://spradip.wordpress.com/2011/02/07/c-4-0-dynamic-keyword/#comments</comments>
		<pubDate>Tue, 08 Feb 2011 05:37:40 +0000</pubDate>
		<dc:creator>spradip</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[Programming C#]]></category>

		<guid isPermaLink="false">http://spradip.wordpress.com/?p=260</guid>
		<description><![CDATA[Introduction Generally, C# is statically typed languages and checked type at the compile time. Languages like Ruby, Python are dynamic and determined the objects at the run time. Visual C# 2010 introduces a new keyword, dynamic which is a static type, but an object of type dynamic bypasses static type checking. So C# can use [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=spradip.wordpress.com&amp;blog=12527361&amp;post=260&amp;subd=spradip&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h2>Introduction</h2>
<div>Generally, C# is statically typed languages and checked type at the compile time. Languages like Ruby, Python are dynamic and determined the objects at the run time. Visual C# 2010 introduces a new keyword, dynamic which is a static type, but an object of type dynamic bypasses static type checking. So C# can use the features of dynamic language. It was introduced for C# 4.0 only and not for .net 4.0.</div>
<p></p>
<div></div>
<div>Whenever you use the dynamic keyword you tell the compiler to turn off the compile time checking. For example</div>
<p></p>
<div></div>
<div><a href="http://spradip.files.wordpress.com/2011/02/dynamicintellisense.png"><img class="aligncenter size-full wp-image-261" title="DynamicIntellisense" src="http://spradip.files.wordpress.com/2011/02/dynamicintellisense.png?w=600" alt="intellisense"   /></a></div>
<p></p>
<div>You can notice intellisense is saying it is dynamic expression and operation will be resolved at runtime.  Consider above example:</div>
<p>
<pre class="brush: csharp;">
dynamic dynamicString = &quot;Test&quot;;
Console.WriteLine(dynamicString.GetType());
dynamic dynamicInteger = 9;
Console.WriteLine(dynamicInteger.GetType());
</pre></p>
<div></div>
<p></p>
<div>Above line of code will give an output. System.String and System.Int32.</div>
<div></div>
<p></p>
<div>Another thing is you can call any function and any property. If it is not declared yet, your code will still compile but you will get runtime exception.</div>
<div></div>
<p>
<pre class="brush: csharp;">
dynamic dynamicString = &quot;Test&quot;;
dynamicString.Display();
dynamicString.Name = &quot;Pradip&quot;;
</pre><br />
</p>
<div>Note in above code actually dynamicString is String type but you can call method like Display() and property like Name which are not in String. Your code compile well but you will get run time exception.</div>
<div></div>
<p></p>
<h2>var Vs dynamic?</h2>
<div>You might be familier with the powerful var keyword from .net which is used for implicitly typed variables and for anonymous types. It is static and occurs at compile time. You can&#8217;t change the type of the variable at run time.</div>
<div></div>
<p>
<pre class="brush: csharp;">
var varString = &quot;Test&quot;;
varString.Display();
</pre></p>
<div></div>
<p></p>
<div>In above code, second line will result the compile time error.</div>
<p></p>
<div>Now lets look this piece of code.</div>
<p></p>
<div></div>
<p><pre class="brush: csharp;">
dynamic dynamicString = &quot;Test&quot;;
var varString = dynamicString;
varString.Display();
</pre><br />
</p>
<div></div>
<div>Now, varString is dynamic type and above code will compile. var is not a type, var keyword just instruct the compiler to infer the type from the variable&#8217;s initialization expression. So, we can use dynamic and var keyword together and they are mutually exclusive in nature.</div>
<div></div>
<p></p>
<h2>Reflection and dynamic</h2>
<div>Reflection is one way from which you can call invoke method (property and other also) dynamically. Compiler won&#8217;t throw any exception about even if there is no exception. But there will be complex long code in your program. For example to use String.Substring you need to write following code.</div>
<div></div>
<p>
<pre class="brush: csharp;">
String str = &quot;Pradip&quot;;
Type type = str.GetType();
 String subStr = (String)type.InvokeMember(&quot;Substring&quot;, System.Reflection.BindingFlags.InvokeMethod, null, str, new Object[] { 3 });
</pre><br />
</p>
<div></div>
<div>Now, using dynamic you can get same result with clean and neat code.</div>
<div></div>
<p>
<pre class="brush: csharp;">
dynamic dynamicString = &quot;Pradip&quot;;
String subString = dynamicString.Substring(3);
</pre><br />
</p>
<div></div>
<div>Of course, above example is not good enough to give explanation of using dynamic over reflection but you will find it better to use dynamic keyword in you application which uses reflection very much.</div>
<div></div>
<p></p>
<h2>Adding/Removing method and property at runtime</h2>
<div>Using dynamic keyword you can add/remove method or property from the object at the runtime. For this .net 4.0 provides ExpandoObject and DynamicObject classes defined in System.Dynamic namespace. Here is an example of adding property and method.</div>
<div></div>
<p>
<pre class="brush: csharp;">
dynamic myObject = new ExpandoObject();
myObject.Name = &quot;Pradip&quot;;
myObject.Display = new Action&lt;String&gt;(o =&gt; Console.WriteLine(myObject.Name));
myObject.Display(null);
</pre><br />
</p>
<div></div>
<h2>COM Interop and dynamic</h2>
<div>Many COM interop allow the variation in arguments type and has the return type as Object. So, there needs the casting to appropriate one to use it as the strongly typed variable. The magical dynamic keyword allows us to treat the object in COM signature as dynamic and hence prevents casting. As a result, our code will become more readable and clean.</div>
<div></div>
<p></p>
<div>Without dynamic we would write like this.</div>
<div></div>
<p>
<pre class="brush: csharp;">
((Excel.Range)excelApp.Cells[1, 2]).Value2 = &quot;Pradip&quot;;
Excel.Range range2008 = (Excel.Range)excelApp.Cells[1, 2];
</pre><br />
</p>
<div>And using dynamic we can write.</div>
<div></div>
<p>
<pre class="brush: csharp;">
excelApp.Cells[1, 2].Value = &quot;Pradip&quot;;
Excel.Range range2010 = excelApp.Cells[1, 2];
</pre><br />
</p>
<div></div>
<h2>Finally</h2>
<div>I hope, you got some knowledge about the NEW dynamic keyword in C# 4.0. The more you use it, the more you will be familier. For now we can say that dyanamic keyword has added the dynamic programming feature in statically typed language C#.</div>
<p>&nbsp;</p>
<br />Filed under: <a href='http://spradip.wordpress.com/category/net/'>.net</a>, <a href='http://spradip.wordpress.com/category/programming-c/'>Programming C#</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/spradip.wordpress.com/260/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/spradip.wordpress.com/260/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/spradip.wordpress.com/260/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/spradip.wordpress.com/260/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/spradip.wordpress.com/260/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/spradip.wordpress.com/260/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/spradip.wordpress.com/260/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/spradip.wordpress.com/260/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/spradip.wordpress.com/260/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/spradip.wordpress.com/260/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/spradip.wordpress.com/260/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/spradip.wordpress.com/260/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/spradip.wordpress.com/260/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/spradip.wordpress.com/260/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=spradip.wordpress.com&amp;blog=12527361&amp;post=260&amp;subd=spradip&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://spradip.wordpress.com/2011/02/07/c-4-0-dynamic-keyword/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/77ee2068edce2e168333e799492f2f15?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">spradip</media:title>
		</media:content>

		<media:content url="http://spradip.files.wordpress.com/2011/02/dynamicintellisense.png" medium="image">
			<media:title type="html">DynamicIntellisense</media:title>
		</media:content>
	</item>
	</channel>
</rss>
