<?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>Thinking outside the box</title>
	<atom:link href="http://gauravsmathur.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://gauravsmathur.wordpress.com</link>
	<description>Just another WordPress.com site</description>
	<lastBuildDate>Tue, 09 Nov 2010 19:02:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='gauravsmathur.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Thinking outside the box</title>
		<link>http://gauravsmathur.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://gauravsmathur.wordpress.com/osd.xml" title="Thinking outside the box" />
	<atom:link rel='hub' href='http://gauravsmathur.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Something wrong with async, await and the Task(Task)</title>
		<link>http://gauravsmathur.wordpress.com/2010/11/04/something-wrong-with-async-await-and-the-tasktask/</link>
		<comments>http://gauravsmathur.wordpress.com/2010/11/04/something-wrong-with-async-await-and-the-tasktask/#comments</comments>
		<pubDate>Thu, 04 Nov 2010 11:57:08 +0000</pubDate>
		<dc:creator>gauravsmathur</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C# 5]]></category>

		<guid isPermaLink="false">http://gauravsmathur.wordpress.com/?p=33</guid>
		<description><![CDATA[Hi all   This is my first blog , so please excuse me (or point me) for any fault.   Whenever something new comes I usually analyze it in my own way figuring out its good and bad and whys. This is the first time I am thinking aloud via blogs.   Something wrong with [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gauravsmathur.wordpress.com&amp;blog=17373323&amp;post=33&amp;subd=gauravsmathur&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal" style="margin:0;"><span style="line-height:115%;font-size:12pt;"><span style="font-family:Calibri;">Hi all</span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="line-height:115%;font-size:12pt;"><span style="font-family:Calibri;"> </span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="line-height:115%;font-size:12pt;"><span style="font-family:Calibri;">This is my first blog <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> , so please excuse me (or point me) for any fault.</span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="line-height:115%;font-size:12pt;"><span style="font-family:Calibri;"> </span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="line-height:115%;font-size:12pt;"><span style="font-family:Calibri;">Whenever something new comes I usually analyze it in my own way figuring out its good and bad and whys. This is the first time I am thinking aloud via blogs.</span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-family:Calibri;"> </span></p>
<p class="MsoNormal" style="margin:0;"><strong><span style="line-height:115%;font-size:12pt;"><span style="font-family:Calibri;">Something wrong with async, await and the Task(Task&lt;T&gt;)</span></span></strong></p>
<p class="MsoNormal" style="margin:0;"><strong><span style="line-height:115%;font-size:12pt;"><span style="font-family:Calibri;"> </span></span></strong></p>
<p class="MsoNormal" style="margin:0;"><span style="line-height:115%;font-size:12pt;"><span style="font-family:Calibri;">There is something wrong with the method modifier (async) and operator (await), something is not in place</span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="line-height:115%;font-size:12pt;"><span style="font-family:Calibri;">Somehow I feel that async, await and Task triplet does not fit in the language rules and expectations</span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="line-height:115%;font-size:12pt;"><span style="font-family:Calibri;"> </span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="line-height:115%;font-size:12pt;"><span style="font-family:Calibri;">Lest analyze this in three parts –</span></span></p>
<p class="MsoListParagraphCxSpFirst" style="text-indent:-.25in;margin:0 0 0 .5in;"><span style="line-height:115%;font-size:12pt;"><span><span style="font-family:Calibri;">1.</span><span style="font:7pt 'Times New Roman';">      </span></span></span><span style="line-height:115%;font-size:12pt;"><span style="font-family:Calibri;">Analyze method invocation statement.</span></span></p>
<p class="MsoListParagraphCxSpMiddle" style="text-indent:-.25in;margin:0 0 0 .5in;"><span style="line-height:115%;font-size:12pt;"><span><span style="font-family:Calibri;">2.</span><span style="font:7pt 'Times New Roman';">      </span></span></span><span style="line-height:115%;font-size:12pt;"><span style="font-family:Calibri;">Analyze method declaration statement</span></span></p>
<p class="MsoListParagraphCxSpMiddle" style="text-indent:-.25in;margin:0 0 0 .5in;"><span><span><span style="font-family:Calibri;">3.</span><span style="font:7pt 'Times New Roman';">       </span></span></span><span style="font-family:Calibri;"><span style="line-height:115%;font-size:12pt;">Analyze method return statement</span>.</span></p>
<p class="MsoListParagraphCxSpLast" style="margin:0 0 0 .5in;"><span style="font-family:Calibri;"> </span></p>
<p class="MsoNormal" style="margin:0;"><span style="text-decoration:underline;"><span style="line-height:115%;font-size:12pt;"><span style="font-family:Calibri;">1. Analyze method invocation statement &#8211; </span></span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="text-decoration:underline;"><span style="line-height:115%;font-size:12pt;"><span style="text-decoration:none;"><span style="font-family:Calibri;"> </span></span></span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="line-height:115%;font-size:12pt;"><span style="font-family:Calibri;">Consider these two statements </span></span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-family:Consolas;color:#2b91af;">Task</span><span style="font-family:Consolas;">&lt;<span style="color:blue;">int</span>&gt; u =<span>       </span>ReturnIntAsync();</span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-family:Consolas;color:blue;">int</span><span style="font-family:Consolas;"><span>       </span>t = <span style="color:blue;">await</span> ReturnIntAsync();</span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-family:Calibri;"> </span></p>
<p class="MsoNormal" style="margin:0;"><span style="line-height:115%;font-family:Consolas;font-size:9.5pt;">Expected – LHS and RHS must be of same data type.</span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-family:Calibri;"> </span></p>
<p class="MsoNormal" style="margin:0;"><span style="line-height:115%;font-size:12pt;"><span style="font-family:Calibri;">first statement, no problem there, it clearly states that ReturnIntAsync() method must have Task&lt;int&gt; as return type and this is the case. Second statement, no problem, it looks like await must be pulling out the Result, which is int, from Task and returning it (in addition to its actual functionality, that is, returning back to the caller after asynchronously invoking the ReturnIntAsync() method).</span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="line-height:115%;font-family:Consolas;font-size:9.5pt;"> </span></p>
<p class="MsoNormal" style="margin:0;"><span style="line-height:115%;font-size:12pt;"><span style="font-family:Calibri;">So Actual &#8211; LHS and RHS are of same data type.</span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-family:Calibri;"> </span></p>
<p class="MsoNormal" style="margin:0;"><span style="text-decoration:underline;"><span style="line-height:115%;font-size:12pt;"><span style="font-family:Calibri;">2. Analyze method declaration statement – </span></span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="line-height:115%;font-size:12pt;"><span style="font-family:Calibri;"> </span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="line-height:115%;font-size:12pt;"><span style="font-family:Calibri;">The method declaration – </span></span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-family:Consolas;color:blue;">async</span><span style="font-family:Consolas;"> <span style="color:blue;">public</span> <span style="color:blue;">static</span> <span style="color:#2b91af;">Task</span>&lt;<span style="color:blue;">int</span>&gt; ReturnIntAsync()</span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-family:Calibri;"> </span></p>
<p class="MsoNormal" style="margin:0;"><span style="line-height:115%;font-size:12pt;"><span style="font-family:Calibri;">Expected – Must match with the caller in terms of return type and function prototype. </span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="line-height:115%;font-size:12pt;"><span style="font-family:Calibri;"> </span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="line-height:115%;font-size:12pt;"><span style="font-family:Calibri;">There are three method modifiers in this statement, static only one copy of this method will be created, public it’s a no restriction access modifier, async marks the method for asynchronous calling.</span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="line-height:115%;font-size:12pt;"><span style="font-family:Calibri;">None of the above method modifier changed return type or function prototype.</span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-family:Calibri;"> </span></p>
<p class="MsoNormal" style="margin:0;"><span style="line-height:115%;font-size:12pt;"><span style="font-family:Calibri;">Actual – Perfectly matching.</span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-family:Calibri;"> </span></p>
<p class="MsoNormal" style="margin:0;"><span style="text-decoration:underline;"><span style="line-height:115%;font-size:12pt;"><span style="font-family:Calibri;">3. Analyze method return statement – </span></span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-family:Calibri;"> </span></p>
<p class="MsoNormal" style="margin:0;"><span style="line-height:115%;font-size:12pt;"><span style="font-family:Calibri;">Expected – it must match with the of return statement of function definition</span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="line-height:115%;font-size:12pt;"><span style="font-family:Calibri;"> </span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="line-height:115%;font-size:12pt;"><span style="font-family:Calibri;">Now take a look at the method body – </span></span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-family:Consolas;color:blue;">async</span><span style="font-family:Consolas;"> <span style="color:blue;">public</span> <span style="color:blue;">static</span> <span style="color:#2b91af;">Task</span>&lt;<span style="color:blue;">int</span>&gt; ReturnIntAsync()</span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-family:Consolas;">{</span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-family:Consolas;"><span>    </span><span style="color:blue;">return</span> <span style="color:#2b91af;">0</span>;</span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-family:Consolas;">}</span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-family:Consolas;"> </span></p>
<p class="MsoNormal" style="margin:0;"><span style="line-height:115%;font-size:12pt;"><span style="font-family:Calibri;">No need to discuss anything its clearly not matching. By no means the value zero(0) can be thought of implicitly converted to Task&lt;int&gt;.</span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="line-height:115%;font-size:12pt;"><span style="font-family:Calibri;"> </span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="line-height:115%;font-size:12pt;"><span style="font-family:Calibri;">Actual – both are different.</span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="line-height:115%;font-size:12pt;"><span style="font-family:Calibri;"> </span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="line-height:115%;font-size:12pt;"><span style="font-family:Calibri;">So, async modifier is responsible of this mis-behavior.</span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="line-height:115%;font-size:12pt;"><span style="font-family:Calibri;"> </span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="text-decoration:underline;"><span style="line-height:115%;font-size:12pt;"><span style="font-family:Calibri;">Why</span></span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="line-height:115%;font-size:12pt;"><span style="font-family:Calibri;">So, before asking the question lets analyze the situation and try some alternatives.</span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="line-height:115%;font-size:12pt;"><span style="font-family:Calibri;"> </span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="line-height:115%;font-size:12pt;"><span style="font-family:Calibri;">First alternative that comes to my mind is to simply change the function definition to return int instead of Task&lt;int&gt; and let the compiler automatically takes care of int and Task&lt;int&gt;. But that’s not possible because at the callers end Task&lt;int&gt; is not getting converted to int by using the await keyword, it’s Task&lt;int&gt;.Result which is an int (see point 1 &#8211; analysis of caller). Also, it’s the method which is creating a task and returning it and not the caller who is creating the task out of int.</span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-family:Calibri;"> </span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-family:Calibri;">Second alternative, we can have a special kind of return statement like async return or task return, which will return the Task&lt;int&gt; instead of int. See below examples &#8211; </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-family:Consolas;color:blue;">async</span><span style="font-family:Consolas;"> <span style="color:blue;">public</span> <span style="color:blue;">static</span> <span style="color:#2b91af;">Task</span>&lt;<span style="color:blue;">int</span>&gt; ReturnIntAsync()</span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-family:Consolas;">{</span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-family:Consolas;"><span>    </span><span style="color:blue;">return</span> <span style="color:#2b91af;">0</span>; <span style="color:green;">//instead </span></span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-family:Consolas;"><span>    </span><span style="color:blue;">async return</span> <span style="color:#2b91af;">0</span>; <span style="color:green;">//use this</span></span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-family:Consolas;color:blue;"><span>    </span>task return</span><span style="font-family:Consolas;"> <span style="color:#2b91af;">0</span>; <span style="color:green;">//or this</span></span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-family:Consolas;color:blue;"><span>    </span>return</span><span style="font-family:Consolas;"> <span style="color:blue;">task </span><span style="color:#2b91af;">0</span>; <span style="color:green;">//or this</span></span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-family:Consolas;color:blue;"><span>    </span>return</span><span style="font-family:Consolas;"> <span style="color:blue;">async </span><span style="color:#2b91af;">0</span>; <span style="color:green;">//or this</span></span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-family:Consolas;">}</span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-family:Calibri;">Now this will make sense, as we are applying some operator to the returning value which can be assumed attaching the return value to the task. At first I thought that just for the sake of readability and making the statement complete should we introduce a new keyword. But then I thought why not if you recall as soon as you apply the async modifier the method is no more returning what it was returning previously instead it is attaching the value of return to the task.</span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-family:Calibri;"> </span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-family:Calibri;">So to conclude I strongly think that something must be done with the return statement. I am sure it must be a big pain to change something in the language but that’s my view with the potential problem and the solution <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</span></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gauravsmathur.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gauravsmathur.wordpress.com/33/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/gauravsmathur.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/gauravsmathur.wordpress.com/33/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/gauravsmathur.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/gauravsmathur.wordpress.com/33/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/gauravsmathur.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/gauravsmathur.wordpress.com/33/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/gauravsmathur.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/gauravsmathur.wordpress.com/33/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/gauravsmathur.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/gauravsmathur.wordpress.com/33/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/gauravsmathur.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/gauravsmathur.wordpress.com/33/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gauravsmathur.wordpress.com&amp;blog=17373323&amp;post=33&amp;subd=gauravsmathur&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://gauravsmathur.wordpress.com/2010/11/04/something-wrong-with-async-await-and-the-tasktask/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4a2c834c6e526918c12b51f581c9ded9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">gauravsmathur</media:title>
		</media:content>
	</item>
	</channel>
</rss>
