<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://weblogs.asp.net/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>Alessandro Zifiglio : RegisterAsyncTask</title><link>http://weblogs.asp.net/alessandro/archive/tags/RegisterAsyncTask/default.aspx</link><description>Tags: RegisterAsyncTask</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>There is a performance hit associated with using exceptions!</title><link>http://weblogs.asp.net/alessandro/archive/2007/09/30/there-is-a-performance-hit-associated-with-using-exceptions.aspx</link><pubDate>Sun, 30 Sep 2007 15:36:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:4257168</guid><dc:creator>alessandro</dc:creator><author>alessandro</author><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/alessandro/rsscomments.aspx?PostID=4257168</wfw:commentRss><comments>http://weblogs.asp.net/alessandro/archive/2007/09/30/there-is-a-performance-hit-associated-with-using-exceptions.aspx#comments</comments><description>
&lt;p&gt;&amp;nbsp;I am a bit annoyed by the fact that the IDataReader interface does not force an implementation that checks to see if the column you are accessing is indeed null or not(if the column is absent or available in the collection).&lt;br&gt;Basically what will happen when you do eventually try to access a column that does not exist is that the underlying DataReader or DataRow will throw an exception of type "System.ArgumentException" and kindly display one of the most descriptive error messages, "Column 'columnWhatever' does not belong to table".&lt;br&gt;&lt;br&gt;This is fine, thanks, but a try/catch is a bit expensive in a loop where you are pulling values from your underlying DataReader or DataRowCollection, specifically you will start to feel it when you have 1000's of rows. What wouldof been nice is if it were possible to test for null before accessing the value in the column. Ofcourse a test for null will again throw the exception. Could we have used the TryParse methods of the value types we are expecting from the column ? Yes, we can try this, but the exception will be thrown regardless because TryParse wont get a chance to evaluate the expression since the error will be thrown before this happens, not to mention that not all value types have a TryParse method so this is also useless in this case.&lt;br&gt;&lt;br&gt;&lt;strike&gt;So, in what situation could one have a column that does not exist or why does the column exist based on certain conditions ? Well, what if you have a query returning a resultset from the product of an outer join. As you all know already, while an inner join eliminates the rows that do not match with a row from the other table, an outer join, however, will return all rows from at least one of the tables or views mentioned in the FROM clause, as long as those rows meet any WHERE or HAVING search conditions. This means if i have an outer join, the columns returned could be a variable number of columns, depending on my search condition, either columns from both tables or from one are returned.&lt;/strike&gt;&lt;br&gt;&lt;/p&gt;

&lt;p&gt;&lt;b style="color: red;"&gt;Update October, 5th, 2007 &lt;/b&gt;: Joe has caught a mistake in my above example situation of an outer join i have used to build my case. He's right to think that I couldof easily resolved checking the column against dbnull. I have left a small comment below, please do read it. The outer join example i speak about above is not valid anymore so please do ignore it. Thanks joe!&lt;br&gt;&lt;/p&gt;

&lt;p&gt;&lt;br&gt;How bad is writing code like this to workaround the fact that you cant test for the column existance with null :&lt;/p&gt;

&lt;pre&gt;DataTable dt &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; CreateDataTable();&lt;br&gt;            DateTime time1 &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; DateTime.Now;&lt;br&gt;            &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;foreach&lt;/span&gt; (DataRow dr &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;in&lt;/span&gt; dt.Rows)&lt;br&gt;            {&lt;br&gt;                &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;int&lt;/span&gt; column1 &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; (&lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;int&lt;/span&gt;)dr[&lt;span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(237, 237, 237);"&gt;"column1"&lt;/span&gt;];&lt;br&gt;                &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;int&lt;/span&gt; column2 &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; -1;&lt;br&gt;                &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;try&lt;/span&gt;&lt;br&gt;                {&lt;br&gt;                    &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;// not existant column. &lt;br&gt;&lt;/span&gt;                    &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;// Sometimes we have a column2, sometimes we dont.&lt;br&gt;&lt;/span&gt;                    column2 &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; (&lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;int&lt;/span&gt;)dr[&lt;span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(237, 237, 237);"&gt;"column2"&lt;/span&gt;];&lt;br&gt;                }&lt;br&gt;                &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;catch&lt;/span&gt; { }&lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;// eat up the exception. :-)&lt;br&gt;&lt;/span&gt;            }&lt;/pre&gt;
&lt;p&gt;I've always thought that try blocks when used in this manner were effecient and&lt;br&gt;didnt have the overhead and always was of the thinking that only* once an &lt;br&gt;exception is thrown, the process of finding the handler and unwinding the stack can be expensive, so to speak.&lt;br&gt;Yes, we could of done it a bit better like the following example code instead :&lt;/p&gt;

&lt;pre&gt;DataTable dt &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; CreateDataTable();&lt;br&gt;            DateTime time1 &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; DateTime.Now;&lt;br&gt;            &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;bool&lt;/span&gt; hasColumn2 &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; dt.Columns.Contains(&lt;span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(237, 237, 237);"&gt;"column2"&lt;/span&gt;);&lt;br&gt;            &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;foreach&lt;/span&gt; (DataRow dr &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;in&lt;/span&gt; dt.Rows)&lt;br&gt;            {&lt;br&gt;                &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;int&lt;/span&gt; column1 &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; (&lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;int&lt;/span&gt;)dr[&lt;span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(237, 237, 237);"&gt;"column1"&lt;/span&gt;];&lt;br&gt;                &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;int&lt;/span&gt; column2 &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; -1;&lt;br&gt;                &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;if&lt;/span&gt; (hasColumn2)&lt;br&gt;                {&lt;br&gt;                    &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;// not existant column. &lt;br&gt;&lt;/span&gt;                    &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;// Sometimes we have a column2, sometimes we dont.&lt;br&gt;&lt;/span&gt;                    column2 &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; (&lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;int&lt;/span&gt;)dr[&lt;span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(237, 237, 237);"&gt;"column2"&lt;/span&gt;];&lt;br&gt;                }&lt;br&gt;                &lt;br&gt;            }&lt;/pre&gt;Since this is just my theory, I've decided to experiment and findout and here, &lt;br&gt;man oh man, what a surprise. It's much much better to search if the column &lt;br&gt;exists before looping through the recordset than actually using a try or a &lt;br&gt;try/catch block! Not only was the try block an overhead, but a try without a &lt;br&gt;catch block showed to have more overhead than a complete try/catch block! &lt;br&gt;wooohoo wouldof guessed :-)&lt;br&gt;&lt;br&gt;And i picked this up from one of microsofts application that in my opinion was &lt;br&gt;one of the most advanced shared source applications written by ms and the &lt;br&gt;community at large! Well, I guess not everybody is perfect and they probably &lt;br&gt;werent returning 1000's of rows so on a 2 - 10 row basis this really didnt effect &lt;br&gt;or show any signs of performance degradation.&lt;br&gt;&lt;br&gt;The moral of this traggic findings for me is simple. "There is a performance &lt;br&gt;hit associated with using exceptions."&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;b&gt;Documentation on msdn states :&lt;br&gt;Exceptions&lt;br&gt;&lt;br&gt;&lt;/b&gt;&lt;b&gt;&lt;/b&gt;
&lt;blockquote&gt;&lt;i&gt;Some restrictions are introduced when using try blocks that inhibit the&lt;br&gt;compiler from performing certain optimizations. On x86 platforms there is&lt;br&gt;additional performance degradation from try blocks due to additional state&lt;br&gt;information that must be generated during code execution. On the 64-bit&lt;br&gt;platforms, try blocks do not degrade performance as much, but once an exception&lt;br&gt;is thrown, the process of finding the handler and unwinding the stack can be&lt;br&gt;expensive.&lt;br&gt;Therefore, it is recommended to avoid introducing try/catch blocks&lt;br&gt;into code that does not really need it. If you must use exceptions, use&lt;br&gt;synchronous exceptions if possible. For more information, see Structured&lt;br&gt;Exception Handling.&lt;br&gt;Lastly, throw exceptions for exceptional cases only.&lt;br&gt;Using exceptions for general control flow will likely make performance&lt;br&gt;suffer.&lt;br&gt;&lt;/i&gt;&lt;/blockquote&gt;

&lt;p&gt;&lt;br&gt;For simplicity, i have chosen to use a DataTable and access DataRows Vs using a DataReader. ASP.NET version 2.0 allows you to register multiple tasks to a page and run them asynchronously prior to rendering the page. I have always wanted to test this feature out and never found an occassion. This experiment has given me that opportunity. Below is the code i have used for testing performance and below it you will find the results.&lt;/p&gt;

&lt;pre&gt;&amp;lt;%@ Page Language=&lt;span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(237, 237, 237);"&gt;"C#"&lt;/span&gt; Async=&lt;span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(237, 237, 237);"&gt;"false"&lt;/span&gt; AsyncTimeout=&lt;span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(237, 237, 237);"&gt;"2000"&lt;/span&gt; &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;%&lt;/span&gt;&amp;gt;&lt;br&gt;&lt;br&gt;&amp;lt;%@ Import Namespace=&lt;span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(237, 237, 237);"&gt;"System.Data"&lt;/span&gt; &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;%&lt;/span&gt;&amp;gt;&lt;br&gt;&amp;lt;%@ Import Namespace=&lt;span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(237, 237, 237);"&gt;"System.Threading"&lt;/span&gt; &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;%&lt;/span&gt;&amp;gt;&lt;br&gt;&amp;lt;%@ Import Namespace=&lt;span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(237, 237, 237);"&gt;"System.Collections.Generic"&lt;/span&gt; &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;%&lt;/span&gt;&amp;gt;&lt;br&gt;&amp;lt;!DOCTYPE html PUBLIC &lt;span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(237, 237, 237);"&gt;"-//W3C//DTD XHTML 1.0 Transitional//EN"&lt;/span&gt; &lt;span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(237, 237, 237);"&gt;"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&lt;/span&gt;&amp;gt;&lt;br&gt;&lt;br&gt;&amp;lt;script runat=&lt;span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(237, 237, 237);"&gt;"server"&lt;/span&gt;&amp;gt;&lt;br&gt;&lt;br&gt;    &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;protected&lt;/span&gt; &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;void&lt;/span&gt; Page_Load(object sender, EventArgs e)&lt;br&gt;    {&lt;br&gt;        &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;// Define the asynchronuous task&lt;br&gt;&lt;/span&gt;        SlowTask slowTask1 &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;new&lt;/span&gt; SlowTask(TaskType.TryBlock);&lt;br&gt;        SlowTask slowTask2 &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;new&lt;/span&gt; SlowTask(TaskType.TryCatchBlock);&lt;br&gt;        SlowTask slowTask3 &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;new&lt;/span&gt; SlowTask(TaskType.VerifyDataTableColumn);&lt;br&gt;        SlowTask slowTask4 &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;new&lt;/span&gt; SlowTask(TaskType.NormalDataTable);&lt;br&gt;        SlowTask slowTask5 &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;new&lt;/span&gt; SlowTask(TaskType.TryBlock2);&lt;br&gt;        SlowTask slowTask6 &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;new&lt;/span&gt; SlowTask(TaskType.TryCatchBlock2);&lt;br&gt;        &lt;br&gt;        &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;bool&lt;/span&gt; executeInParallel &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;false&lt;/span&gt;;&lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;// lets make them execute sequentially, one after the other&lt;br&gt;&lt;/span&gt;&lt;br&gt;        PageAsyncTask asyncTask1 &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;new&lt;/span&gt; PageAsyncTask(slowTask1.OnBegin, slowTask1.OnEnd, slowTask1.OnTimeout, &lt;span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(237, 237, 237);"&gt;"With Try block : "&lt;/span&gt;, executeInParallel);&lt;br&gt;        PageAsyncTask asyncTask2 &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;new&lt;/span&gt; PageAsyncTask(slowTask2.OnBegin, slowTask2.OnEnd, slowTask2.OnTimeout, &lt;span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(237, 237, 237);"&gt;"With Try/Catch block : "&lt;/span&gt;, executeInParallel);&lt;br&gt;        PageAsyncTask asyncTask3 &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;new&lt;/span&gt; PageAsyncTask(slowTask3.OnBegin, slowTask3.OnEnd, slowTask3.OnTimeout, &lt;span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(237, 237, 237);"&gt;"Verifying column existance : "&lt;/span&gt;, executeInParallel);&lt;br&gt;        PageAsyncTask asyncTask4 &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;new&lt;/span&gt; PageAsyncTask(slowTask4.OnBegin, slowTask4.OnEnd, slowTask4.OnTimeout, &lt;span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(237, 237, 237);"&gt;"Without any Try/Catch blocks : "&lt;/span&gt;, executeInParallel);&lt;br&gt;        PageAsyncTask asyncTask5 &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;new&lt;/span&gt; PageAsyncTask(slowTask5.OnBegin, slowTask5.OnEnd, slowTask5.OnTimeout, &lt;span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(237, 237, 237);"&gt;"With Try block2 : "&lt;/span&gt;, executeInParallel);&lt;br&gt;        PageAsyncTask asyncTask6 &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;new&lt;/span&gt; PageAsyncTask(slowTask6.OnBegin, slowTask6.OnEnd, slowTask6.OnTimeout, &lt;span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(237, 237, 237);"&gt;"With Try/Catch block2 : "&lt;/span&gt;, executeInParallel);&lt;br&gt;&lt;br&gt;        &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;// Register the asynchronous task.&lt;br&gt;&lt;/span&gt;&lt;br&gt;        Page.RegisterAsyncTask(asyncTask1);&lt;br&gt;        Page.RegisterAsyncTask(asyncTask2);&lt;br&gt;        Page.RegisterAsyncTask(asyncTask3);&lt;br&gt;        &lt;br&gt;        Page.RegisterAsyncTask(asyncTask4);&lt;br&gt;        Page.RegisterAsyncTask(asyncTask5);&lt;br&gt;        Page.RegisterAsyncTask(asyncTask6);&lt;br&gt;        &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;// Execute the register asynchronous task.&lt;br&gt;&lt;/span&gt;        Page.ExecuteRegisteredAsyncTasks();&lt;br&gt;        TaskMessage.InnerHtml &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; slowTask1.GetAsyncTaskProgress() &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;+&lt;/span&gt; &lt;span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(237, 237, 237);"&gt;"&amp;lt;br /&amp;gt;"&lt;/span&gt; &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;+&lt;/span&gt;&lt;br&gt;                                slowTask2.GetAsyncTaskProgress() &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;+&lt;/span&gt; &lt;span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(237, 237, 237);"&gt;"&amp;lt;br /&amp;gt;"&lt;/span&gt; &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;+&lt;/span&gt;&lt;br&gt;                                slowTask3.GetAsyncTaskProgress() &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;+&lt;/span&gt; &lt;span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(237, 237, 237);"&gt;"&amp;lt;br /&amp;gt;"&lt;/span&gt; &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;+&lt;/span&gt;&lt;br&gt;                                slowTask4.GetAsyncTaskProgress() &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;+&lt;/span&gt; &lt;span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(237, 237, 237);"&gt;"&amp;lt;br /&amp;gt;"&lt;/span&gt; &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;+&lt;/span&gt;&lt;br&gt;                                slowTask5.GetAsyncTaskProgress() &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;+&lt;/span&gt; &lt;span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(237, 237, 237);"&gt;"&amp;lt;br /&amp;gt;"&lt;/span&gt; &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;+&lt;/span&gt;&lt;br&gt;                                slowTask6.GetAsyncTaskProgress();  &lt;br&gt;&lt;br&gt;&lt;br&gt;    }&lt;br&gt;&lt;br&gt;    &lt;br&gt;&lt;br&gt;&lt;br&gt;&amp;lt;/script&amp;gt;&lt;br&gt;&lt;br&gt;&amp;lt;html xmlns=&lt;span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(237, 237, 237);"&gt;"http://www.w3.org/1999/xhtml"&lt;/span&gt; &amp;gt;&lt;br&gt;&amp;lt;head runat=&lt;span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(237, 237, 237);"&gt;"server"&lt;/span&gt;&amp;gt;&lt;br&gt;    &amp;lt;title&amp;gt;Untitled Page&amp;lt;/title&amp;gt;&lt;br&gt;&amp;lt;/head&amp;gt;&lt;br&gt;&lt;br&gt;&amp;lt;body&amp;gt;&lt;br&gt;    &amp;lt;form id=&lt;span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(237, 237, 237);"&gt;"form1"&lt;/span&gt; runat=&lt;span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(237, 237, 237);"&gt;"server"&lt;/span&gt;&amp;gt;&lt;br&gt;    &amp;lt;div&amp;gt;&lt;br&gt;      &amp;lt;span id=&lt;span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(237, 237, 237);"&gt;"TaskMessage"&lt;/span&gt; runat=server&amp;gt;&lt;br&gt;&lt;br&gt;      &amp;lt;/span&amp;gt;&lt;br&gt;    &amp;lt;/div&amp;gt;&lt;br&gt;    &amp;lt;/form&amp;gt;&lt;br&gt;&amp;lt;/body&amp;gt;&lt;br&gt;&amp;lt;/html&amp;gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;using&lt;/span&gt; System;&lt;br&gt;&lt;br&gt;&lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;using&lt;/span&gt; System.Web;&lt;br&gt;&lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;using&lt;/span&gt; System.Web.UI;&lt;br&gt;&lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;using&lt;/span&gt; System.Threading;&lt;br&gt;&lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;using&lt;/span&gt; System.Data;&lt;br&gt;&lt;br&gt;    &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;public&lt;/span&gt; enum TaskType&lt;br&gt;    {&lt;br&gt;        TryBlock,&lt;br&gt;        TryCatchBlock,&lt;br&gt;        VerifyDataTableColumn,&lt;br&gt;        TryBlock2,&lt;br&gt;        TryCatchBlock2,&lt;br&gt;        NormalDataTable&lt;br&gt;    }&lt;br&gt;&lt;br&gt;    &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;public&lt;/span&gt; &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;class&lt;/span&gt; SlowTask&lt;br&gt;    {&lt;br&gt;        &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;public&lt;/span&gt; SlowTask(TaskType tt)&lt;br&gt;        {&lt;br&gt;            taskValue &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; tt;&lt;br&gt;        }&lt;br&gt;        &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;private&lt;/span&gt; String _taskprogress;&lt;br&gt;        &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;private&lt;/span&gt; AsyncTaskDelegate _dlgt;&lt;br&gt;&lt;br&gt;        &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;private&lt;/span&gt; TaskType taskValue;&lt;br&gt;&lt;br&gt;        &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;public&lt;/span&gt; TaskType Task&lt;br&gt;        {&lt;br&gt;            &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;get&lt;/span&gt; { &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;return&lt;/span&gt; taskValue; }&lt;br&gt;            &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;set&lt;/span&gt; { taskValue &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; value; }&lt;br&gt;        }&lt;br&gt;        &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;// Create delegate.&lt;br&gt;&lt;/span&gt;&lt;br&gt;        &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;protected&lt;/span&gt; delegate &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;void&lt;/span&gt; AsyncTaskDelegate(TaskType tt);&lt;br&gt;&lt;br&gt;        &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;public&lt;/span&gt; String GetAsyncTaskProgress()&lt;br&gt;        {&lt;br&gt;            &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;return&lt;/span&gt; _taskprogress;&lt;br&gt;        }&lt;br&gt;&lt;br&gt;        &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;public&lt;/span&gt; &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;void&lt;/span&gt; ExecuteAsyncTask(TaskType tt)&lt;br&gt;        {&lt;br&gt;            &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;// Introduce an artificial delay to simulate a delayed &lt;br&gt;&lt;/span&gt;&lt;br&gt;            &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;// asynchronous task.&lt;br&gt;&lt;/span&gt;            &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;switch&lt;/span&gt; (tt)&lt;br&gt;            {&lt;br&gt;                &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;case&lt;/span&gt; TaskType.TryBlock:&lt;br&gt;                    TryBlock();&lt;br&gt;                    &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;break&lt;/span&gt;;&lt;br&gt;                &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;case&lt;/span&gt; TaskType.TryCatchBlock:&lt;br&gt;                    TryCatchBlock();&lt;br&gt;                    &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;break&lt;/span&gt;;&lt;br&gt;                &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;case&lt;/span&gt; TaskType.VerifyDataTableColumn:&lt;br&gt;                    VerifyDataTableColumn();&lt;br&gt;                    &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;break&lt;/span&gt;;&lt;br&gt;                &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;case&lt;/span&gt; TaskType.TryBlock2:&lt;br&gt;                    TryBlock2();&lt;br&gt;                    &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;break&lt;/span&gt;;&lt;br&gt;                &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;case&lt;/span&gt; TaskType.TryCatchBlock2:&lt;br&gt;                    TryCatchBlock2();&lt;br&gt;                    &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;break&lt;/span&gt;;&lt;br&gt;                &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;case&lt;/span&gt; TaskType.NormalDataTable:&lt;br&gt;                    NormalDataTable();&lt;br&gt;                    &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;break&lt;/span&gt;;&lt;br&gt;            }&lt;br&gt;        }&lt;br&gt;&lt;br&gt;        &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;// Define the method that will get called to&lt;br&gt;&lt;/span&gt;&lt;br&gt;        &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;// start the asynchronous task.&lt;br&gt;&lt;/span&gt;        &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;public&lt;/span&gt; IAsyncResult OnBegin(object sender, EventArgs e,&lt;br&gt;            AsyncCallback cb, object extraData)&lt;br&gt;        {&lt;br&gt;            _taskprogress &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;string&lt;/span&gt;.Format(&lt;span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(237, 237, 237);"&gt;"&amp;lt;br /&amp;gt;{0} started at: {1}"&lt;/span&gt; , extraData.ToString(), DateTime.Now);&lt;br&gt;&lt;br&gt;            _dlgt &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;new&lt;/span&gt; AsyncTaskDelegate(ExecuteAsyncTask);&lt;br&gt;            extraData &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; DateTime.Now;&lt;br&gt;            IAsyncResult result &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; _dlgt.BeginInvoke(Task, cb, extraData);&lt;br&gt;&lt;br&gt;            &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;return&lt;/span&gt; result;&lt;br&gt;        }&lt;br&gt;&lt;br&gt;        &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;// Define the method that will get called when&lt;br&gt;&lt;/span&gt;&lt;br&gt;        &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;// the asynchronous task is ended.&lt;br&gt;&lt;/span&gt;        &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;public&lt;/span&gt; &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;void&lt;/span&gt; OnEnd(IAsyncResult ar)&lt;br&gt;        {&lt;br&gt;            TimeSpan ts &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; DateTime.Now.Subtract((DateTime)ar.AsyncState);&lt;br&gt;            _taskprogress += &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;string&lt;/span&gt;.Format(&lt;span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(237, 237, 237);"&gt;"&amp;lt;br /&amp;gt;Operation took exactly : {1} milliseconds"&lt;/span&gt;, ar.AsyncState.ToString(),  ts.TotalMilliseconds);&lt;br&gt;            _dlgt.EndInvoke(ar);&lt;br&gt;        }&lt;br&gt;&lt;br&gt;        &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;// Define the method that will get called if the task&lt;br&gt;&lt;/span&gt;        &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;// is not completed within the asynchronous timeout interval.&lt;br&gt;&lt;/span&gt;&lt;br&gt;        &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;public&lt;/span&gt; &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;void&lt;/span&gt; OnTimeout(IAsyncResult ar)&lt;br&gt;        {&lt;br&gt;            _taskprogress += &lt;span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(237, 237, 237);"&gt;"AsyncTask failed to complete "&lt;/span&gt; &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;+&lt;/span&gt;&lt;br&gt;                &lt;span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(237, 237, 237);"&gt;"because it exceeded the AsyncTimeout parameter."&lt;/span&gt;;&lt;br&gt;        }&lt;br&gt;&lt;br&gt;        &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;/// &amp;lt;summary&amp;gt;&lt;br&gt;&lt;/span&gt;        &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;/// Operation took exactly : 4562.5 milliseconds&lt;br&gt;&lt;/span&gt;&lt;br&gt;        &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;/// This try block that eats up the exception took longer&lt;br&gt;&lt;/span&gt;        &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;/// than the try/catch block that was actually catching the exception!&lt;br&gt;&lt;/span&gt;        &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;/// &amp;lt;/summary&amp;gt;&lt;br&gt;&lt;/span&gt;        &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;private&lt;/span&gt; &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;void&lt;/span&gt; TryBlock()&lt;br&gt;        {&lt;br&gt;            DataTable dt &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; CreateDataTable();&lt;br&gt;            DateTime time1 &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; DateTime.Now;&lt;br&gt;            &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;foreach&lt;/span&gt; (DataRow dr &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;in&lt;/span&gt; dt.Rows)&lt;br&gt;            {&lt;br&gt;                &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;int&lt;/span&gt; column1 &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; (&lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;int&lt;/span&gt;)dr[&lt;span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(237, 237, 237);"&gt;"column1"&lt;/span&gt;];&lt;br&gt;                &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;int&lt;/span&gt; column2 &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; -1;&lt;br&gt;                &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;try&lt;/span&gt;&lt;br&gt;&lt;br&gt;                {&lt;br&gt;                    &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;// not existant column. &lt;br&gt;&lt;/span&gt;                    &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;// Sometimes we have a column2, sometimes we dont.&lt;br&gt;&lt;/span&gt;                    column2 &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; (&lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;int&lt;/span&gt;)dr[&lt;span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(237, 237, 237);"&gt;"column2"&lt;/span&gt;];&lt;br&gt;                }&lt;br&gt;                &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;catch&lt;/span&gt; { }&lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;// eat up the exception. :-)&lt;br&gt;&lt;/span&gt;&lt;br&gt;            }&lt;br&gt;        }&lt;br&gt;&lt;br&gt;        &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;/// &amp;lt;summary&amp;gt;&lt;br&gt;&lt;/span&gt;        &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;/// Operation took exactly : 4468.75 milliseconds&lt;br&gt;&lt;/span&gt;        &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;/// This is a try/catch block that catches the &lt;br&gt;&lt;/span&gt;        &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;/// exception being thrown by the DataRow,&lt;br&gt;&lt;/span&gt;        &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;/// when the column is not found!&lt;br&gt;&lt;/span&gt;&lt;br&gt;        &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;/// &amp;lt;/summary&amp;gt;&lt;br&gt;&lt;/span&gt;        &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;private&lt;/span&gt; &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;void&lt;/span&gt; TryCatchBlock()&lt;br&gt;        {&lt;br&gt;            DataTable dt &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; CreateDataTable();&lt;br&gt;            DateTime time1 &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; DateTime.Now;&lt;br&gt;            &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;foreach&lt;/span&gt; (DataRow dr &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;in&lt;/span&gt; dt.Rows)&lt;br&gt;            {&lt;br&gt;                &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;int&lt;/span&gt; column1 &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; (&lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;int&lt;/span&gt;)dr[&lt;span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(237, 237, 237);"&gt;"column1"&lt;/span&gt;];&lt;br&gt;                &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;int&lt;/span&gt; column2 &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; -1;&lt;br&gt;                &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;try&lt;/span&gt;&lt;br&gt;&lt;br&gt;                {&lt;br&gt;                    &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;// not existant column. &lt;br&gt;&lt;/span&gt;                    &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;// Sometimes we have a column2, sometimes we dont.&lt;br&gt;&lt;/span&gt;                    column2 &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; (&lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;int&lt;/span&gt;)dr[&lt;span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(237, 237, 237);"&gt;"column2"&lt;/span&gt;];&lt;br&gt;                }&lt;br&gt;                &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;catch&lt;/span&gt; (System.ArgumentException) { }&lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;// System.ArgumentException :-)&lt;br&gt;&lt;/span&gt;&lt;br&gt;            }&lt;br&gt;        }&lt;br&gt;&lt;br&gt;        &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;/// &amp;lt;summary&amp;gt;&lt;br&gt;&lt;/span&gt;        &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;/// Operation took exactly : 421.875 milliseconds&lt;br&gt;&lt;/span&gt;        &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;/// This is the fastest, when you have a column&lt;br&gt;&lt;/span&gt;        &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;/// that might not exist, and need to check for the &lt;br&gt;&lt;/span&gt;        &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;/// existance of the column, before accesing it.&lt;br&gt;&lt;/span&gt;&lt;br&gt;        &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;/// This can be done once outside the loop. &lt;br&gt;&lt;/span&gt;        &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;/// And is more efficient than the above&lt;br&gt;&lt;/span&gt;        &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;/// two methods, simply because there is&lt;br&gt;&lt;/span&gt;        &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;/// no exception being thrown here.&lt;br&gt;&lt;/span&gt;        &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;/// &amp;lt;/summary&amp;gt;&lt;br&gt;&lt;/span&gt;        &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;private&lt;/span&gt; &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;void&lt;/span&gt; VerifyDataTableColumn()&lt;br&gt;        {&lt;br&gt;            DataTable dt &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; CreateDataTable();&lt;br&gt;            DateTime time1 &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; DateTime.Now;&lt;br&gt;            &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;bool&lt;/span&gt; hasColumn2 &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; dt.Columns.Contains(&lt;span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(237, 237, 237);"&gt;"column2"&lt;/span&gt;);&lt;br&gt;            &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;foreach&lt;/span&gt; (DataRow dr &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;in&lt;/span&gt; dt.Rows)&lt;br&gt;            {&lt;br&gt;                &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;int&lt;/span&gt; column1 &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; (&lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;int&lt;/span&gt;)dr[&lt;span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(237, 237, 237);"&gt;"column1"&lt;/span&gt;];&lt;br&gt;                &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;int&lt;/span&gt; column2 &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; -1;&lt;br&gt;                &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;if&lt;/span&gt; (hasColumn2)&lt;br&gt;                {&lt;br&gt;                    &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;// not existant column. &lt;br&gt;&lt;/span&gt;&lt;br&gt;                    &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;// Sometimes we have a column2, sometimes we dont.&lt;br&gt;&lt;/span&gt;                    column2 &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; (&lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;int&lt;/span&gt;)dr[&lt;span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(237, 237, 237);"&gt;"column2"&lt;/span&gt;];&lt;br&gt;                }&lt;br&gt;                &lt;br&gt;            }&lt;br&gt;        }&lt;br&gt;&lt;br&gt;        &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;/// &amp;lt;summary&amp;gt;&lt;br&gt;&lt;/span&gt;        &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;/// Operation took exactly : 375 milliseconds&lt;br&gt;&lt;/span&gt;        &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;/// This is the fastest, since we dont have&lt;br&gt;&lt;/span&gt;&lt;br&gt;        &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;/// columns that might not exist and do not&lt;br&gt;&lt;/span&gt;        &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;/// check for this either! The differences btw&lt;br&gt;&lt;/span&gt;        &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;/// this and the above are superficial.&lt;br&gt;&lt;/span&gt;        &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;/// &amp;lt;/summary&amp;gt;&lt;br&gt;&lt;/span&gt;        &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;private&lt;/span&gt; &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;void&lt;/span&gt; NormalDataTable()&lt;br&gt;        {&lt;br&gt;            DataTable dt &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; CreateDataTable();&lt;br&gt;            DateTime time1 &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; DateTime.Now;&lt;br&gt;            &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;foreach&lt;/span&gt; (DataRow dr &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;in&lt;/span&gt; dt.Rows)&lt;br&gt;            {&lt;br&gt;                &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;int&lt;/span&gt; column1 &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; (&lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;int&lt;/span&gt;)dr[&lt;span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(237, 237, 237);"&gt;"column1"&lt;/span&gt;];&lt;br&gt;            }&lt;br&gt;        }&lt;br&gt;        DataTable CreateDataTable()&lt;br&gt;        {&lt;br&gt;            DataTable dt &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;new&lt;/span&gt; DataTable();&lt;br&gt;            DataRow dr;&lt;br&gt;&lt;br&gt;            dt.Columns.Add(&lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;new&lt;/span&gt; DataColumn(&lt;span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(237, 237, 237);"&gt;"column1"&lt;/span&gt;, typeof(Int32)));&lt;br&gt;            &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;for&lt;/span&gt; (&lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;int&lt;/span&gt; i &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; 0; i &amp;lt; 100000; i++)&lt;br&gt;            {&lt;br&gt;                dr &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; dt.NewRow();&lt;br&gt;                dr[0] &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; i;&lt;br&gt;                dt.Rows.Add(dr);&lt;br&gt;            }&lt;br&gt;&lt;br&gt;            &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;return&lt;/span&gt; dt;&lt;br&gt;        }&lt;br&gt;&lt;br&gt;        &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;/// &amp;lt;summary&amp;gt;&lt;br&gt;&lt;/span&gt;&lt;br&gt;        &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;/// Operation took exactly : 6390.625 milliseconds&lt;br&gt;&lt;/span&gt;        &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;/// This is just a generic try without the catch.&lt;br&gt;&lt;/span&gt;        &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;/// &amp;lt;/summary&amp;gt;&lt;br&gt;&lt;/span&gt;        &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;public&lt;/span&gt; &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;void&lt;/span&gt; TryBlock2()&lt;br&gt;        {&lt;br&gt;            &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;for&lt;/span&gt; (&lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;int&lt;/span&gt; i &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; 0; i &amp;lt; 100000; i++)&lt;br&gt;            {&lt;br&gt;                object x &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; &lt;span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(237, 237, 237);"&gt;"not an int"&lt;/span&gt;;&lt;br&gt;                &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;int&lt;/span&gt; a &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; 0;&lt;br&gt;                &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;try&lt;/span&gt;&lt;br&gt;&lt;br&gt;                {&lt;br&gt;                    a &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; (&lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;int&lt;/span&gt;)x;&lt;br&gt;                }&lt;br&gt;                &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;catch&lt;/span&gt; { }&lt;br&gt;            }&lt;br&gt;        }&lt;br&gt;&lt;br&gt;        &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;/// &amp;lt;summary&amp;gt;&lt;br&gt;&lt;/span&gt;        &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;/// Operation took exactly : 6250 milliseconds&lt;br&gt;&lt;/span&gt;        &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;/// This is a simple try/catch that is actually catching an&lt;br&gt;&lt;/span&gt;        &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;/// exception. It executes faster than the above that &lt;br&gt;&lt;/span&gt;&lt;br&gt;        &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;/// eats up the exception! What a revelation :-)&lt;br&gt;&lt;/span&gt;        &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;/// &amp;lt;/summary&amp;gt;&lt;br&gt;&lt;/span&gt;        &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;public&lt;/span&gt; &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;void&lt;/span&gt; TryCatchBlock2()&lt;br&gt;        {&lt;br&gt;            &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;for&lt;/span&gt; (&lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;int&lt;/span&gt; i &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; 0; i &amp;lt; 100000; i++)&lt;br&gt;            {&lt;br&gt;                object x &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; &lt;span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(237, 237, 237);"&gt;"not an int"&lt;/span&gt;;&lt;br&gt;                &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;int&lt;/span&gt; a &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; 0;&lt;br&gt;                &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;try&lt;/span&gt;&lt;br&gt;&lt;br&gt;                {&lt;br&gt;                    a &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: white;"&gt;=&lt;/span&gt; (&lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;int&lt;/span&gt;)x;&lt;br&gt;                }&lt;br&gt;                &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;catch&lt;/span&gt; (System.InvalidCastException e)&lt;br&gt;                {&lt;br&gt;                   &lt;span style="font-size: 11px; color: green; font-family: Courier New; background-color: white;"&gt;//&lt;br&gt;&lt;/span&gt;                }&lt;br&gt;            }&lt;br&gt;        }&lt;br&gt;    }&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;------------------------------&lt;br&gt;These are my results on a 32bit AMD Opteron processor and 1.5gb ram&lt;br&gt;-------------------------------&lt;br&gt;&lt;br&gt;With Try block &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;in&lt;/span&gt; DataRowCollection : started at: 12/12/2006 8:02:55 AM&lt;br&gt;Operation took exactly : 4562.5 milliseconds&lt;br&gt;&lt;br&gt;With Try/Catch block &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;in&lt;/span&gt; DataRowCollection: started at: 12/12/2006 8:02:59 AM&lt;br&gt;Operation took exactly : 4468.75 milliseconds&lt;br&gt;&lt;br&gt;Verifying column existance &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;in&lt;/span&gt; DataRowCollection: started at: 12/12/2006 8:03:04 AM&lt;br&gt;Operation took exactly : 421.875 milliseconds&lt;br&gt;&lt;br&gt;Without any Try/Catch blocks &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: white;"&gt;in&lt;/span&gt; DataRowCollection: started at: 12/12/2006 8:03:04 AM&lt;br&gt;Operation took exactly : 375 milliseconds&lt;br&gt;&lt;br&gt;Simple Try block2 : started at: 12/12/2006 8:03:05 AM&lt;br&gt;Operation took exactly : 6390.625 milliseconds&lt;br&gt;&lt;br&gt;Simple Try/Catch block2 : started at: 12/12/2006 8:03:11 AM&lt;br&gt;Operation took exactly : 6250 milliseconds&lt;/pre&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=4257168" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/alessandro/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/alessandro/archive/tags/ExecuteRegisteredAsyncTasks/default.aspx">ExecuteRegisteredAsyncTasks</category><category domain="http://weblogs.asp.net/alessandro/archive/tags/RegisterAsyncTask/default.aspx">RegisterAsyncTask</category><category domain="http://weblogs.asp.net/alessandro/archive/tags/Exception+handling/default.aspx">Exception handling</category></item></channel></rss>