<?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>Shawson&#039;s Code Blog &#187; Synchonous</title>
	<atom:link href="http://codeblog.shawson.co.uk/tag/synchonous/feed/" rel="self" type="application/rss+xml" />
	<link>http://codeblog.shawson.co.uk</link>
	<description>development notes for my failing memory</description>
	<lastBuildDate>Tue, 15 May 2012 15:22:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Synchonous Yield messing up my foreach loop!</title>
		<link>http://codeblog.shawson.co.uk/asynchonous-yield-messing-up-my-foreach-loop/</link>
		<comments>http://codeblog.shawson.co.uk/asynchonous-yield-messing-up-my-foreach-loop/#comments</comments>
		<pubDate>Tue, 08 Sep 2009 13:10:41 +0000</pubDate>
		<dc:creator>shawson</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[C#.net]]></category>
		<category><![CDATA[Synchonous]]></category>
		<category><![CDATA[timeout]]></category>
		<category><![CDATA[yield]]></category>

		<guid isPermaLink="false">http://www.shawson.co.uk/codeblog/?p=348</guid>
		<description><![CDATA[I recently got stung while debugging a colleagues code, by my lack of understanding of the Yield keyword! We had a foreach loop which looks like this; foreach (PurchaseOrderLine aLine in PurchaseOrderLine.LoadExtractLines(extractTime, systemId)) { //do some processing stuff... PurchaseOrder.MarkAsExported(lastId, extractTime); } The LoadExtractLines returns an IEnumerable and uses yield to return each line- the stored [...]


Related posts:<ul><li><a href='http://codeblog.shawson.co.uk/entity-framework-the-data-reader-is-incompatible-with-the-specified-complex-type/' rel='bookmark' title='Entity Framework &#8220;The data reader is incompatible with the specified complex type.&#8221;'>Entity Framework &#8220;The data reader is incompatible with the specified complex type.&#8221;</a></li>
<li><a href='http://codeblog.shawson.co.uk/axcms-net-v9-release-any-good/' rel='bookmark' title='AxCMS.net v9 Release&#8211; any good?'>AxCMS.net v9 Release&#8211; any good?</a></li>
<li><a href='http://codeblog.shawson.co.uk/2-tabs-fresh/' rel='bookmark' title='2 tabs fresh!'>2 tabs fresh!</a></li>
</ul>]]></description>
			<content:encoded><![CDATA[<p>I recently got stung while debugging a colleagues code, by my lack of understanding of the Yield keyword!  We had a foreach loop which looks like this;</p>
<pre class="brush: csharp">
foreach (PurchaseOrderLine aLine in PurchaseOrderLine.LoadExtractLines(extractTime, systemId)) {
  //do some processing stuff...
  PurchaseOrder.MarkAsExported(lastId, extractTime);
}
</pre>
<p>The LoadExtractLines returns an IEnumerable and uses yield to return each line- the stored proc it runs is quite intensive- the whole thing looks like this;</p>
<pre class="brush: csharp">
public static IEnumerable&lt;PurchaseOrderLine&gt; LoadExtractLines(DateTime cutOff,int systemId)
        {
            using (SqlConnection conn = new SqlConnection(SingleAccess.Instance.ConnectionToUse))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand(&quot;Get_PurchaseOrderLinesToExtract&quot;, conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue(&quot;@CutOff&quot;, cutOff.ToString(&quot;dd MMM yyyy HH:mm:ss&quot;));
                    cmd.Parameters.AddWithValue(&quot;@SystemId&quot;, systemId);
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            yield return LoadLine(reader);
                        }
                    }
                }
            }
        }
</pre>
<p>MarkAsExported runs quite an intensive update procedure on the database.  As more and more data came into the system we started to see Sql Timeouts, and upon running a trace noticed something strange.  Logging the RPC:Start and Completed events of the stored procs, the Get_PurchaseOrderLinesToExtract proc which feed&#8217;s the for loop was starting, then the update was starting before the Get_ had finished- the two were running side by side, causing the timeout&#8217;s!  </p>
<p>Turns out the foreach loop started the moment it received it&#8217;s first row, yielded back from the LoadExtractLine method- which in retrospect does make sense!  The solution was to convert the Load method to populate a local List<> then return the whole thing once complete, removing the yield statement alltoghether and forcing the process to wait for the entire result set to be complete before starting the loop.</p>


<p>Related posts:<ul><li><a href='http://codeblog.shawson.co.uk/entity-framework-the-data-reader-is-incompatible-with-the-specified-complex-type/' rel='bookmark' title='Entity Framework &#8220;The data reader is incompatible with the specified complex type.&#8221;'>Entity Framework &#8220;The data reader is incompatible with the specified complex type.&#8221;</a></li>
<li><a href='http://codeblog.shawson.co.uk/axcms-net-v9-release-any-good/' rel='bookmark' title='AxCMS.net v9 Release&#8211; any good?'>AxCMS.net v9 Release&#8211; any good?</a></li>
<li><a href='http://codeblog.shawson.co.uk/2-tabs-fresh/' rel='bookmark' title='2 tabs fresh!'>2 tabs fresh!</a></li>
</ul></p>]]></content:encoded>
			<wfw:commentRss>http://codeblog.shawson.co.uk/asynchonous-yield-messing-up-my-foreach-loop/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

