I have written following button click event handler in my code:
protected async void btnAsyncPostBack_Click(object sender, EventArgs e)
{
var sum = await GetSumAsync();
lblMessage.Text = string.Format("Sum = {0}", sum);
}
where GetSumAsync() can be assumed as any CPU intensive operation such as getting a lot of data from DB and applying heavy computations on it etc.
It returns a Task<T>.
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.
WebForms does not support asynchronous event handlers for callbacks. 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.
</div> <div>Note that the postback handler is itself synchronous. It's registering the method MyMethodAsync to be called at the appropriate time in the Page lifecycle.
Also remember to mark your page as <%@ Page Async="true" %>.</div>
prabodhm
Participant
958 Points
232 Posts
Asynchronous event handler using async keyword
Sep 21, 2011 02:55 PM|LINK
I have written following button click event handler in my code:
protected async void btnAsyncPostBack_Click(object sender, EventArgs e) { var sum = await GetSumAsync(); lblMessage.Text = string.Format("Sum = {0}", sum); }where GetSumAsync() can be assumed as any CPU intensive operation such as getting a lot of data from DB and applying heavy computations on it etc.
It returns a Task<T>.
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.
levib
Star
7702 Points
1099 Posts
Microsoft
Re: Asynchronous event handler using async keyword
Sep 21, 2011 09:03 PM|LINK
WebForms does not support asynchronous event handlers for callbacks. 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.
prabodhm
Participant
958 Points
232 Posts
Re: Asynchronous event handler using async keyword
Sep 22, 2011 07:35 AM|LINK
Con you provide with a sample which implements Task-based asynchronous code execution in web forms.
I did not get how i can make use of this in webforms
levib
Star
7702 Points
1099 Posts
Microsoft
Re: Asynchronous event handler using async keyword
Sep 22, 2011 08:02 AM|LINK
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("Sum = {0}", sum); }</div> <div>Note that the postback handler is itself synchronous. It's registering the method MyMethodAsync to be called at the appropriate time in the Page lifecycle.Also remember to mark your page as <%@ Page Async="true" %>.</div>