<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Alik Levin's : Development Phase</title><link>http://blogs.msdn.com/alikl/archive/tags/Development+Phase/default.aspx</link><description>Tags: Development Phase</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>DebugView - Free Simple Tool To Quickly Identify Performance Problems</title><link>http://blogs.msdn.com/alikl/archive/2008/11/11/debugview-free-simple-tool-to-quickly-identify-performance-problems.aspx</link><pubDate>Tue, 11 Nov 2008 18:12:53 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9060327</guid><dc:creator>alikl</dc:creator><slash:comments>4</slash:comments><comments>http://blogs.msdn.com/alikl/comments/9060327.aspx</comments><wfw:commentRss>http://blogs.msdn.com/alikl/commentrss.aspx?PostID=9060327</wfw:commentRss><wfw:comment>http://blogs.msdn.com/alikl/rsscomments.aspx?PostID=9060327</wfw:comment><description>&lt;table cellspacing="5" cellpadding="2" width="557" border="0"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top" width="226"&gt;&amp;#160;&lt;a href="http://practicethis.com/" target="_blank"&gt;&lt;img title="Alik Levin" height="50" alt="Alik Levin" src="http://blogs.microsoft.co.il/blogs/mcs/WindowsLiveWriter/d20b00ba5cce_FD44/image_5.png" width="50" border="0" /&gt;&lt;/a&gt;&amp;#160;&amp;#160; &lt;a href="http://www.microsoft.com/technet/sysinternals/Miscellaneous/DebugView.mspx"&gt;DebugView is well known free tool from Systinternals (Microsoft)&lt;/a&gt;. Vast majority of developers is aware of its existence. Nevertheless, I've decided to write this post following my practice of journaling my technical accomplishments during engagements with the customers. Yesterday the tool helped us identify serious performance problem without installing profiler or even taking memory dumps.&lt;/td&gt;        &lt;td valign="top" width="314"&gt;&lt;script type="text/javascript"&gt;
 
&lt;!-- 
lqm_channel=1;
lqm_publisher=253;
lqm_zone=1;
lqm_format=6;
//--&gt;&lt;/script&gt;&lt;script src="http://a.lakequincy.com/s.js" type="text/javascript"&gt;&lt;/script&gt;&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;h3&gt;Customer Case Study&lt;/h3&gt;  &lt;p&gt;The customer complained about &amp;quot;bad performance&amp;quot; of web pages. The application was pretty simple one - ASP.NET pages accessing SQL Server for simple queries. The SQL Server was installed on the same machine where ASP.NET app resided. The pages took up to ten seconds to render.&lt;/p&gt;  &lt;h3&gt;Analysis&lt;/h3&gt;  &lt;p&gt;After quick code review we found out that the code called function that traversed all server controls using recursion. This function was called several times during the page lifecycle. I presented the team with my assumption that this code is a potential culprit of the &amp;quot;bad performance&amp;quot;. The opposite assumption I faced was &amp;quot;but there is only so few controls on the page, it cannot consume too much time.&amp;quot;&lt;/p&gt;  &lt;p&gt;We decided to add single line of code to the function in question:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;em&gt;System.Diagnostics.Trace.WriteLine (&amp;quot;Calling Recursive Function&amp;quot;);&lt;/em&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Then we fired up DebugView and ran the page.&lt;/p&gt;  &lt;p&gt;The team was amazed by what DebugView presented interactively. &amp;quot;Calling Recursive Function&amp;quot; line kept piling up on and on. Each call took from 50 to 100 milliseconds but when multiplied to overall number of calls (~700) it gave us clear understanding what caused the performance hit.&lt;/p&gt;  &lt;h3&gt;Conclusion&lt;/h3&gt;  &lt;p&gt;Instrument your application properly - simple yet powerful technique that helps you avoid calling expensive consultants onsite. &lt;/p&gt;  &lt;p&gt;Instrument your application and surprise your end users by high performing application.&lt;/p&gt;  &lt;h3&gt;Related Materials&lt;/h3&gt;  &lt;ul&gt;   &lt;li&gt;&amp;#160;&lt;a href="http://blogs.msdn.com/alikl/archive/2007/07/16/use-sysinternals-debugview-to-diagnose-the-application.aspx"&gt;Use Sysinternals DebugView To Diagnose The Application&lt;/a&gt;. &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/alikl/archive/2008/11/03/asp-net-performance-by-design-takeaways-from-pdc.aspx"&gt;ASP.NET Performance By Design: Takeaways From PDC&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;This post is made with &lt;a href="http://practicethis.com/" target="_blank"&gt;PracticeThis.com&lt;/a&gt; plugin for Windows Live Writer&lt;/strong&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9060327" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/alikl/archive/tags/Test+Phase/default.aspx">Test Phase</category><category domain="http://blogs.msdn.com/alikl/archive/tags/Development+Phase/default.aspx">Development Phase</category><category domain="http://blogs.msdn.com/alikl/archive/tags/Performance/default.aspx">Performance</category></item><item><title>ASP.NET Performance: Fast AJAX, Faster AJAX</title><link>http://blogs.msdn.com/alikl/archive/2008/10/29/asp-net-performance-fast-ajax-faster-ajax.aspx</link><pubDate>Wed, 29 Oct 2008 00:43:52 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9021016</guid><dc:creator>alikl</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/alikl/comments/9021016.aspx</comments><wfw:commentRss>http://blogs.msdn.com/alikl/commentrss.aspx?PostID=9021016</wfw:commentRss><wfw:comment>http://blogs.msdn.com/alikl/rsscomments.aspx?PostID=9021016</wfw:comment><description>&lt;table cellspacing="5" cellpadding="2" width="557" border="0"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top" width="226"&gt;&amp;#160;&lt;a href="http://practicethis.com/" target="_blank"&gt;&lt;img title="Alik Levin" height="50" alt="Alik Levin" src="http://blogs.microsoft.co.il/blogs/mcs/WindowsLiveWriter/d20b00ba5cce_FD44/image_5.png" width="50" border="0" /&gt;&lt;/a&gt;&amp;#160;&amp;#160;&amp;#160; AJAX improves significantly both user experience and performance. It can be further improved by using down level capabilities that .Net framework offers. Specifically, consuming Web Services and WCF directly from client script. The best part is that ASP.NET AJAX comes with built in libraries - server and client - that make coding fun while significantly improving the web application's performance.&lt;/td&gt;        &lt;td valign="top" width="314"&gt;&lt;script type="text/javascript"&gt;

 
&lt;!-- 
lqm_channel=1;
lqm_publisher=253;
lqm_zone=1;
lqm_format=6;
//--&gt;&lt;/script&gt;&lt;script src="http://a.lakequincy.com/s.js" type="text/javascript"&gt;&lt;/script&gt;&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;h3&gt;Customer Case Study&lt;/h3&gt;  &lt;p&gt;The customer was using hand crafted XmlHttp requests from client scripts requesting the data from ASPX pages. While the goal was achieved - the amount of information sent to the server was minimal and the user interface was responsive - the coding was not really fun. Also, since the requests were sent to regular ASPX pages the whole ASPX pipeline was executing unnecessarily utilizing CPU for nothing. The customer did not want to use Update Panel control. Although it boosts coding productivity, it also adds some burden on the network. Network utilization should have been kept to the minimum. &lt;/p&gt;  &lt;h3&gt;Analysis&lt;/h3&gt;  &lt;p&gt;After quick research I found two great resources that directed me to the solution that would satisfy both requirements:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Coding productivity. &lt;/li&gt;    &lt;li&gt;Minimum of data in transit on the wire. &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;The first one is from Chris Hay&amp;#160; - &lt;a href="http://silverlightuk.blogspot.com/2008/10/remix08-uk-aspnet-front-end-performance.html"&gt;remix08 UK ASP.NET Front End Performance Slides&lt;/a&gt; and the other one is from Jeff Prosise - &lt;a href="http://download.microsoft.com/download/f/2/a/f2a04d97-4472-4d3d-bc5a-ecec0d073f24/03_Power_%20ASP_NET_AJAX_Programming.ppt"&gt;Power ASP.NET AJAX Programming&lt;/a&gt;. They both outline the usage of Script-Callable Web Services. There are three simple steps to follow:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;Declare your Web Service as Script-Callable by adding class level attribute:&lt;/strong&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;pre class="code"&gt;[System.Web.Script.Services.&lt;span style="color: #2b91af"&gt;ScriptService&lt;/span&gt;]
&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;AJAXCallableWebService &lt;/span&gt;: System.Web.Services.&lt;span style="color: #2b91af"&gt;WebService
&lt;/span&gt;{
    [&lt;span style="color: #2b91af"&gt;WebMethod&lt;/span&gt;]
    &lt;span style="color: blue"&gt;public string &lt;/span&gt;HelloWorld(&lt;span style="color: blue"&gt;string &lt;/span&gt;name)
    {
        &lt;span style="color: blue"&gt;return &lt;/span&gt;&lt;span style="color: #a31515"&gt;&amp;quot;Hello, &amp;quot; &lt;/span&gt;+ name;
    }
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Declare service reference inside the ScriptManager:&lt;/strong&gt; &lt;/li&gt;
&lt;/ul&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;asp&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;ScriptManager &lt;/span&gt;&lt;span style="color: red"&gt;ID&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;ScriptManager1&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;runat&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;server&amp;quot;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Services&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;asp&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;ServiceReference &lt;/span&gt;&lt;span style="color: red"&gt;InlineScript&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;true&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;Path&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;~/AJAXCallableWebService.asmx&amp;quot; /&amp;gt;
    &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;Services&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;asp&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;ScriptManager&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Call a Web Service from the client script:&lt;/strong&gt; &lt;/li&gt;
&lt;/ul&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;script &lt;/span&gt;&lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;text/javascript&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;language&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;javascript&amp;quot;&amp;gt;
function &lt;/span&gt;callAjax()
{
    &lt;span style="color: blue"&gt;var &lt;/span&gt;text = document.getElementById(&lt;span style="color: #a31515"&gt;&amp;quot;Text1&amp;quot;&lt;/span&gt;).value
    
    AJAXCallsWebService.AJAXCallableWebService.HelloWorld(text,onSuccess);
}
&lt;span style="color: blue"&gt;function &lt;/span&gt;onSuccess(result)
{
    document.getElementById(&lt;span style="color: #a31515"&gt;&amp;quot;result&amp;quot;&lt;/span&gt;).innerText = result;
}
&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;script&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;pre class="code"&gt;    &lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;span &lt;/span&gt;&lt;span style="color: red"&gt;id&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;result&amp;quot;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;span&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;input &lt;/span&gt;&lt;span style="color: red"&gt;id&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Button1&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;button&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;value&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;button&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;onclick&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;callAjax()&amp;quot; /&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;input &lt;/span&gt;&lt;span style="color: red"&gt;id&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Text1&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;text&amp;quot; /&amp;gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;h3&gt;Sample Visual Studio Solution&lt;/h3&gt;

&lt;p&gt;Grab the sample solution implemented using Visual Studio 2008 form my SkyDrive &lt;a href="http://cid-dd25b83e4ca261f7.skydrive.live.com/self.aspx/Visual%20Studio%20Projects/AJAXCallsWebService.zip"&gt;here&lt;/a&gt;:&lt;/p&gt;
&lt;iframe style="border-right: #dde5e9 1px solid; padding-right: 0px; border-top: #dde5e9 1px solid; padding-left: 0px; padding-bottom: 0px; margin: 3px; border-left: #dde5e9 1px solid; width: 240px; padding-top: 0px; border-bottom: #dde5e9 1px solid; height: 66px; background-color: #ffffff" marginwidth="0" marginheight="0" src="http://cid-dd25b83e4ca261f7.skydrive.live.com/embedrowdetail.aspx/Visual%20Studio%20Projects/AJAXCallsWebService.zip" frameborder="0" scrolling="no"&gt;&lt;/iframe&gt;

&lt;h3&gt;Related Materials&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href="http://blogs.msdn.com/alikl/archive/2008/02/18/how-to-consume-wcf-using-ajax-without-asp-net.aspx"&gt;How To Consume WCF Using AJAX Without ASP.NET&lt;/a&gt; &lt;/li&gt;

  &lt;li&gt;&lt;a href="http://blogs.msdn.com/alikl/archive/2007/12/31/asp-net-ajax-control-toolkit-basic-sample-for-dynamicpopulate-control.aspx"&gt;ASP.NET AJAX Control Toolkit - Basic Sample For DynamicPopulate Control&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;
&lt;a onclick="window.location = &amp;#39;http://www.reddit.com/submit?url=&amp;#39; + encodeURIComponent(window.location); return false" href="http://www.reddit.com/submit"&gt;&lt;img alt="submit to reddit" src="http://www.reddit.com/static/spreddit11.gif" border="0" /&gt; &lt;/a&gt;

&lt;p&gt;&lt;strong&gt;This template is made with &lt;a href="http://practicethis.com/" target="_blank"&gt;PracticeThis.com&lt;/a&gt; plugin for Windows Live Writer&lt;/strong&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9021016" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/alikl/archive/tags/Development+Phase/default.aspx">Development Phase</category><category domain="http://blogs.msdn.com/alikl/archive/tags/Implementation/default.aspx">Implementation</category><category domain="http://blogs.msdn.com/alikl/archive/tags/Performance/default.aspx">Performance</category><category domain="http://blogs.msdn.com/alikl/archive/tags/AJAX/default.aspx">AJAX</category></item><item><title>Improve ASP.NET 2.0 Performance With PageAsyncTask - Multithreading For The Masses</title><link>http://blogs.msdn.com/alikl/archive/2008/10/02/improve-asp-net-2-0-performance-with-pageasynctask-multithreading-for-the-masses.aspx</link><pubDate>Thu, 02 Oct 2008 11:31:38 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8973165</guid><dc:creator>alikl</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/alikl/comments/8973165.aspx</comments><wfw:commentRss>http://blogs.msdn.com/alikl/commentrss.aspx?PostID=8973165</wfw:commentRss><wfw:comment>http://blogs.msdn.com/alikl/rsscomments.aspx?PostID=8973165</wfw:comment><description>&lt;table cellspacing="5" cellpadding="2" width="557" border="0"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top" width="186"&gt;&amp;#160;&lt;a href="http://practicethis.com/" target="_blank"&gt;&lt;img title="Alik Levin" height="50" alt="Alik Levin" src="http://blogs.microsoft.co.il/blogs/mcs/WindowsLiveWriter/d20b00ba5cce_FD44/image_5.png" width="50" border="0" /&gt;&lt;/a&gt;&amp;#160;&amp;#160;&amp;#160; Multithreading is tough. This is what &lt;a href="http://www.wintellect.com/CS/blogs/jrobbins/default.aspx"&gt;John Robbins&lt;/a&gt; says about it in his excellent book &lt;a href="http://www.amazon.com/gp/redirect.html?ie=UTF8&amp;amp;location=http%3A%2F%2Fwww.amazon.com%2FDebugging-Microsoft-NET-2-0-Applications%2Fdp%2F0735622027%2F&amp;amp;tag=practhis-20&amp;amp;linkCode=ur2&amp;amp;camp=1789&amp;amp;creative=9325"&gt;Debugging Microsoft .NET 2.0 Applications&lt;/a&gt;&lt;img style="margin: 0px; border-top-style: none! important; border-right-style: none! important; border-left-style: none! important; border-bottom-style: none! important" height="1" alt="" src="http://www.assoc-amazon.com/e/ir?t=practhis-20&amp;amp;l=ur2&amp;amp;o=1" width="1" border="0" /&gt;:           &lt;br /&gt;          &lt;br /&gt;&amp;quot;Don't do it... Make sure there's no other way you can structure your program before you decide to incorporate multithreading into your application... you are easily adding a minimum of an extra month of development and testing to your schedule&amp;quot;. I agree with it completely. But there are times multithreading is unavoidable. Especially when more and more Services popping up in the wild...&lt;/td&gt;        &lt;td valign="top" width="354"&gt;&lt;img height="365" alt="" src="http://i.msdn.microsoft.com/cc163725.fig02(en-us).gif" width="354" /&gt;           &lt;br /&gt;          &lt;br /&gt;From &lt;a href="http://msdn.microsoft.com/en-us/magazine/cc163725.aspx"&gt;Asynchronous Pages in ASP.NET 2.0&lt;/a&gt;&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;h3&gt;Customer Case Study&lt;/h3&gt;  &lt;p&gt;I am working with the customer to improve their ASP.NET 2.0 application performance. The application issues multiple requests to the backend middleware that is exposed as a web service. The application cannot get a hold on the actual proxies rather it is provided with the components that wrap the web services proxies. The application needs to issue concurrent&amp;#160; requests. Serial request would result in serious latency which is unacceptable by end users.&lt;/p&gt;  &lt;h3&gt;Analysis&lt;/h3&gt;  &lt;p&gt;Since we cannot get hold on web services proxies we cannot utilize available asynchronous methods available with it: BeginMyMethod/EndMyMethod and MyMethodAsync/MyMethodCompleted. But we definitely can utilize another option available with ASP.NET 2.0 - registering asynchronous tasks using PageAsyncTask class.&lt;/p&gt;  &lt;p&gt;The following information is based on the following materials:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/magazine/cc163725.aspx"&gt;Asynchronous Pages in ASP.NET 2.0&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.web.ui.pageasynctask.aspx"&gt;PageAsyncTask Class&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;h3&gt;PageAsyncTask Implementation Steps&lt;/h3&gt;  &lt;p&gt;This is the summary of the steps to implement and register PageAsyncTask:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;Create a class that contains lengthy operation&lt;/strong&gt; &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Declare AsyncTaskDelegate&lt;/strong&gt; &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Add OnBegin, OnEnd, OnTimeout methods to the class&lt;/strong&gt; &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Mark the calling page as Async=&amp;quot;true&amp;quot;&lt;/strong&gt; &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Register the PageAsyncTasks in the ASPX page and execute it&lt;/strong&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;You can grab the Visual Studio 2008 project with the implementation from my SkyDrive &lt;a href="http://cid-dd25b83e4ca261f7.skydrive.live.com/self.aspx/Visual%20Studio%20Projects/MultipleCallsSynchronizedViaAsync.zip"&gt;here&lt;/a&gt;:&lt;/p&gt; &lt;iframe style="border-right: #dde5e9 1px solid; padding-right: 0px; border-top: #dde5e9 1px solid; padding-left: 0px; padding-bottom: 0px; margin: 3px; border-left: #dde5e9 1px solid; width: 240px; padding-top: 0px; border-bottom: #dde5e9 1px solid; height: 66px; background-color: #ffffff" marginwidth="0" marginheight="0" src="http://cid-dd25b83e4ca261f7.skydrive.live.com/embedrowdetail.aspx/Visual%20Studio%20Projects/MultipleCallsSynchronizedViaAsync.zip" frameborder="0" scrolling="no"&gt;&lt;/iframe&gt;  &lt;h3&gt;Related Materials&lt;/h3&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://blogs.msdn.com/alikl/archive/2008/06/23/improve-asp-net-performance-with-multithreading-using-thread-or-threadpool-objects.aspx"&gt;Improve ASP.NET Performance With Multithreading Using Thread Or ThreadPool Objects&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/ace_team/archive/2008/02/14/do-you-really-need-a-distributed-architecture.aspx"&gt;Do You Really Need A Distributed Architecture?&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;&lt;font size="1"&gt;This template is made with &lt;/font&gt;&lt;a href="http://practicethis.com/" target="_blank"&gt;&lt;font size="1"&gt;PracticeThis.com&lt;/font&gt;&lt;/a&gt;&lt;font size="1"&gt; plugin for Windows Live Writer&lt;/font&gt;&lt;/strong&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8973165" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/alikl/archive/tags/Development+Phase/default.aspx">Development Phase</category><category domain="http://blogs.msdn.com/alikl/archive/tags/Implementation/default.aspx">Implementation</category><category domain="http://blogs.msdn.com/alikl/archive/tags/Performance/default.aspx">Performance</category><category domain="http://blogs.msdn.com/alikl/archive/tags/Threading/default.aspx">Threading</category></item><item><title>Best ASP.NET Performance Winner For Data Binding - Hands Up To Response.Write()</title><link>http://blogs.msdn.com/alikl/archive/2008/07/31/best-asp-net-performance-winner-for-data-binding-hands-up-to-response-write.aspx</link><pubDate>Thu, 31 Jul 2008 15:29:58 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8793251</guid><dc:creator>alikl</dc:creator><slash:comments>21</slash:comments><comments>http://blogs.msdn.com/alikl/comments/8793251.aspx</comments><wfw:commentRss>http://blogs.msdn.com/alikl/commentrss.aspx?PostID=8793251</wfw:commentRss><wfw:comment>http://blogs.msdn.com/alikl/rsscomments.aspx?PostID=8793251</wfw:comment><description>&lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://practicethis.com/" target="_blank"&gt;&lt;img title="Alik Levin" height="50" alt="Alik Levin" src="http://blogs.microsoft.co.il/blogs/mcs/WindowsLiveWriter/d20b00ba5cce_FD44/image_5.png" width="50" border="0" /&gt;&lt;/a&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; To achieve best performance you need to make decisions based on trade-off between coolness, coding productivity, and personal engineering values. I never thought I would be recommending my customer considering using old fashion Response.Write() in his Internet facing ASP.NET web application in order to significantly improve the application’s performance. &lt;/p&gt;  &lt;h3&gt;Customer Case Study&lt;/h3&gt;  &lt;p&gt;During load/stress testing customer’s ASP.NET web application we identified high CPU utilization (up to 90%). After quick investigation we noticed that &lt;strong&gt;%Time in GC&lt;/strong&gt; performance counter is less than optimal. Our assumption was that the application uses memory allocation techniques that are less than optimal. From &lt;a href="http://blogs.msdn.com/maoni/archive/2004/06/03/148029.aspx" target="_blank"&gt;GC Performance Counters&lt;/a&gt;:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&amp;quot;First thing you may want to look at is &lt;b&gt;“% Time in GC”&lt;/b&gt;... What is a health value for this counter? It’s hard to say. It depends on what your app does. But if you are seeing a really high value (like 50% or more) then it’s a reasonable time to look at what’s going on inside of the managed heap.&amp;quot;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Another resource we used is timeless patterns &amp;amp; practices’ Chapter &lt;a href="http://msdn.microsoft.com/en-us/library/ms998579.aspx" target="_blank"&gt;15 — Measuring .NET Application Performance&lt;/a&gt;:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;.&lt;b&gt;NET CLR Memory\% Time in GC&lt;/b&gt;&lt;/p&gt;    &lt;p&gt;&amp;quot;…The most common cause of a high value is making too many allocations, which may be the case if you are allocating on a per-request basis for ASP.NET applications. You need to study the allocation profile for your application if this counter shows a higher value.&amp;quot; &lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;So we headed to looking into the code and this is what we found out.&lt;/p&gt;  &lt;h3&gt;Analysis&lt;/h3&gt;  &lt;p&gt;During &lt;a href="http://msdn.microsoft.com/en-us/library/ms998574.aspx" target="_blank"&gt;performance code inspection&lt;/a&gt; we identified massive usage of collections. The collections were used to transfer the data between the logical layers and then the collections were transferred into datatables to be bindable for DataGrid (yes, it is .Net 1.1 app). &lt;/p&gt;  &lt;p&gt;Eureka! We just spotted 3 performance anti-patterns. Massive memory allocation, massive loops, massive type conversions. I’ve shown it to 4 very respected professionals and everyone was saying the same – current situation is pure performance anti-pattern. Here are few suggestions that came up:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Bind collections directly to DataGrid eliminating additional memory allocations and loops. &lt;/li&gt;    &lt;li&gt;Create Datatable directly from XML skipping collection creation step eliminating additional memory allocations and loops. &lt;/li&gt;    &lt;li&gt;Use Xslt transformation transforming original Xml into Html table using Xslt elminating memory allocations and loops for both collections and datatables. &lt;/li&gt;    &lt;li&gt;Use Response.Write() as it’s suggested by &lt;a href="http://msdn.microsoft.com/en-us/library/ms998549.aspx" target="_blank"&gt;patterns &amp;amp; practices&lt;/a&gt;: &lt;/li&gt; &lt;/ul&gt;  &lt;blockquote&gt;   &lt;p&gt;&amp;quot;Use the &lt;b&gt;Response.Write&lt;/b&gt; method. It is one of the fastest ways to return output back to the browser.&amp;quot;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Case close? Not really...&lt;/p&gt;  &lt;p&gt;Secretly I’ve built Visual Studio 2003 project with these implementations and ran simple stress test using &lt;a href="http://blogs.msdn.com/alikl/archive/2008/07/20/use-free-tools-from-iis-resource-kit-to-warm-up-your-asp-net-1-1-application-by-batch-compilation.aspx" target="_blank"&gt;TinyGet&lt;/a&gt; utility. The results left us all a bit surprised.&lt;/p&gt;  &lt;h3&gt;Converting Collection To Datatable (Current Situation)&lt;/h3&gt;  &lt;p&gt;&lt;strong&gt;The code:&lt;/strong&gt;&lt;/p&gt;  &lt;div&gt;   &lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;     &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   1:&lt;/span&gt; &lt;span style="color: #008000"&gt;//create custom collection&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   2:&lt;/span&gt; MyCollection myCollection = (MyCollection)SampleServices.GenerateCollection(200);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   3:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   4:&lt;/span&gt; &lt;span style="color: #008000"&gt;//convert collection to datatable&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   5:&lt;/span&gt; DataTable datatable = SampleServices.ConvertCollectionTableIntoDataTalbe(myCollection);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   6:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   7:&lt;/span&gt; &lt;span style="color: #008000"&gt;//bind datatalbe to dynamically created datagrid&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   8:&lt;/span&gt; datagrid.DataSource = datatable;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   9:&lt;/span&gt; datagrid.DataBind();&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;div&gt;&amp;#160;&lt;/div&gt;

&lt;div&gt;&lt;strong&gt;The stress test:&lt;/strong&gt;&lt;/div&gt;

&lt;div&gt;&amp;#160;&lt;/div&gt;

&lt;div&gt;tinyget.exe&amp;#160; -srv:192.168.50.68 -uri:/dynamiccontrolsloadingrelease/UseDataTable.aspx -loop:100 -threads:15&lt;/div&gt;

&lt;div&gt;&amp;#160;&lt;/div&gt;

&lt;div&gt;&lt;strong&gt;The result:&lt;/strong&gt;&lt;/div&gt;

&lt;div&gt;&amp;#160;&lt;/div&gt;

&lt;div&gt;&lt;a href="http://blogs.msdn.com/blogfiles/alikl/WindowsLiveWriter/c1810fdf4ec4_8814/image_2.png"&gt;&lt;img title="image" style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="353" alt="image" src="http://blogs.msdn.com/blogfiles/alikl/WindowsLiveWriter/c1810fdf4ec4_8814/image_thumb.png" width="213" border="0" /&gt;&lt;/a&gt; &lt;/div&gt;

&lt;h3&gt;Bind Collection Directly To Grid&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;The code:&lt;/strong&gt;&lt;/p&gt;

&lt;div&gt;
  &lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;
    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   1:&lt;/span&gt; MyCollection myCollection = (MyCollection)SampleServices.GenerateCollection(200);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   2:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   3:&lt;/span&gt; &lt;span style="color: #008000"&gt;//bind datatalbe to dynamically created datagrid&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   4:&lt;/span&gt; datagrid.DataSource = myCollection;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   5:&lt;/span&gt; datagrid.DataBind();&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;The stress test:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;tinyget.exe&amp;#160; -srv:192.168.50.68 -uri:/dynamiccontrolsloadingrelease/UseCustomCollection.aspx -loop:100 -threads:15&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The result:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/alikl/WindowsLiveWriter/c1810fdf4ec4_8814/image_4.png"&gt;&lt;img title="image" style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="372" alt="image" src="http://blogs.msdn.com/blogfiles/alikl/WindowsLiveWriter/c1810fdf4ec4_8814/image_thumb_1.png" width="243" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h3&gt;Create Datatable From Xml&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;The code:&lt;/strong&gt;&lt;/p&gt;

&lt;div&gt;
  &lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;
    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   1:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; xml = SampleServices.GenerateXml(200);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   2:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   3:&lt;/span&gt; StringReader theReader = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; StringReader(xml);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   4:&lt;/span&gt; DataSet theDataSet = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; DataSet();&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   5:&lt;/span&gt; theDataSet.ReadXml(theReader);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   6:&lt;/span&gt;  &lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   7:&lt;/span&gt; datagrid.DataSource = theDataSet.Tables[0].DefaultView;;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   8:&lt;/span&gt; datagrid.DataBind();&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;The stress test:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;/strong&gt;tinyget.exe&amp;#160; -srv:192.168.50.68 -uri:/dynamiccontrolsloadingrelease/LoadXmlIntoDataTable.aspx -loop:100 -threads:15 &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The result:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;/strong&gt;&lt;a href="http://blogs.msdn.com/blogfiles/alikl/WindowsLiveWriter/c1810fdf4ec4_8814/image_6.png"&gt;&lt;img title="image" style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="434" alt="image" src="http://blogs.msdn.com/blogfiles/alikl/WindowsLiveWriter/c1810fdf4ec4_8814/image_thumb_2.png" width="279" border="0" /&gt;&lt;/a&gt;&amp;#160; &lt;/p&gt;

&lt;h3&gt;Use Xslt Transformation To Create Html Table&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;The code:&lt;/strong&gt;&lt;/p&gt;

&lt;div&gt;
  &lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;
    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   1:&lt;/span&gt; Xml1.DocumentContent = SampleServices.GenerateXml(200);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   2:&lt;/span&gt; Xml1.TransformSource=&lt;span style="color: #006080"&gt;@&amp;quot;xsl.xml&amp;quot;&lt;/span&gt;;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;The stress test:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;/strong&gt;tinyget.exe&amp;#160; -srv:192.168.50.68 -uri:/dynamiccontrolsloadingrelease/XmlXslTransformation.aspx -loop:100 -threads:15 &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The result:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;/strong&gt;&amp;#160;&lt;a href="http://blogs.msdn.com/blogfiles/alikl/WindowsLiveWriter/c1810fdf4ec4_8814/image_8.png"&gt;&lt;img title="image" style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="308" alt="image" src="http://blogs.msdn.com/blogfiles/alikl/WindowsLiveWriter/c1810fdf4ec4_8814/image_thumb_3.png" width="245" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;h3&gt;Use Response.Write()&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;The code:&lt;/strong&gt;&lt;/p&gt;

&lt;div&gt;
  &lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;
    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   1:&lt;/span&gt; MyCollection myCollection = (MyCollection)SampleServices.GenerateCollection(200);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   2:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   3:&lt;/span&gt; &lt;span style="color: #008000"&gt;// Put user code to initialize the page here&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   4:&lt;/span&gt; Response.Write(&lt;span style="color: #006080"&gt;&amp;quot;&amp;lt;table&amp;gt;&amp;quot;&lt;/span&gt;);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   5:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   6:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt;(MyModelItem item &lt;span style="color: #0000ff"&gt;in&lt;/span&gt;  myCollection)&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   7:&lt;/span&gt; {&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   8:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   9:&lt;/span&gt;     Response.Write(&lt;span style="color: #006080"&gt;&amp;quot;&amp;lt;tr&amp;gt;&amp;quot;&lt;/span&gt;);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  10:&lt;/span&gt;     Response.Write(&lt;span style="color: #006080"&gt;&amp;quot;&amp;lt;td&amp;gt;&amp;quot;&lt;/span&gt; +  item.Address  + &lt;span style="color: #006080"&gt;&amp;quot;&amp;lt;td&amp;gt;&amp;quot;&lt;/span&gt;);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  11:&lt;/span&gt;     Response.Write(&lt;span style="color: #006080"&gt;&amp;quot;&amp;lt;td&amp;gt;&amp;quot;&lt;/span&gt; +  item.City  + &lt;span style="color: #006080"&gt;&amp;quot;&amp;lt;td&amp;gt;&amp;quot;&lt;/span&gt;);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  12:&lt;/span&gt;     Response.Write(&lt;span style="color: #006080"&gt;&amp;quot;&amp;lt;td&amp;gt;&amp;quot;&lt;/span&gt; +  item.Education+ &lt;span style="color: #006080"&gt;&amp;quot;&amp;lt;td&amp;gt;&amp;quot;&lt;/span&gt;);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  13:&lt;/span&gt;     Response.Write(&lt;span style="color: #006080"&gt;&amp;quot;&amp;lt;td&amp;gt;&amp;quot;&lt;/span&gt; +  item.Family  + &lt;span style="color: #006080"&gt;&amp;quot;&amp;lt;td&amp;gt;&amp;quot;&lt;/span&gt;);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  14:&lt;/span&gt;     Response.Write(&lt;span style="color: #006080"&gt;&amp;quot;&amp;lt;td&amp;gt;&amp;quot;&lt;/span&gt; +  item.Name  + &lt;span style="color: #006080"&gt;&amp;quot;&amp;lt;td&amp;gt;&amp;quot;&lt;/span&gt;);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  15:&lt;/span&gt;     Response.Write(&lt;span style="color: #006080"&gt;&amp;quot;&amp;lt;/tr&amp;gt;&amp;quot;&lt;/span&gt;);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  16:&lt;/span&gt; }&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  17:&lt;/span&gt; Response.Write(&lt;span style="color: #006080"&gt;&amp;quot;&amp;lt;/table&amp;gt;&amp;quot;&lt;/span&gt;);&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;The stress test:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;/strong&gt;tinyget.exe&amp;#160; -srv:192.168.50.68 -uri:/dynamiccontrolsloadingrelease/ResponseWrite.aspx -loop:100 -threads:15 &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The result:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;/strong&gt;&lt;a href="http://blogs.msdn.com/blogfiles/alikl/WindowsLiveWriter/c1810fdf4ec4_8814/image_10.png"&gt;&lt;img title="image" style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="219" alt="image" src="http://blogs.msdn.com/blogfiles/alikl/WindowsLiveWriter/c1810fdf4ec4_8814/image_thumb_4.png" width="227" border="0" /&gt;&lt;/a&gt;&amp;#160; &lt;/p&gt;

&lt;h3&gt;Sample Visual Studio 2003 Project&lt;/h3&gt;

&lt;p&gt;Interested in testing it yourself? Grab the source code from my SkyDrive &lt;a href="http://cid-dd25b83e4ca261f7.skydrive.live.com/self.aspx/Visual%20Studio%20Projects/DynamicControlsLoading.zip" target="_blank"&gt;here&lt;/a&gt;:&lt;/p&gt;

&lt;p&gt;&lt;iframe style="border-right: #dde5e9 1px solid; padding-right: 0px; border-top: #dde5e9 1px solid; padding-left: 0px; padding-bottom: 0px; margin: 3px; border-left: #dde5e9 1px solid; width: 240px; padding-top: 0px; border-bottom: #dde5e9 1px solid; height: 66px; background-color: #ffffff" marginwidth="0" marginheight="0" src="http://cid-dd25b83e4ca261f7.skydrive.live.com/embedrowdetail.aspx/Visual%20Studio%20Projects/DynamicControlsLoading.zip" frameborder="0" scrolling="no"&gt;&lt;/iframe&gt;&lt;/p&gt;

&lt;h3&gt;Conclusion&lt;/h3&gt;

&lt;p&gt;After conducting this simple test these are the conclusions I’ve made:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;“Don't be afraid to challenge the pros, even in their own backyard.&amp;quot; - &lt;a href="http://jtaylorgoodlife.blogspot.com/2007/06/how-to-get-things-done-colin-powell.html" target="_blank"&gt;How to Get Things Done - Colin Powell Version&lt;/a&gt; &lt;/li&gt;

  &lt;li&gt;Testing IS DA thing. Assumptions are good but nothing speaks louder than facts. &lt;/li&gt;

  &lt;li&gt;Test early - avoid massive rework afterwards. Create POC's (Proof of concept) early in architecture/design stages. &lt;/li&gt;

  &lt;li&gt;Best performance comes on expense of productivity and coolness. &lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;Related Materials&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href="http://blogs.msdn.com/alikl/archive/2008/07/20/use-free-tools-from-iis-resource-kit-to-warm-up-your-asp-net-1-1-application-by-batch-compilation.aspx"&gt;Use FREE Tools From IIS Resource Kit To Warm Up Your ASP.NET 1.1 Application By Batch Compilation&lt;/a&gt; &lt;/li&gt;

  &lt;li&gt;&lt;a href="http://blogs.msdn.com/alikl/archive/2008/05/05/asp-net-performance-engineering-stress-test-your-architecture-design-and-code.aspx"&gt;ASP.NET Performance Engineering - Stress Test Your Architecture, Design, And Code&lt;/a&gt; &lt;/li&gt;

  &lt;li&gt;&lt;a href="http://blogs.msdn.com/alikl/archive/2008/04/28/performance-sin-chatty-database-access-and-loops-plus-another-free-performance-tool.aspx"&gt;Performance Sin - Chatty Database Access And Loops (Plus Another Free Performance Tool)&lt;/a&gt; &lt;/li&gt;

  &lt;li&gt;&lt;a href="http://blogs.msdn.com/alikl/archive/2008/03/09/stress-test-asp-net-web-application-with-free-wcat-tool.aspx"&gt;Stress Test ASP.NET Web Application With Free WCAT Tool&lt;/a&gt; &lt;/li&gt;

  &lt;li&gt;&lt;a href="http://blogs.msdn.com/alikl/archive/2008/01/21/performance-code-review-tool-practices-checker.aspx"&gt;Performance Code Review Tool – Practices Checker&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8793251" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/alikl/archive/tags/Test+Phase/default.aspx">Test Phase</category><category domain="http://blogs.msdn.com/alikl/archive/tags/Development+Phase/default.aspx">Development Phase</category><category domain="http://blogs.msdn.com/alikl/archive/tags/Implementation/default.aspx">Implementation</category><category domain="http://blogs.msdn.com/alikl/archive/tags/Performance/default.aspx">Performance</category><category domain="http://blogs.msdn.com/alikl/archive/tags/Planning+Phase/default.aspx">Planning Phase</category></item><item><title>Improve ASP.NET Performance With Multithreading Using Thread Or ThreadPool Objects</title><link>http://blogs.msdn.com/alikl/archive/2008/06/23/improve-asp-net-performance-with-multithreading-using-thread-or-threadpool-objects.aspx</link><pubDate>Mon, 23 Jun 2008 13:27:12 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8641772</guid><dc:creator>alikl</dc:creator><slash:comments>8</slash:comments><comments>http://blogs.msdn.com/alikl/comments/8641772.aspx</comments><wfw:commentRss>http://blogs.msdn.com/alikl/commentrss.aspx?PostID=8641772</wfw:commentRss><wfw:comment>http://blogs.msdn.com/alikl/rsscomments.aspx?PostID=8641772</wfw:comment><description>&lt;p&gt;Your ASP.NET application performs slower than expected? How many times do you access your database for each request? Did you use &lt;a href="http://msdn.microsoft.com/en-us/library/ms187929.aspx" target="_blank"&gt;SQL Profiler&lt;/a&gt; to find out? Did you know that &lt;a href="http://blogs.msdn.com/alikl/archive/2008/04/28/performance-sin-chatty-database-access-and-loops-plus-another-free-performance-tool.aspx" target="_blank"&gt;chatty database access is one of the most common performance sins&lt;/a&gt; (this is my own observation)?&lt;/p&gt;  &lt;p&gt;Want to improve? - Reduce the number of database queries.&lt;/p&gt;  &lt;p&gt;Not an option? - You may want using multithreading. Proceed with caution.&lt;/p&gt;  &lt;h3&gt;&lt;b&gt;Real world scenario&lt;/b&gt;&lt;/h3&gt;  &lt;p&gt;You need to run 3 independent heavy database queries – Q1, Q2, Q3. You need to use the result from the three – Result = Q1+Q2+Q3. You never know the order in which each query completes. It can be: Q1Q2Q3, Q1Q3Q2, Q2Q3Q1, Q2Q1Q3, Q3Q2Q1, Q3Q1Q2. Missed any combination? Run one after another would create significant latency. Running each on its own thread would create latency of Max(Q1,Q2,Q3) which is less that sum of the three.&lt;/p&gt;  &lt;p&gt;How to run each query on its own thread and wait for the completion for each one?&lt;/p&gt;  &lt;h3&gt;Using Thread object&lt;/h3&gt;  &lt;p&gt;The following code spawns three threads and waits for all to join, and then use the results from each.&lt;/p&gt;  &lt;div&gt;   &lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;     &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   1:&lt;/span&gt; Thread t1 = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Thread(DoWork1);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   2:&lt;/span&gt; t1.Start();&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   3:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   4:&lt;/span&gt; Thread t2 = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Thread(DoWork2);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   5:&lt;/span&gt; t2.Start();&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   6:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   7:&lt;/span&gt; Thread t3 = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Thread(DoWork3);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   8:&lt;/span&gt; t3.Start();&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   9:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  10:&lt;/span&gt; t1.Join(1000);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  11:&lt;/span&gt; t2.Join(1000);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  12:&lt;/span&gt; t3.Join(1000);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  13:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  14:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  15:&lt;/span&gt; Label1.Text = (i1 + i2 + i3).ToString();&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;See any issues using it in ASP.NET? One caveat though, creating threads manually like this may cause performance hit as it involves context switching – CPU consuming operation. Consider using ThreadPool object.&lt;/p&gt;

&lt;h3&gt;Using ThreadPool object (preferred, but are you using COM?)&lt;/h3&gt;

&lt;p&gt;ThreadPool is preferred as it already has live threads allocated for you. No need to ask for a favor from CPU in the moment of truth. Here is the code:&lt;/p&gt;

&lt;div&gt;
  &lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;
    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   1:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;partial&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; _Default : System.Web.UI.Page&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   2:&lt;/span&gt; {&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   3:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; i1 = 0;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   4:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; i2 = 0;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   5:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; i3 = 0;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   6:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   7:&lt;/span&gt;     WaitHandle[] waitHandles = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; WaitHandle[]&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   8:&lt;/span&gt;         { &lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   9:&lt;/span&gt;             &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; ManualResetEvent(&lt;span style="color: #0000ff"&gt;false&lt;/span&gt;),&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  10:&lt;/span&gt;             &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; ManualResetEvent(&lt;span style="color: #0000ff"&gt;false&lt;/span&gt;),&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  11:&lt;/span&gt;             &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; ManualResetEvent(&lt;span style="color: #0000ff"&gt;false&lt;/span&gt;) &lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  12:&lt;/span&gt;         };&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  13:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  14:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  15:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;protected&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Page_Load(&lt;span style="color: #0000ff"&gt;object&lt;/span&gt; sender, EventArgs e)&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  16:&lt;/span&gt;     {&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  17:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  18:&lt;/span&gt;     }&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  19:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  20:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;protected&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Button1_Click(&lt;span style="color: #0000ff"&gt;object&lt;/span&gt; sender, EventArgs e)&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  21:&lt;/span&gt;     {&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  22:&lt;/span&gt;         Stopwatch sw = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Stopwatch();&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  23:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  24:&lt;/span&gt;         sw.Start();&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  25:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  26:&lt;/span&gt;         WaitCallback method1 = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; WaitCallback(DoWork1);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  27:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;bool&lt;/span&gt; isQueued1 = ThreadPool.QueueUserWorkItem(method1, waitHandles[0]);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  28:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  29:&lt;/span&gt;         WaitCallback method2 = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; WaitCallback(DoWork2);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  30:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;bool&lt;/span&gt; isQueued2 = ThreadPool.QueueUserWorkItem(method2, waitHandles[1]);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  31:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  32:&lt;/span&gt;         WaitCallback method3 = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; WaitCallback(DoWork3);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  33:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;bool&lt;/span&gt; isQueued3 = ThreadPool.QueueUserWorkItem(method3, waitHandles[2]);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  34:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  35:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (WaitHandle.WaitAll(waitHandles, 5000, &lt;span style="color: #0000ff"&gt;false&lt;/span&gt;))&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  36:&lt;/span&gt;             Label1.Text = (i1 + i2 + i3).ToString();&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  37:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;else&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  38:&lt;/span&gt;             Label1.Text = &lt;span style="color: #006080"&gt;&amp;quot;Problem&amp;quot;&lt;/span&gt;;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  39:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  40:&lt;/span&gt;     }&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  41:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  42:&lt;/span&gt;      &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; DoWork1(&lt;span style="color: #0000ff"&gt;object&lt;/span&gt; state)&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  43:&lt;/span&gt;     {&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  44:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;int&lt;/span&gt;.TryParse(TextBox1.Text, &lt;span style="color: #0000ff"&gt;out&lt;/span&gt; i1);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  45:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  46:&lt;/span&gt;         &lt;span style="color: #008000"&gt;//HEAVY QUERY GOES HERE. Sleep is for the demo only! Remove it!&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  47:&lt;/span&gt;         Thread.Sleep(i1);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  48:&lt;/span&gt;         ManualResetEvent mre = (ManualResetEvent)state;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  49:&lt;/span&gt;         mre.Set();&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  50:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  51:&lt;/span&gt;     }&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  52:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  53:&lt;/span&gt;      &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; DoWork2(&lt;span style="color: #0000ff"&gt;object&lt;/span&gt; state)&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  54:&lt;/span&gt;     {&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  55:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;int&lt;/span&gt;.TryParse(TextBox2.Text, &lt;span style="color: #0000ff"&gt;out&lt;/span&gt;  i2);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  56:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  57:&lt;/span&gt;         &lt;span style="color: #008000"&gt;//HEAVY QUERY GOES HERE. Sleep is for the demo only! Remove it!&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  58:&lt;/span&gt;         Thread.Sleep(i2);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  59:&lt;/span&gt;         ManualResetEvent mre = (ManualResetEvent)state;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  60:&lt;/span&gt;         mre.Set();&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  61:&lt;/span&gt;     }&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  62:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  63:&lt;/span&gt;      &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; DoWork3(&lt;span style="color: #0000ff"&gt;object&lt;/span&gt; state)&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  64:&lt;/span&gt;     {&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  65:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;int&lt;/span&gt;.TryParse(TextBox3.Text, &lt;span style="color: #0000ff"&gt;out&lt;/span&gt;  i3);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  66:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  67:&lt;/span&gt;         &lt;span style="color: #008000"&gt;//HEAVY QUERY GOES HERE. Sleep is for the demo only! Remove it!&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  68:&lt;/span&gt;         Thread.Sleep(i3);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  69:&lt;/span&gt;         ManualResetEvent mre = (ManualResetEvent)state;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  70:&lt;/span&gt;         mre.Set();&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  71:&lt;/span&gt;     }&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  72:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  73:&lt;/span&gt; }&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;h3&gt;Caveat&lt;/h3&gt;

&lt;p&gt;If your function calls on COM object – avoid using ThreadPool, its Apartment model is not compatible with COM and it cannot be changed. More info - &lt;a href="http://blogs.msdn.com/alikl/archive/2008/06/20/pitfalls-with-net-multithreading-and-com-objects-threads-must-have-compatible-apartment-models-mta-vs-sta.aspx"&gt;Pitfalls With .Net Multithreading And COM Objects – Threads Must Have Compatible Apartment Models (MTA vs. STA)&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Another point to call out is that performance improvements based on multithreading is subject to amount of available CPU’s. Test your solution first!&lt;/p&gt;

&lt;p&gt;This post was written with help from Lior, &lt;a href="http://blogs.microsoft.co.il/blogs/mcs" target="_blank"&gt;MCS Israel&lt;/a&gt; Architect. &lt;/p&gt;

&lt;p&gt;Download &lt;a href="http://ywv0xg.bay.livefilestore.com/y1pvMbtoAAyxz6KIPYkthf1HMXx0X18a6AWRMnj8rZfROFjx_NZiPF5AFsz6oXg06-YY2jupaldvTUoP0JNVb4G4g/SynchThreadsInWebPage.zip?download" target="_blank"&gt;sample project&lt;/a&gt; with the code from my SkyDrive:&lt;/p&gt;

&lt;p&gt;&lt;iframe style="border-right: #dde5e9 1px solid; padding-right: 0px; border-top: #dde5e9 1px solid; padding-left: 0px; padding-bottom: 0px; margin: 3px; border-left: #dde5e9 1px solid; width: 240px; padding-top: 0px; border-bottom: #dde5e9 1px solid; height: 66px; background-color: #ffffff" marginwidth="0" marginheight="0" src="http://cid-dd25b83e4ca261f7.skydrive.live.com/embedrowdetail.aspx/Visual%20Studio%20Projects/SynchThreadsInWebPage.zip" frameborder="0" scrolling="no"&gt;&lt;/iframe&gt;&lt;/p&gt;

&lt;p&gt;Enjoy.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8641772" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/alikl/archive/tags/Development+Phase/default.aspx">Development Phase</category><category domain="http://blogs.msdn.com/alikl/archive/tags/Performance/default.aspx">Performance</category><category domain="http://blogs.msdn.com/alikl/archive/tags/Threading/default.aspx">Threading</category></item><item><title>Pitfalls With .Net Multithreading And COM Objects – Threads Must Have Compatible Apartment Models (MTA vs. STA)</title><link>http://blogs.msdn.com/alikl/archive/2008/06/20/pitfalls-with-net-multithreading-and-com-objects-threads-must-have-compatible-apartment-models-mta-vs-sta.aspx</link><pubDate>Fri, 20 Jun 2008 10:09:11 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8624899</guid><dc:creator>alikl</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/alikl/comments/8624899.aspx</comments><wfw:commentRss>http://blogs.msdn.com/alikl/commentrss.aspx?PostID=8624899</wfw:commentRss><wfw:comment>http://blogs.msdn.com/alikl/rsscomments.aspx?PostID=8624899</wfw:comment><description>&lt;p&gt;&lt;/p&gt;  &lt;p&gt;Be alert when implementing multithreading .Net in conjunction with COM objects. Thread apartment models matter.&lt;/p&gt;  &lt;p&gt;.Net threads have Multi Threaded Apartment (MTA) model by default. COM objects have Single Thread Apartment (STA). Calling on COM objects on .Net threads that you spawn may cause unpredicted result. &lt;/p&gt;  &lt;p&gt;Multithreading in .Net is easily implemented based on either Thread or ThreadPool objects. Thread.Start() method spawns new thread which has Multi Threaded Apartment (MTA) model . ThreadPool.QueueUserWorkItem(myMethod) queues myMethod to be executed on available thread managed by it.&lt;/p&gt;  &lt;h3&gt;Thread object and COM&lt;/h3&gt;  &lt;p&gt;When spawning your own threads with Thread.Start() set Thread’s apartment model to STA if you plan calling COM object on it:&lt;/p&gt;  &lt;div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; background-color: #f4f4f4"&gt;   &lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;     &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   1:&lt;/span&gt; Thread t = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Thread(DoWork);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   2:&lt;/span&gt; t.SetApartmentState(ApartmentState.STA);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   3:&lt;/span&gt; t.Start();&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;Note, spawning your own threads with Thread.Start() utilizes resources – mainly CPU, spawning too much threads may cause performance hit. Consider using ThreadPool object.&lt;/p&gt;

&lt;h3&gt;ThreadPool object and COM&lt;/h3&gt;

&lt;p&gt;All threads managed by ThreadPool objects are MTA threads. Apartment model of ThreadPool’s threads cannot be changed. Do not call COM objects on ThreadPool’s threads.&lt;/p&gt;

&lt;h3&gt;Possible designs and solutions&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;ASP.NET.&lt;/strong&gt; Calling COM objects in ASP.NET pages configure &lt;a href="http://msdn.microsoft.com/en-us/library/zwk9h2kb.aspx"&gt;AspCompat=true&lt;/a&gt;: &lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
  &lt;p&gt;“Most COM components will work with ASP.NET…”&lt;/p&gt;

  &lt;p&gt;“…The AspCompat attribute forces the page to execute in STA mode. The runtime throws an exception if the compatibility tag is omitted and an STA component is referenced on the page.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Client side (like WinForms)multithreading&lt;/strong&gt;. Use ThreadPool with its MTA threads. Spawn another thread with Thread.Start() setting its apartment model to STA. &lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;Custom server side multithreading&lt;/strong&gt;. I’d consider using Remoting infrastructure. In case your server application should serve messages that are not covered by Remoting’s built in mechanisms I’d consider building my own &lt;a href="http://msdn.microsoft.com/en-us/library/aa309086(VS.71).aspx"&gt;Channel sink&lt;/a&gt;. &lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;And the last but not the least&lt;/strong&gt;. Do not forget to call Marshal.ReleaseComObject(obj) to dispose the COM objects created on your the threads. &lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;Related materials&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/ms998551.aspx"&gt;Chapter 7 — Improving Interop Performance&lt;/a&gt; &lt;/li&gt;

  &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/ms998565.aspx" target="_blank"&gt;Chapter 11 — Improving Remoting Performance&lt;/a&gt; &lt;/li&gt;

  &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/ms979193.aspx" target="_blank"&gt;How To: Improve Serialization Performance&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8624899" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/alikl/archive/tags/Development+Phase/default.aspx">Development Phase</category><category domain="http://blogs.msdn.com/alikl/archive/tags/Implementation/default.aspx">Implementation</category><category domain="http://blogs.msdn.com/alikl/archive/tags/Performance/default.aspx">Performance</category><category domain="http://blogs.msdn.com/alikl/archive/tags/Planning+Phase/default.aspx">Planning Phase</category><category domain="http://blogs.msdn.com/alikl/archive/tags/Threading/default.aspx">Threading</category></item><item><title>WCF Security - Input/Data Validation Using Schemas</title><link>http://blogs.msdn.com/alikl/archive/2008/05/25/wcf-security-input-data-validation-using-schemas.aspx</link><pubDate>Sun, 25 May 2008 17:52:48 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8551021</guid><dc:creator>alikl</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/alikl/comments/8551021.aspx</comments><wfw:commentRss>http://blogs.msdn.com/alikl/commentrss.aspx?PostID=8551021</wfw:commentRss><wfw:comment>http://blogs.msdn.com/alikl/rsscomments.aspx?PostID=8551021</wfw:comment><description>&lt;p&gt;WCF offers very flexible approach of Input and Data Validation based on XML Schemas. The approach is flexible since the validation rules are expressed in form of XML schema and can be changed at any time without recompiling the solution.&lt;/p&gt;  &lt;p&gt;I followed the steps detailed in &lt;a href="http://www.codeplex.com/WCFSecurity/Wiki/View.aspx?title=How%20To%20-%20Perform%20Message%20Validation%20with%20Schemas%20in%20WCF&amp;amp;referringTitle=How%20Tos" target="_blank"&gt;How To: Perform Message Validation with Schema Validation in WCF&lt;/a&gt; and ended up with another working sample (imagine that!).&lt;/p&gt;  &lt;p&gt;It took me a bit to struggle with the schema thing and then &lt;a href="http://msdn.microsoft.com/en-us/library/ms788993.aspx" target="_blank"&gt;enabling debugging&lt;/a&gt; info on the service side (remember, WCF is secure by default) to understand what's going on and why it fails time after time.&lt;/p&gt;  &lt;p&gt;In the end me and WCF made friends and I'd thought it'd be good to share with you the Visual Studio project. Download it &lt;a href="http://cid-dd25b83e4ca261f7.skydrive.live.com/self.aspx/Visual%20Studio%20Projects/WCFInputValidationSchema.zip" target="_blank"&gt;here&lt;/a&gt; from my SkyDrive and save yourself some time. &lt;/p&gt;  &lt;p&gt;&lt;iframe style="border-right: #dde5e9 1px solid; padding-right: 0px; border-top: #dde5e9 1px solid; padding-left: 0px; padding-bottom: 0px; margin: 3px; border-left: #dde5e9 1px solid; width: 240px; padding-top: 0px; border-bottom: #dde5e9 1px solid; height: 66px; background-color: #ffffff" marginwidth="0" marginheight="0" src="http://cid-dd25b83e4ca261f7.skydrive.live.com/embedrowdetail.aspx/Visual%20Studio%20Projects/WCFInputValidationSchema.zip" frameborder="0" scrolling="no"&gt;&lt;/iframe&gt;&lt;/p&gt;  &lt;h3&gt;My related posts&lt;/h3&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://blogs.msdn.com/alikl/archive/2008/05/25/wcf-security-input-data-validation-sample-visual-studio-project.aspx"&gt;WCF Security - Input/Data Validation Sample Visual Studio Project&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Enjoy.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8551021" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/alikl/archive/tags/WCF/default.aspx">WCF</category><category domain="http://blogs.msdn.com/alikl/archive/tags/Input+Validation/default.aspx">Input Validation</category><category domain="http://blogs.msdn.com/alikl/archive/tags/Security/default.aspx">Security</category><category domain="http://blogs.msdn.com/alikl/archive/tags/Development+Phase/default.aspx">Development Phase</category><category domain="http://blogs.msdn.com/alikl/archive/tags/Implementation/default.aspx">Implementation</category></item><item><title>WCF Security - Input/Data Validation Sample Visual Studio Project</title><link>http://blogs.msdn.com/alikl/archive/2008/05/25/wcf-security-input-data-validation-sample-visual-studio-project.aspx</link><pubDate>Sun, 25 May 2008 14:17:44 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8550886</guid><dc:creator>alikl</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/alikl/comments/8550886.aspx</comments><wfw:commentRss>http://blogs.msdn.com/alikl/commentrss.aspx?PostID=8550886</wfw:commentRss><wfw:comment>http://blogs.msdn.com/alikl/rsscomments.aspx?PostID=8550886</wfw:comment><description>&lt;p&gt;Input and Data Validation is one of the &lt;a href="http://shapingsoftware.com/2008/04/07/security-frame/" target="_blank"&gt;core security principles&lt;/a&gt;. &lt;a href="http://blogs.msdn.com/alikl/archive/2007/03/04/how-to-hack-wcf-new-technology-old-hacking-tricks.aspx" target="_blank"&gt;WCF is no exception&lt;/a&gt;. To get most out of WCF in secure way one must implement proper Input and Data Validation.&lt;/p&gt;  &lt;p&gt;I was following instructions on &lt;a href="http://www.codeplex.com/WCFSecurity/Wiki/View.aspx?title=How%20To%20-%20Perform%20Input%20Validation%20in%20WCF&amp;amp;referringTitle=How%20Tos" target="_blank"&gt;How To &amp;#8211; Perform Input Validation in WCF&lt;/a&gt; compiled by patterns&amp;amp;practice team lead by &lt;a href="http://blogs.msdn.com/jmeier" target="_blank"&gt;JD Meier&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;In a nutshell the process consists of creating 3 classes and tweaking a config file a &amp;quot;bit&amp;quot;.&lt;/p&gt;  &lt;p&gt;From the guide:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Step 4 &amp;#8211; Create a Class That Implements the Validation Logic &lt;/li&gt;    &lt;li&gt;Step 5 &amp;#8211; Create a Class That Implements a Custom Endpoint Behavior &lt;/li&gt;    &lt;li&gt;Step 6 &amp;#8211; Create a Class That Implements a Custom Configuration Element &lt;/li&gt;    &lt;li&gt;Step 7 &amp;#8211; Add the Custom Behavior to the Configuration File &lt;/li&gt;    &lt;li&gt;Step 8 &amp;#8211; Create an Endpoint Behavior and Map It to Use the Custom Behavior &lt;/li&gt;    &lt;li&gt;Step 9 &amp;#8211; Configure the Service Endpoint to Use the Endpoint Behavior &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;I ended up with working sample built with Visual Studio 2008. I though it'd be good idea to share it to help you boost your productivity.&lt;/p&gt;  &lt;p&gt;Grab the Visual Studio project on my SkyDrive &lt;a href="http://cid-dd25b83e4ca261f7.skydrive.live.com/self.aspx/Visual%20Studio%20Projects/WCFInputValidation.zip" target="_blank"&gt;here&lt;/a&gt;. &lt;/p&gt; &lt;iframe style="border-right: #dde5e9 1px solid; padding-right: 0px; border-top: #dde5e9 1px solid; padding-left: 0px; padding-bottom: 0px; margin: 3px; border-left: #dde5e9 1px solid; width: 240px; padding-top: 0px; border-bottom: #dde5e9 1px solid; height: 66px; background-color: #ffffff" marginwidth="0" marginheight="0" src="http://cid-dd25b83e4ca261f7.skydrive.live.com/embedrowdetail.aspx/Visual%20Studio%20Projects/WCFInputValidation.zip" frameborder="0" scrolling="no"&gt;&lt;/iframe&gt;  &lt;p&gt;Enjoy&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8550886" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/alikl/archive/tags/WCF/default.aspx">WCF</category><category domain="http://blogs.msdn.com/alikl/archive/tags/Input+Validation/default.aspx">Input Validation</category><category domain="http://blogs.msdn.com/alikl/archive/tags/Security/default.aspx">Security</category><category domain="http://blogs.msdn.com/alikl/archive/tags/Development+Phase/default.aspx">Development Phase</category><category domain="http://blogs.msdn.com/alikl/archive/tags/Implementation/default.aspx">Implementation</category></item><item><title>ASP.NET Performance Sin - Serving Images Dynamically (Or Another Reason To Love Fiddler)</title><link>http://blogs.msdn.com/alikl/archive/2008/05/02/asp-net-performance-sin-serving-images-dynamically-or-another-reason-to-love-fiddler.aspx</link><pubDate>Fri, 02 May 2008 16:12:19 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8450457</guid><dc:creator>alikl</dc:creator><slash:comments>7</slash:comments><comments>http://blogs.msdn.com/alikl/comments/8450457.aspx</comments><wfw:commentRss>http://blogs.msdn.com/alikl/commentrss.aspx?PostID=8450457</wfw:commentRss><wfw:comment>http://blogs.msdn.com/alikl/rsscomments.aspx?PostID=8450457</wfw:comment><description>&lt;p&gt;Serving images dynamically may cause performance hit. Dynamically served images require more HTTP requests which violates &lt;a href="http://stevesouders.com/bio.html" target="_blank"&gt;Steve Souders'&lt;/a&gt; performance rule #1 - &lt;a href="http://developer.yahoo.com/performance/rules.html#num_http"&gt;Make Fewer HTTP Requests&lt;/a&gt;. The latency is also caused by parallelism (or parallel downloading) limitations as described in detail here &lt;a href="http://yuiblog.com/blog/2007/04/11/performance-research-part-4/"&gt;Performance Research, Part 4: Maximizing Parallel Downloads in the Carpool Lane&lt;/a&gt;&lt;/p&gt;  &lt;h3&gt;Static Images&lt;/h3&gt;  &lt;p&gt;Below are the series of images that served dynamically and static.&lt;/p&gt;  &lt;p&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="244" alt="image" src="http://blogs.msdn.com/blogfiles/alikl/WindowsLiveWriter/PerformanceSinServingImagesDynamically_BD45/image_11.png" width="157" border="0" /&gt; &lt;/p&gt;  &lt;p&gt;Static images displayed using GridView's ImageFiled column type. ImageField generates the following HTML mark-up:&lt;/p&gt;  &lt;div&gt;   &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;img&lt;/span&gt; &lt;span style="color: #ff0000"&gt;src&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;IMAGES/Birds/icon-penguin.gif&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;style&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;border-width:0px;&amp;quot;&lt;/span&gt; &lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Browser interprets it as a static image and is ready to cache it for further reuse.&lt;/p&gt;

&lt;h3&gt;Serving Images Dynamically&lt;/h3&gt;

&lt;p&gt;Below is the sample code that implements dynamic image serving. I witness in the field different variation but the pattern (I'd call it anti-pattern) remains the same. ASP.NET and HTML mark-up that is usually part of repeater control looks similar to the following:&lt;/p&gt;

&lt;div&gt;
  &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;p&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;img&lt;/span&gt; &lt;span style="color: #ff0000"&gt;src&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;ServeImage.ashx?FN = &lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;%#DataBinder.GetPropertyValue(Container.DataItem, &amp;quot;&lt;/span&gt;&lt;span style="color: #ff0000"&gt;Image&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;quot;)%&amp;gt;&amp;quot;&lt;/span&gt; &lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color: #0000ff"&gt;&lt;/span&gt;&amp;#160;&lt;/p&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;ASHX file's code that actually serves the image looks similar to this:&lt;/p&gt;

&lt;div&gt;
  &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;p&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; ProcessRequest(HttpContext context)
{
    &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; imageFileName = &lt;/p&gt;&lt;p&gt;           context.Request.MapPath(&lt;span style="color: #006080"&gt;@&amp;quot;IMAGES\&amp;quot; + context.Request.QueryString[&amp;quot;&lt;/span&gt;FN&lt;span style="color: #006080"&gt;&amp;quot;]);

    context.Response.ContentType = &amp;quot;&lt;/span&gt;image/jpeg&amp;quot;;
    context.Response.WriteFile(imageFileName);
    context.Response.Flush();
    context.Response.Close();

}&lt;/p&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h3&gt;Network Analysis&lt;/h3&gt;

&lt;p&gt;Using one of my most favorite tools - &lt;a href="http://www.fiddler2.com/fiddler2/" target="_blank"&gt;Fiddler&lt;/a&gt; - it is easy to reveal browser's view on the traffic:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/alikl/WindowsLiveWriter/PerformanceSinServingImagesDynamically_BD45/image_4.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="100" alt="image" src="http://blogs.msdn.com/blogfiles/alikl/WindowsLiveWriter/PerformanceSinServingImagesDynamically_BD45/image_thumb_1.png" width="539" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There is expiration attribute attached to static images while dynamically served images do not have such attribute. &lt;/p&gt;

&lt;p&gt;Subsequent call the the same page that gets the same images reveals the following:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/alikl/WindowsLiveWriter/PerformanceSinServingImagesDynamically_BD45/image_8.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="149" alt="image" src="http://blogs.msdn.com/blogfiles/alikl/WindowsLiveWriter/PerformanceSinServingImagesDynamically_BD45/image_thumb_3.png" width="538" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;All dynamically served images are not cached and utilize the network on each request. &lt;/p&gt;

&lt;p&gt;Further investigation shows, using &lt;a href="http://blogs.msdn.com/alikl/archive/2007/10/17/improve-web-application-performance-by-reducing-number-of-http-requests-fiddler-to-the-rescue.aspx" target="_blank"&gt;Fiddler's P and C fantastic feature&lt;/a&gt;, that overall network utilization caused by these dynamically served images is about 350 KB, which could be saved by caching the images.&lt;/p&gt;

&lt;h3&gt;Recommendations&lt;/h3&gt;

&lt;p&gt;Avoid serving images dynamically. Follow best practices outlined at &lt;a href="http://developer.yahoo.com/performance/" target="_blank"&gt;Exceptional Performance&lt;/a&gt;:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href="http://developer.yahoo.com/performance/rules.html#num_http" target="_blank"&gt;Make Fewer HTTP Requests&lt;/a&gt; &lt;/li&gt;

  &lt;li&gt;&lt;a href="http://developer.yahoo.com/performance/rules.html#dns_lookups" target="_blank"&gt;Reduce DNS Lookups&lt;/a&gt; &lt;/li&gt;

  &lt;li&gt;&lt;a href="http://developer.yahoo.com/performance/rules.html#redirects" target="_blank"&gt;Avoid Redirects&lt;/a&gt; &lt;/li&gt;

  &lt;li&gt;&lt;a href="http://developer.yahoo.com/performance/rules.html#cacheajax" target="_blank"&gt;Make Ajax Cacheable&lt;/a&gt; &lt;/li&gt;

  &lt;li&gt;&lt;a href="http://developer.yahoo.com/performance/rules.html#postload" target="_blank"&gt;Post-load Components&lt;/a&gt; &lt;/li&gt;

  &lt;li&gt;&lt;a href="http://developer.yahoo.com/performance/rules.html#preload" target="_blank"&gt;Preload Components&lt;/a&gt; &lt;/li&gt;

  &lt;li&gt;&lt;a href="http://developer.yahoo.com/performance/rules.html#min_dom" target="_blank"&gt;Reduce the Number of DOM Elements&lt;/a&gt; &lt;/li&gt;

  &lt;li&gt;&lt;a href="http://developer.yahoo.com/performance/rules.html#split" target="_blank"&gt;Split Components Across Domains&lt;/a&gt; &lt;/li&gt;

  &lt;li&gt;&lt;a href="http://developer.yahoo.com/performance/rules.html#iframes" target="_blank"&gt;Minimize the Number of iframes&lt;/a&gt; &lt;/li&gt;

  &lt;li&gt;&lt;a href="http://developer.yahoo.com/performance/rules.html#no404" target="_blank"&gt;No 404s&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;My relative posts&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href="http://blogs.msdn.com/alikl/archive/2008/04/28/performance-sin-chatty-database-access-and-loops-plus-another-free-performance-tool.aspx"&gt;Performance Sin - Chatty Database Access And Loops (Plus Another Free Performance Tool)&lt;/a&gt; &lt;/li&gt;

  &lt;li&gt;&lt;a href="http://blogs.msdn.com/alikl/archive/2008/02/02/performance-sin-using-exceptions-to-control-flow.aspx"&gt;Performance Sin - Using Exceptions To Control Flow&lt;/a&gt;&amp;#160; &lt;/li&gt;
&lt;/ul&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8450457" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/alikl/archive/tags/Test+Phase/default.aspx">Test Phase</category><category domain="http://blogs.msdn.com/alikl/archive/tags/Tools/default.aspx">Tools</category><category domain="http://blogs.msdn.com/alikl/archive/tags/Development+Phase/default.aspx">Development Phase</category><category domain="http://blogs.msdn.com/alikl/archive/tags/Implementation/default.aspx">Implementation</category><category domain="http://blogs.msdn.com/alikl/archive/tags/Performance/default.aspx">Performance</category><category domain="http://blogs.msdn.com/alikl/archive/tags/Planning+Phase/default.aspx">Planning Phase</category></item><item><title>Free Performance Tool - Analyze IIS Logs Like A Pro With Funnel Web Analyzer</title><link>http://blogs.msdn.com/alikl/archive/2008/04/21/free-performance-tool-analyze-iis-logs-like-a-pro-with-funnel-web-analyzer.aspx</link><pubDate>Mon, 21 Apr 2008 13:16:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8414362</guid><dc:creator>alikl</dc:creator><slash:comments>10</slash:comments><comments>http://blogs.msdn.com/alikl/comments/8414362.aspx</comments><wfw:commentRss>http://blogs.msdn.com/alikl/commentrss.aspx?PostID=8414362</wfw:commentRss><wfw:comment>http://blogs.msdn.com/alikl/rsscomments.aspx?PostID=8414362</wfw:comment><description>&lt;P&gt;These free performance tools will save you time and money identifying performance bottlenecks. Your customers will thank you for building fast and responsive applications.&lt;/P&gt;
&lt;H3&gt;Funnel Web Analyzer 5.0 for Windows&lt;/H3&gt;
&lt;P&gt;Download the tool &lt;A href="http://www.quest.com/funnel-web-analyzer/software-downloads.asp" target=_blank mce_href="http://www.quest.com/funnel-web-analyzer/software-downloads.asp"&gt;here.&lt;/A&gt;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;Analyzer gives insight into everything from server load and customer usage to intranet analysis. It allows you to gain vital feedback on visitor behaviour and preferences, so you can more accurately customize your site to meet the needs of your clients&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;H3&gt;How- to use&lt;/H3&gt;
&lt;P&gt;After you download and install the tool &lt;!--more--&gt;- run it. Just drag and drop log files onto it and then click on View icon:&lt;/P&gt;
&lt;P&gt;&lt;IMG style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" height=210 alt=image src="http://blogs.msdn.com/blogfiles/alikl/WindowsLiveWriter/FreeToolsFunnelsqlexpressprofiler_7ED0/image_3.png" width=466 border=0 mce_src="http://blogs.msdn.com/blogfiles/alikl/WindowsLiveWriter/FreeToolsFunnelsqlexpressprofiler_7ED0/image_3.png"&gt; &lt;/P&gt;
&lt;P&gt;You will be presented with nice array of summary and detailed reports:&lt;/P&gt;
&lt;P&gt;&lt;IMG style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" height=158 alt=image src="http://blogs.msdn.com/blogfiles/alikl/WindowsLiveWriter/FreeToolsFunnelsqlexpressprofiler_7ED0/image_6.png" width=466 border=0 mce_src="http://blogs.msdn.com/blogfiles/alikl/WindowsLiveWriter/FreeToolsFunnelsqlexpressprofiler_7ED0/image_6.png"&gt; &lt;/P&gt;
&lt;H3&gt;Usage Scenarios&lt;/H3&gt;
&lt;P&gt;There are few scenarios I can see this tool would be very useful:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Developer analyzes the module she developed during development phase just before submitting for holistic performance testing. &lt;/LI&gt;
&lt;LI&gt;IT support personnel probes production applications to get a quick view on potential performance hotspots. &lt;/LI&gt;
&lt;LI&gt;Performance team analyzes the data after running load/stress tests. &lt;/LI&gt;&lt;/UL&gt;
&lt;H3&gt;My related posts&lt;/H3&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="http://blogs.msdn.com/alikl/archive/2007/11/02/identify-asp-net-web-services-and-wcf-performance-issues-by-examining-iis-logs.aspx" mce_href="http://blogs.msdn.com/alikl/archive/2007/11/02/identify-asp-net-web-services-and-wcf-performance-issues-by-examining-iis-logs.aspx"&gt;Identify ASP.NET, Web Services, And WCF Performance Issues By Examining IIS Logs&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://blogs.msdn.com/alikl/archive/2008/01/21/performance-code-review-tool-practices-checker.aspx" mce_href="http://blogs.msdn.com/alikl/archive/2008/01/21/performance-code-review-tool-practices-checker.aspx"&gt;Performance Code Review Tool – Practices Checker&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://blogs.msdn.com/ace_team/archive/2008/03/11/improve-net-applications-performance-effectively-and-efficiently.aspx" target=_blank mce_href="http://blogs.msdn.com/ace_team/archive/2008/03/11/improve-net-applications-performance-effectively-and-efficiently.aspx"&gt;Improve .Net Applications Performance Effectively And Efficiently&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://blogs.msdn.com/alikl/archive/2007/03/28/performance-testing-for-the-masses.aspx" mce_href="http://blogs.msdn.com/alikl/archive/2007/03/28/performance-testing-for-the-masses.aspx"&gt;Performance Testing For The Masses&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://blogs.msdn.com/alikl/archive/2008/03/09/stress-test-asp-net-web-application-with-free-wcat-tool.aspx" mce_href="http://blogs.msdn.com/alikl/archive/2008/03/09/stress-test-asp-net-web-application-with-free-wcat-tool.aspx"&gt;Stress Test ASP.NET Web Application With Free WCAT Tool&lt;/A&gt; &lt;/LI&gt;&lt;/UL&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8414362" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/alikl/archive/tags/Test+Phase/default.aspx">Test Phase</category><category domain="http://blogs.msdn.com/alikl/archive/tags/Tools/default.aspx">Tools</category><category domain="http://blogs.msdn.com/alikl/archive/tags/Development+Phase/default.aspx">Development Phase</category><category domain="http://blogs.msdn.com/alikl/archive/tags/Performance/default.aspx">Performance</category></item><item><title>patterns &amp; practices WCF Security Guidance Project - live on Codeplex</title><link>http://blogs.msdn.com/alikl/archive/2008/04/02/patterns-practices-wcf-security-guidance-project-live-on-codeplex.aspx</link><pubDate>Wed, 02 Apr 2008 10:53:10 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8350230</guid><dc:creator>alikl</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/alikl/comments/8350230.aspx</comments><wfw:commentRss>http://blogs.msdn.com/alikl/commentrss.aspx?PostID=8350230</wfw:commentRss><wfw:comment>http://blogs.msdn.com/alikl/rsscomments.aspx?PostID=8350230</wfw:comment><description>&lt;p&gt;patterns &amp;amp; practices has recently released &lt;a href="http://www.codeplex.com/WCFSecurity/" target="_blank"&gt;WCF Security Guidance Project&lt;/a&gt;. &lt;a href="http://blogs.msdn.com/jmeier/" target="_blank"&gt;JD&lt;/a&gt;, the program manager behind the effort, &lt;a href="http://blogs.msdn.com/jmeier/archive/2008/03/27/patterns-and-practices-wcf-security-guidance-now-available.aspx" target="_blank"&gt;has been blogging about it&lt;/a&gt; too.It is evolving project but the initial content is fantastic already. It has &lt;a href="http://www.codeplex.com/WCFSecurity/Wiki/View.aspx?title=Application%20Scenarios&amp;amp;referringTitle=Home" target="_blank"&gt;Application Scenarios&lt;/a&gt;, &lt;a href="http://www.codeplex.com/WCFSecurity/Wiki/View.aspx?title=Video%20Index&amp;amp;referringTitle=Home" target="_blank"&gt;Video Index&lt;/a&gt;, but my favorites are How-To's:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://www.codeplex.com/WCFSecurity/Wiki/View.aspx?title=How%20To%20-%20Create%20and%20Install%20Temporary%20Certificates%20in%20WCF%20for%20Message%20Security%20During%20Development&amp;amp;referringTitle=How%20Tos" target="_blank"&gt;How To - Create and Install Temporary Certificates in WCF for Message Security During Development&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://www.codeplex.com/WCFSecurity/Wiki/View.aspx?title=How%20To%20-%20Create%20and%20Install%20Temporary%20Certificates%20in%20WCF%20for%20Transport%20Security%20during%20Development&amp;amp;referringTitle=How%20Tos" target="_blank"&gt;How To - Create and Install Temporary Certificates in WCF for Transport Security during Development&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://www.codeplex.com/WCFSecurity/Wiki/View.aspx?title=How%20To%20-%20Impersonate%20the%20Original%20Caller%20in%20WCF%20calling%20from%20Web%20Application&amp;amp;referringTitle=How%20Tos" target="_blank"&gt;How To - Impersonate the Original Caller in WCF calling from Web Application&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://www.codeplex.com/WCFSecurity/Wiki/View.aspx?title=How%20To%20-%20Impersonate%20the%20Original%20Caller%20in%20WCF%20calling%20from%20Windows%20Forms&amp;amp;referringTitle=How%20Tos" target="_blank"&gt;How To - Impersonate the Original Caller in WCF calling from Windows Forms&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://www.codeplex.com/WCFSecurity/Wiki/View.aspx?title=How%20To%20-%20Use%20netTcpBinding%20with%20Windows%20Authentication%20and%20Transport%20Security%20in%20WCF%20from%20Windows%20Forms&amp;amp;referringTitle=How%20Tos" target="_blank"&gt;How To - Use netTcpBinding with Windows Authentication and Transport Security in WCF from Windows Forms&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://www.codeplex.com/WCFSecurity/Wiki/View.aspx?title=How%20To%20-%20Use%20SQL%20Role%20Provider%20with%20Username%20Authentication%20in%20WCF%20calling%20from%20Windows%20Forms&amp;amp;referringTitle=How%20Tos" target="_blank"&gt;How To - Use SQL Role Provider with Username Authentication in WCF calling from Windows Forms&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://www.codeplex.com/WCFSecurity/Wiki/View.aspx?title=How%20To%20-%20Use%20SQL%20Role%20Provider%20with%20Windows%20Authentication%20in%20WCF%20calling%20from%20Windows%20Forms&amp;amp;referringTitle=How%20Tos" target="_blank"&gt;How To - Use SQL Role Provider with Windows Authentication in WCF calling from Windows Forms&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://www.codeplex.com/WCFSecurity/Wiki/View.aspx?title=How%20To%20-%20Use%20Username%20Authentication%20with%20the%20SQL%20Membership%20Provider%20and%20Message%20Security%20in%20WCF%20from%20Windows%20Forms&amp;amp;referringTitle=How%20Tos" target="_blank"&gt;How To - Use Username Authentication with the SQL Membership Provider and Message Security in WCF from Windows Forms&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://www.codeplex.com/WCFSecurity/Wiki/View.aspx?title=How%20To%20-%20Use%20wsHttpBinding%20with%20Windows%20Authentication%20and%20Message%20Security%20in%20WCF%20from%20Windows%20Forms&amp;amp;referringTitle=How%20Tos" target="_blank"&gt;How To - Use wsHttpBinding with Windows Authentication and Message Security in WCF from Windows Forms&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://www.codeplex.com/WCFSecurity/Wiki/View.aspx?title=How%20To%20-%20Use%20wsHttpBinding%20with%20Windows%20Authentication%20and%20Transport%20Security%20in%20WCF%20calling%20from%20Windows%20Forms&amp;amp;referringTitle=How%20Tos" target="_blank"&gt;How To - Use wsHttpBinding with Windows Authentication and Transport Security in WCF calling from Windows Forms&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Pure love.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8350230" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/alikl/archive/tags/WCF/default.aspx">WCF</category><category domain="http://blogs.msdn.com/alikl/archive/tags/Security/default.aspx">Security</category><category domain="http://blogs.msdn.com/alikl/archive/tags/Development+Phase/default.aspx">Development Phase</category><category domain="http://blogs.msdn.com/alikl/archive/tags/Implementation/default.aspx">Implementation</category><category domain="http://blogs.msdn.com/alikl/archive/tags/Practices/default.aspx">Practices</category></item><item><title>How To Consume WCF Using AJAX Without ASP.NET</title><link>http://blogs.msdn.com/alikl/archive/2008/02/18/how-to-consume-wcf-using-ajax-without-asp-net.aspx</link><pubDate>Mon, 18 Feb 2008 23:35:21 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7779790</guid><dc:creator>alikl</dc:creator><slash:comments>11</slash:comments><comments>http://blogs.msdn.com/alikl/comments/7779790.aspx</comments><wfw:commentRss>http://blogs.msdn.com/alikl/commentrss.aspx?PostID=7779790</wfw:commentRss><wfw:comment>http://blogs.msdn.com/alikl/rsscomments.aspx?PostID=7779790</wfw:comment><description>&lt;p&gt;How to consume WCF services directly from Html client? How to add AJAX-like functionally to application that does not natively support ASP.NET AJAX like classic ASP, ASP.NET 1.1, or PHP?&lt;/p&gt;  &lt;p&gt;WCF that ships with .Net 3.5 provides capability to consume it from any JavaScript enabled client via XML or JSON encoding. There is new built in webHttpBinding that supports either JSON or XML encoded messages to be sent to WCF services.The functionality can dramatically improve performance and user experience.&lt;/p&gt;  &lt;p&gt;This post summarizes the steps to create and consume basic WCF service using webHttpBinding binding. &lt;/p&gt;  &lt;p&gt;&lt;b&gt;Summary of Steps &lt;/b&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;b&gt;Step 1 &amp;#8211; Create WCF service.&lt;/b&gt; &lt;/li&gt;    &lt;li&gt;&lt;b&gt;Step 2 &amp;#8211; Configure WCF end point.&lt;/b&gt; &lt;/li&gt;    &lt;li&gt;&lt;b&gt;Step 3 &amp;#8211; Create JavaScript to invoke WCF service.&lt;/b&gt; &lt;/li&gt;    &lt;li&gt;&lt;b&gt;Step 4 &amp;#8211; Test the solution.&lt;/b&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Next section describes each and every step in details&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Step 1 &amp;#8211; Create WCF service&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Open Visual Studio and create new WCF service project by choosing &amp;quot;WCF Service Application&amp;quot; template under &amp;quot;Web&amp;quot; project type. Name it Wcf2Ajax. Open IService1.cs file and create OperationContract as follows:&lt;/p&gt;  &lt;div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; background-color: #f4f4f4"&gt;   &lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;     &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   1:&lt;/span&gt; [ServiceContract]&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   2:&lt;/span&gt;  &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;interface&lt;/span&gt; IService1&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   3:&lt;/span&gt;  {&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   4:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   5:&lt;/span&gt;      [OperationContract]&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   6:&lt;/span&gt;      &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; Sum2Integers(&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; n1, &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; n2);&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;Open Service1.svc.cs file and add public method that accepts to integers and returns the sum of it. This is the functionality that will be exposed by the WCF service and consumed by JavaScript enabled client:&lt;/p&gt;

&lt;div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; background-color: #f4f4f4"&gt;
  &lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;
    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   1:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; Service1 : IService1&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   2:&lt;/span&gt; {&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   3:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; Sum2Integers(&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; n1, &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; n2)&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   4:&lt;/span&gt;     {&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   5:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; result = num1 + num2;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   6:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   7:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; result.ToString();&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   8:&lt;/span&gt;     }&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;b&gt;Step 2 &amp;#8211; Configure WCF end point&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;Open web.config file and add &amp;lt;binding&amp;gt; section to &amp;lt;system.serviceModel section. Add &amp;lt;webHttpBinding&amp;gt; to binding section:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;system.serviceModel&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
  &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;bindings&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;webHttpBinding&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
      &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;binding &lt;/span&gt;&lt;span style="color: red"&gt;name&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;AjaxBinding&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;/&amp;gt;
    &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;webHttpBinding&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
  &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;bindings&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Add AjaxBehavior to &amp;lt;behaviors&amp;gt; section to support WCF invocation via AJAX:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;behaviors&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
  &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;endpointBehaviors&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;behavior &lt;/span&gt;&lt;span style="color: red"&gt;name&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;AjaxBehavior&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;&amp;gt;
      &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;enableWebScript&lt;/span&gt;&lt;span style="color: blue"&gt;/&amp;gt;
    &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;behavior&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Configure WCF service's endpoint to use newly created binding:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;endpoint &lt;/span&gt;&lt;span style="color: red"&gt;address&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;ajaxEndpoint&lt;/span&gt;&amp;quot; 
          &lt;span style="color: red"&gt;behaviorConfiguration&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;AjaxBehavior&lt;/span&gt;&amp;quot; 
          &lt;span style="color: red"&gt;binding&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;webHttpBinding&lt;/span&gt;&amp;quot; 
          &lt;span style="color: red"&gt;bindingConfiguration&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;AjaxBinding&lt;/span&gt;&amp;quot; 
          &lt;span style="color: red"&gt;contract&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;Wcf2Ajax.IService1&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;&lt;b&gt;Step 3 &amp;#8211; Create JavaScript to invoke WCF service&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;Add Html file to the solution by right clicking on the solution node in solution explorer and choosing &amp;quot;New Item...&amp;quot; and then &amp;quot;HTML Page&amp;quot; template. Name it WCFConsumer.htm. Add few HTML controls - two text boxes to accept two integers, one pure HTML button to trigger WCF call, and &amp;lt;span&amp;gt; element to present the result:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;input &lt;/span&gt;&lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;text&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;id&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;num1&amp;quot; /&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;input &lt;/span&gt;&lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;text&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;id&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;num2&amp;quot; /&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;br&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;input &lt;/span&gt;&lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;button&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;onclick&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;CallWcfAjax()&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;value&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Call WCF via AJAX&amp;quot; /&amp;gt;
&lt;/span&gt;Result &lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;span &lt;/span&gt;&lt;span style="color: red"&gt;id&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;result&amp;quot;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;span&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Add &amp;lt;script&amp;gt; block to &amp;lt;header&amp;gt; section and add JavaSctip that builds HTTP request and wires invocation function to some event, say button click:&lt;/p&gt;

&lt;pre class="code"&gt;    &lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;script &lt;/span&gt;&lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;text/javascript&amp;quot;&amp;gt;
    function &lt;/span&gt;CallWcfAjax()
    {
        &lt;span style="color: blue"&gt;var &lt;/span&gt;xmlHttp = &lt;span style="color: blue"&gt;new &lt;/span&gt;ActiveXObject(&lt;span style="color: #a31515"&gt;&amp;quot;Microsoft.XmlHttp&amp;quot;&lt;/span&gt;);

        &lt;span style="color: blue"&gt;var &lt;/span&gt;url = &lt;span style="color: #a31515"&gt;&amp;quot;Service1.svc/ajaxEndpoint/&amp;quot;&lt;/span&gt;;
        url = url + &lt;span style="color: #a31515"&gt;&amp;quot;Sum2Integers&amp;quot;&lt;/span&gt;;

        &lt;span style="color: blue"&gt;var &lt;/span&gt;body = &lt;span style="color: #a31515"&gt;'{&amp;quot;n1&amp;quot;:'&lt;/span&gt;;
        body = body + document.getElementById(&lt;span style="color: #a31515"&gt;&amp;quot;num1&amp;quot;&lt;/span&gt;).value + &lt;span style="color: #a31515"&gt;',&amp;quot;n2&amp;quot;:'&lt;/span&gt;;
        body = body + document.getElementById(&lt;span style="color: #a31515"&gt;&amp;quot;num2&amp;quot;&lt;/span&gt;).value + &lt;span style="color: #a31515"&gt;'}'&lt;/span&gt;;
          
        &lt;span style="color: green"&gt;// Send the HTTP request
        &lt;/span&gt;xmlHttp.open(&lt;span style="color: #a31515"&gt;&amp;quot;POST&amp;quot;&lt;/span&gt;, url, &lt;span style="color: blue"&gt;true&lt;/span&gt;);
        xmlHttp.setRequestHeader(&lt;span style="color: #a31515"&gt;&amp;quot;Content-type&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;application/json&amp;quot;&lt;/span&gt;);
        xmlHttp.send(body);

        &lt;span style="color: green"&gt;// Create result handler 
        &lt;/span&gt;xmlHttp.onreadystatechange= &lt;span style="color: blue"&gt;function &lt;/span&gt;X()
        {
        
             &lt;span style="color: blue"&gt;if&lt;/span&gt;(xmlHttp.readyState == 4)
             {
                  &lt;span style="color: #a31515"&gt;&lt;font color="#333333"&gt;result.innerText&lt;/font&gt;&lt;/span&gt; = xmlHttp.responseText;
             }
        }
    }
    &lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;script&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;&lt;b&gt;Step 4 &amp;#8211; Test the solution&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;Build the solution and navigate to WCFConsumer.htm. Provide two integers to both text boxes and hit &amp;quot;Call WCF via Ajax&amp;quot; button. You should expect for result similar as depicted below:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/alikl/WindowsLiveWriter/ConsumeWCFServiceByAnyJavaScriptEnabledC_710F/image_2.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="123" alt="image" src="http://blogs.msdn.com/blogfiles/alikl/WindowsLiveWriter/ConsumeWCFServiceByAnyJavaScriptEnabledC_710F/image_thumb.png" width="381" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Parse the result to your needs.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;
&lt;b&gt;My related post&lt;/b&gt;

&lt;p&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href="http://blogs.msdn.com/alikl/archive/2007/12/31/asp-net-ajax-control-toolkit-basic-sample-for-dynamicpopulate-control.aspx"&gt;ASP.NET AJAX Control Toolkit - Basic Sample For DynamicPopulate Control&lt;/a&gt;&lt;/li&gt;

  &lt;li&gt;&lt;a href="http://blogs.msdn.com/alikl/archive/2007/10/03/ajax-security-client-side-validation-is-for-usability-only-not-for-security.aspx"&gt;AJAX Security - Client Side Validation Is For Usability Only, Not For Security&lt;/a&gt;&lt;/li&gt;

  &lt;li&gt;&lt;a href="http://blogs.msdn.com/alikl/archive/2007/07/26/wcf-security-in-intranet-scenario-thoughts-on-cons-and-pros.aspx"&gt;WCF Security In Intranet Scenario : Thoughts On Cons and Pros&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;b&gt;Related resources&lt;/b&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/bb885100.aspx" target="_blank"&gt;Creating WCF AJAX Services without ASP.NET&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Download Visual Studio 2008 project with the sample from my SkyDrive&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;
&lt;iframe style="border-right: #dde5e9 1px solid; padding-right: 0px; border-top: #dde5e9 1px solid; padding-left: 0px; padding-bottom: 0px; margin: 3px; border-left: #dde5e9 1px solid; width: 240px; padding-top: 0px; border-bottom: #dde5e9 1px solid; height: 66px; background-color: #ffffff" marginwidth="0" marginheight="0" src="http://cid-dd25b83e4ca261f7.skydrive.live.com/embedrowdetail.aspx/Visual%20Studio%20Projects/Wcf2Ajax.zip" frameborder="0" scrolling="no"&gt;&lt;/iframe&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=7779790" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/alikl/archive/tags/WCF/default.aspx">WCF</category><category domain="http://blogs.msdn.com/alikl/archive/tags/Development+Phase/default.aspx">Development Phase</category><category domain="http://blogs.msdn.com/alikl/archive/tags/Implementation/default.aspx">Implementation</category><category domain="http://blogs.msdn.com/alikl/archive/tags/Performance/default.aspx">Performance</category><category domain="http://blogs.msdn.com/alikl/archive/tags/Planning+Phase/default.aspx">Planning Phase</category><category domain="http://blogs.msdn.com/alikl/archive/tags/AJAX/default.aspx">AJAX</category></item><item><title>Performance Sin - Using Exceptions To Control Flow</title><link>http://blogs.msdn.com/alikl/archive/2008/02/02/performance-sin-using-exceptions-to-control-flow.aspx</link><pubDate>Sat, 02 Feb 2008 16:19:50 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7390784</guid><dc:creator>alikl</dc:creator><slash:comments>4</slash:comments><comments>http://blogs.msdn.com/alikl/comments/7390784.aspx</comments><wfw:commentRss>http://blogs.msdn.com/alikl/commentrss.aspx?PostID=7390784</wfw:commentRss><wfw:comment>http://blogs.msdn.com/alikl/rsscomments.aspx?PostID=7390784</wfw:comment><description>&lt;p&gt;Want to spot coding anti-patterns from performance perspective without actually looking in the code?&lt;/p&gt;  &lt;p&gt;One of the common performance coding anti-patterns I&amp;#8217;ve noticed lately is using Exception Handling to control program flow. &lt;/p&gt;  &lt;p&gt;&lt;b&gt;The anti-patterns&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Most common anti-pattern is just using exception handling to control flow, in some cases it was even nested exception handling &amp;#8211; that means the exception is thrown anyway.&lt;/p&gt;  &lt;p&gt;In other cases there were empty &amp;#8220;catch&amp;#8221; exception statements. That means that precious cycles .Net consumes to handle the exception spent for nothing.&lt;/p&gt;  &lt;p&gt;The last case was where exception handling was done to catch simple types parsing. That was done on each request. &lt;/p&gt;  &lt;p&gt;&lt;b&gt;How to identify Exception Handling anti-pattern&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;To identify exception handling anti-pattern set &amp;#8220;.NET CLR Exceptions/# of Excepts Thrown&amp;#8221; perf counter. If you see the graph constantly climbs on each request chance are Exception Handling is used to control the flow which is performance anti-pattern:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/alikl/WindowsLiveWriter/PerformanceSinUsingExceptionsToControlFl_D5AC/clip_image002_2.jpg"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="126" alt="clip_image002" src="http://blogs.msdn.com/blogfiles/alikl/WindowsLiveWriter/PerformanceSinUsingExceptionsToControlFl_D5AC/clip_image002_thumb.jpg" width="221" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Look at the relevant source code to spot try/catch blocks. If the source code is not available use &lt;a href="http://www.aisto.com/roeder/dotnet/" target="_blank"&gt;Reflector&lt;/a&gt; to reverse engineer the compiled assemblies into C# sources &amp;#8211; I used it very successfully during my latest performance review.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Best practices&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Do not use exception handling to control the flow. Try to reduce catching exceptions to only most upper component/class. Catching exceptions is expensive from both CPU and memory perspective. &lt;/p&gt;  &lt;p&gt;Use TryParse method instead Parse method to avoid throwing exceptions .&lt;/p&gt;  &lt;p&gt;Use simple &amp;quot;if&amp;quot; statement to check for nulls.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Tools&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Use perfmon to spot the anti-pattern with Exception handling. Run perfmon in command line and add &amp;#8220;.NET CLR Exceptions/# of Excepts Thrown&amp;#8221; counter. Then run few scenarios to see the graph. &lt;/p&gt;  &lt;p&gt;Use Practice Checker for static code analysis. The tool scans the code and reveals excessive usage of exception handling.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;My related posts&lt;/b&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://blogs.msdn.com/alikl/archive/2008/01/21/performance-code-review-tool-practices-checker.aspx"&gt;Performance Code Review Tool &amp;#8211; Practices Checker&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=7390784" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/alikl/archive/tags/Test+Phase/default.aspx">Test Phase</category><category domain="http://blogs.msdn.com/alikl/archive/tags/Tools/default.aspx">Tools</category><category domain="http://blogs.msdn.com/alikl/archive/tags/Development+Phase/default.aspx">Development Phase</category><category domain="http://blogs.msdn.com/alikl/archive/tags/Performance/default.aspx">Performance</category><category domain="http://blogs.msdn.com/alikl/archive/tags/Practices/default.aspx">Practices</category><category domain="http://blogs.msdn.com/alikl/archive/tags/Exception+Handling/default.aspx">Exception Handling</category></item><item><title>Performance Code Review Tool – Practices Checker</title><link>http://blogs.msdn.com/alikl/archive/2008/01/21/performance-code-review-tool-practices-checker.aspx</link><pubDate>Tue, 22 Jan 2008 00:37:50 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7188855</guid><dc:creator>alikl</dc:creator><slash:comments>8</slash:comments><comments>http://blogs.msdn.com/alikl/comments/7188855.aspx</comments><wfw:commentRss>http://blogs.msdn.com/alikl/commentrss.aspx?PostID=7188855</wfw:commentRss><wfw:comment>http://blogs.msdn.com/alikl/rsscomments.aspx?PostID=7188855</wfw:comment><description>&lt;p&gt;Care about performance? Do you write your code with performance in mind? Want little help to spot performance bottlenecks automatically?&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Practices Checker&lt;/strong&gt; to the rescue. &lt;/p&gt;  &lt;p&gt;The goal of the tool is&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&amp;#8220;Help you perform a manual code inspection by analyzing your application for potential coding and configuration settings that do not adhere to the patterns &amp;amp; practices ASP.NET Performance Checklist.&amp;#8221;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;The tool is available as free download &lt;a href="http://www.codeplex.com/PracticesChecker" target="_blank"&gt;here&lt;/a&gt;. Download, install, point to your web application solution folder and hit &amp;#8220;Analyze&amp;#8221; button. You will get the report for potential performance issues and recommendations on how to fix. I am sure you will be surprised by few findings.&lt;/p&gt;  &lt;p&gt;Performance rules:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://www.codeplex.com/PracticesChecker/Wiki/View.aspx?title=Rules%20List&amp;amp;referringTitle=Home" target="_blank"&gt;Rules List&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://www.codeplex.com/PracticesChecker/Wiki/View.aspx?title=Rules%20Checked%20by%20Practices%20Checker&amp;amp;referringTitle=Home" target="_blank"&gt;Rules Checked by Practices Checker&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://www.codeplex.com/PracticesChecker/Wiki/View.aspx?title=Rules%20Not%20Checked%20by%20Practices%20Checker&amp;amp;referringTitle=Home" target="_blank"&gt;Rules Not Checked by Practices Checker&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;b&gt;Case Study&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;I used Practices Checker with one of my recent engagements . The tool spotted in no time web pages where there were 30 loops, enormous amount of serialization issues, and few more.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;My related posts&lt;/b&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://blogs.msdn.com/alikl/archive/2007/08/16/improve-asp-net-performance-by-disabling-viewstate-and-setting-session-as-readonly.aspx"&gt;Improve ASP.NET Performance By Disabling ViewState And Setting Session As ReadOnly&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/alikl/archive/2007/07/06/typed-dataset-potential-performance-and-security-risk.aspx"&gt;Typed DataSet - Potential Performance And Security Risk&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/alikl/archive/2007/03/27/performance-gain-security-risk.aspx"&gt;Performance Gain - Security Risk&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/alikl/archive/2007/03/28/performance-testing-for-the-masses.aspx"&gt;Performance Testing For The Masses&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=7188855" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/alikl/archive/tags/Test+Phase/default.aspx">Test Phase</category><category domain="http://blogs.msdn.com/alikl/archive/tags/Tools/default.aspx">Tools</category><category domain="http://blogs.msdn.com/alikl/archive/tags/Development+Phase/default.aspx">Development Phase</category><category domain="http://blogs.msdn.com/alikl/archive/tags/Performance/default.aspx">Performance</category></item><item><title>How To Keep ASP.NET ViewState On The Server – Revised</title><link>http://blogs.msdn.com/alikl/archive/2008/01/08/how-to-keep-asp-net-viewstate-on-the-server-revised.aspx</link><pubDate>Tue, 08 Jan 2008 23:13:52 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7032152</guid><dc:creator>alikl</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/alikl/comments/7032152.aspx</comments><wfw:commentRss>http://blogs.msdn.com/alikl/commentrss.aspx?PostID=7032152</wfw:commentRss><wfw:comment>http://blogs.msdn.com/alikl/rsscomments.aspx?PostID=7032152</wfw:comment><description>&lt;p&gt;This is a follow up post to &lt;a href="http://blogs.msdn.com/alikl/archive/2008/01/02/basic-sample-how-to-keep-asp-net-viewstate-on-the-server.aspx"&gt;Basic Sample - How To Keep ASP.NET ViewState On The Server&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;ASP.NET 2.0 offers much easier built-in mechanism to save ViewState on the server in Session using &lt;b&gt;SessionPageStatePersister&lt;/b&gt;. Thanks to Russ who pointed me to this feature available in ASP.NET 2.0 via comments in the &lt;a href="http://blogs.msdn.com/alikl/archive/2008/01/02/basic-sample-how-to-keep-asp-net-viewstate-on-the-server.aspx"&gt;previous post&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;The details can be found here &lt;a href="http://msdn2.microsoft.com/en-us/library/aa479403.aspx" target="_blank"&gt;ASP.NET 2.0 Page State Persister&lt;/a&gt; [thanks go to Eddie from our own &lt;a href="http://blogs.msdn.com/ace_team/" target="_blank"&gt;ACE team&lt;/a&gt; for the pointer].&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Short analysis.&lt;/b&gt; Using &lt;i&gt;SessionPageStatePersister&lt;/i&gt; to store VIewState is very convenient as there is no code needed, almost. The down side is that this approach allows storing ViewState only in Session while the approach described in previous post allows routing ViewState to any storage of your choice &amp;#8211; Session, Cache, DB, Web Services &amp;#8211; up to your imagination. This can be important when considering scalability aspects of the solution that kazlak pointed out in the comments for the previous post.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;To commentators. &lt;i&gt;Big-thank-you&lt;/i&gt;&lt;/b&gt; for leaving your insightful comments. Sharing your insights helps me to improve and I am sure it also enriches this blog readers experience &amp;#8211; THANKS! Please comment more and leave your web sites/blogs so I can link back to you and give you full credit to your insights.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=7032152" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/alikl/archive/tags/Development+Phase/default.aspx">Development Phase</category><category domain="http://blogs.msdn.com/alikl/archive/tags/Implementation/default.aspx">Implementation</category><category domain="http://blogs.msdn.com/alikl/archive/tags/Performance/default.aspx">Performance</category></item></channel></rss>