If parallelism can greatly improve performance, but in our use, it is not all parallel and wired line operation, so we need to do parallel operation in the business logic layer of the Protection exhibition:
The data access layer is unchanged or the same as before:
Public classUserdal { PublicUser GetUser () {User User=NewUser (); User. Name="N1"; User. Address="A1"; returnuser; } PublicList<user>getuserlist () {List<User> list =NewList<user>(); List. ADD (NewUser {Name ="N11", Address ="A11" }); List. ADD (NewUser {Name ="N12", Address ="A12" }); returnlist; } }
The business logic layer needs to have async in the protection exhibition, as follows:
Public classuserbll {userdal dal=NewUserdal (); Public AsyncTask<user>Getuserasync () {return awaitTask.run (() = {returndal. GetUser (); }); } PublicUser GetUser () {returndal. GetUser (); } Public AsyncTask<list<user>>Getuserlistasync () {return awaitTask.run (() = {returndal. Getuserlist (); }); } PublicList<user>getuserlist () {returndal. Getuserlist (); } }
The last is called, as follows:
USERBLL USERBLL = USERBLL (); public async task< Actionresult> Index () { var User = Userbll.getuserasync (); var listuser = Userbll.getuserlistasync (); int t1 = Environment.tickcount; await Task.whenall (user, listuser); Viewbag.user = User. Result; Viewbag.listuser = Listuser.result;
Viewbag.user = await User;
Viewbag.listuser = await listuser;
= Environment.tickcount- t1; return View (); }
Specific performance we can test, in the data access layer each time delay 500 milliseconds, with parallel and line line test, parallel about the time of about 500 milliseconds, and the line is about 1000 milliseconds
The above reference Dudu practical case: Implementing parallel http://www.cnblogs.com/dudu/p/async_await_parallel.html in existing code through ASYNC/AWAIT
. NET implements parallelism through Async/await