Asynchronous event handler using async keywordhttp://forums.asp.net/t/1722876.aspx/1?Asynchronous+event+handler+using+async+keywordThu, 22 Sep 2011 08:02:34 -040017228764607284http://forums.asp.net/p/1722876/4607284.aspx/1?Asynchronous+event+handler+using+async+keywordAsynchronous event handler using async keyword <p>I have written following button click event handler in my code:</p> <pre class="prettyprint">protected async void btnAsyncPostBack_Click(object sender, EventArgs e) { var sum = await GetSumAsync(); lblMessage.Text = string.Format(&quot;Sum = {0}&quot;, sum); }</pre> <p>where GetSumAsync() can be assumed as any CPU intensive operation such as getting a lot of data from DB and applying heavy&nbsp;computations on&nbsp;it etc.</p> <p>It returns a Task&lt;T&gt;.</p> <p>My question is what is the difference between this event handler and a simple event handler. The code written above works fine, however, i was not able to find any noticable difference in its execution.</p> 2011-09-21T14:55:29-04:004607809http://forums.asp.net/p/1722876/4607809.aspx/1?Re+Asynchronous+event+handler+using+async+keywordRe: Asynchronous event handler using async keyword <p>WebForms does not support asynchronous event handlers for callbacks. &nbsp;The only Task-based asynchronous code execution that is supported for WebForms is code that is registered using the Page.RegisterAsyncTask method or code that is written for the Page_PreRenderCompleteAsync event.</p> 2011-09-21T21:03:02-04:004608376http://forums.asp.net/p/1722876/4608376.aspx/1?Re+Asynchronous+event+handler+using+async+keywordRe: Asynchronous event handler using async keyword <p>Con you provide with a sample which implements Task-based asynchronous code execution in web forms.</p> <p>I did not get how i can make use of this in webforms</p> 2011-09-22T07:35:49-04:004608431http://forums.asp.net/p/1722876/4608431.aspx/1?Re+Asynchronous+event+handler+using+async+keywordRe: Asynchronous event handler using async keyword &lt;div&gt; <pre class="prettyprint">protected void btnAsyncPostBack_Click(object sender, EventArgs e) { RegisterAsyncTask(new PageAsyncTask(MyMethodAsync)); } private async Task MyMethodAsync(object sender, EventArgs e, CancellationToken cancellationToken) { var sum = await GetSumAsync(); lblMessage.Text = string.Format(&quot;Sum = {0}&quot;, sum); }</pre> &lt;/div&gt; &lt;div&gt;Note that the postback handler is itself <em>synchronous</em>. It's registering the method MyMethodAsync to be called at the appropriate time in the Page lifecycle.<br> Also remember to mark your page as &lt;%@ Page Async=&quot;true&quot; %&gt;.&lt;/div&gt; 2011-09-22T08:02:34-04:00