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