Using async,await for asynchronous operations in ASP. NET WebForm

Source: Internet
Author: User

Summary

Recently, I've been thinking of making some of the time-consuming operations in the project asynchronous. Get a demo on your own to learn. You can register an asynchronous operation in an ASPX page in just a few steps.

Demo

For example, we need to crawl the content of a URL, this time we may have the following method.

usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Linq;usingSystem.Net;usingSystem.Text;usingSystem.Threading.Tasks;usingsystem.web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;namespacewolfy.asyncweb{ Public Partial classIndex:System.Web.UI.Page {protected voidPage_Load (Objectsender, EventArgs e) {            if(!IsPostBack) {                 This. Page.registerasynctask (NewPageAsyncTask (Gethtmlasync)); }        }        Private AsyncTask Gethtmlasync () {stringURL ="http://www.cnblogs.com/"; WebClient Client=NewWebClient (); Client. Encoding=Encoding.UTF8; stringHTML =awaitclient.            Downloadstringtaskasync (URL); stringstrpath = MapPath ("~/html"); if(!directory.exists (strpath))            {directory.createdirectory (strpath); }            stringSavepath = Path.Combine (strpath,"Blog.txt"); using(FileStream fs =NewFileStream (Savepath, FileMode.Create, FileAccess.Write, Fileshare.readwrite)) {                byte[] buffer =Encoding.UTF8.GetBytes (HTML); awaitFs. WriteAsync (Buffer,0, buffer.            Length); }        }    }}

At this time thought is finished, browsing the page when found still a little something.

Locate the corresponding page to add the async attribute.

<%@ page language="C #" autoeventwireup="true" codebehind= " Index.aspx.cs " inherits="Wolfy.AsyncWeb.Index" async="true" % >

Results

Reference

http://blog.csdn.net/youaregoo/article/details/8973387

http://mrbool.com/how-to-create-asynchronous-device-page-in-asp-net-4-5/26022

Http://www.cnblogs.com/dudu/p/aspnet-webform-async.html

Using async,await for asynchronous operations in ASP. NET WebForm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.