WebApi uploads the image await keyword,

Source: Internet
Author: User

WebApi uploads the image await keyword,
Influence of await keywords on method execution

Modify the code in the previous WebApi upload image (the await keyword is used) as follows:

 

[HttpPost] public async Task <string> Post () {if (! Request. Content. IsMimeMultipartContent () throw new HttpResponseException (Request. CreateResponse (HttpStatusCode. NotAcceptable, "Invalid Request! "); // Get Student information Student model = new Student () {Name = HttpContext. current. request. form ["StuName"], GroupName = HttpContext. current. request. form ["GroupName"], //...}; // obtain the student's Subject name string passSubject = HttpContext. current. request. form ["passSubject"]; // obtain the student's failed account name string noPassSubject = HttpContext. current. request. form ["passSubject"]; Trace. writeLine ("begin add student information"); // Add student information await stuService. addStu ByAsync (model ). continueWith (p => {long stuId = p. result; Trace. writeLine ("begin through the subject"); subjectService. addPassSubject (passSubject, stuId); // Add this student Trace through the subject information. writeLine ("end through KE Jing"); Trace. writeLine ("begin failed"); subjectService. addNoPassSubject (noPassSubject, stuId); // Add Trace of the student's failed subject information. writeLine ("end failed to pass the Cortana") ;}); Trace. writeLine ("end add student information"); string path = System. web. httpContext. current. ser Ver. MapPath ("~ /Images/upload/"); Trace. writeLine ("Get image ...... "); Request. content. readAsMultipartAsync (). continueWith (p => {var content = p. result. contents; Trace. writeLine ("begin image"); foreach (var item in content) {if (string. isNullOrEmpty (item. headers. contentDisposition. fileName) {continue;} item. readAsStreamAsync (). continueWith (a => {Stream stream =. result; string fileName = item. headers. contentDisposition. fileName; fileName = fileName. substring (1, fileName. length-2); Trace. writeLine ("image name:" + fileName); // convert stream to image saveImg (path, stream, fileName) ;});} Trace. writeLine ("end image") ;}); return "OK ";}

 

Result:

Not included in the await main line and child in ContinueWith

All threads are executing

The await main thread will wait for the child in ContinueWith

Thread

We can see from the comparison that after await is added, when the method is executed to await, it does not continue to be executed downward. Instead, it will continue to be executed after the method is executed to await.

That is to say, when await is encountered, the current thread will temporarily stop and wait until the method execution after await is completed.

 

 

 

Add a new method AddPassSubjectByAsync. await is not used in this method.

The code for adding student information is modified as follows:
Trace. writeLine ("begin add student information"); // Add student information stuService. addStuByAsync (model ). continueWith (async p => {Trace. writeLine ("begin subthread 2"); long stuId = p. result; subjectService. addPassSubjectByAsync (passSubject, stuId ). continueWith (a => {Trace. writeLine ("subthread 3") ;}); Trace. writeLine ("end sub-thread 2") ;}); Trace. writeLine ("end add student information ");

Running result:

 

We can see that when sub-thread 3 is running, sub-thread 2 and the main thread are also running.

The next step is the problem. If the subthread 2 contains the await keyword, what is the thread execution status?

 

Example 1:

The Code is as follows:

Trace. writeLine ("begin add student information"); // Add student information stuService. addStuByAsync (model ). continueWith (async p => {Trace. writeLine ("begin subthread 2"); long stuId = p. result; await subjectService. addPassSubjectByAsync (passSubject, stuId ). continueWith (a => {Trace. writeLine ("begin subthread 3"); Trace. writeLine ("begin failed"); subjectService. addNoPassSubject (noPassSubject, stuId); Trace. writeLine ("end does not pass the Cortana"); Trace. writeLine ("end subthread 3") ;}); Trace. writeLine ("end sub-thread 2") ;}); Trace. writeLine ("end add student information"); // test whether the main thread executes Trace during thread 3 execution. writeLine ("begin test"); subjectService. getSubjectList (); // gets Trace information about all subjects. writeLine ("end test ");

Result:

 

The first four lines show that both the main thread and subthread 2 are being executed. When Sub-thread 2 is executed to await, sub-thread 2 changes to "paused" and sub-thread 3 is executed. At this time, the main thread and sub-thread 3 are being executed (from sub-thread 3 has not ended, the main thread has started the test. You can see that ).

When Sub-thread 3 is finished, sub-thread 2 wakes up from the "paused" status and continues to run down.

It indicates that the execution status of sub-threads 2 and 3 does not affect the main thread.

 

Example 2:

All have await. The Code is as follows:

Trace. writeLine ("begin add student information"); // Add student information await stuService. addStuByAsync (model ). continueWith (async p => {Trace. writeLine ("begin subthread 2"); long stuId = p. result; await subjectService. addPassSubjectByAsync (passSubject, stuId ). continueWith (a => {Trace. writeLine ("begin subthread 3"); Trace. writeLine ("begin failed"); subjectService. addNoPassSubject (noPassSubject, stuId); Trace. writeLine ("end does not pass the Cortana"); Trace. writeLine ("end subthread 3") ;}); Trace. writeLine ("end sub-thread 2") ;}); Trace. writeLine ("end add student information"); // test whether the main thread executes Trace during thread 3 execution. writeLine ("begin test"); subjectService. getSubjectList (); // gets Trace information about all subjects. writeLine ("end test ");

Result:

The first two lines of output show that when the main thread is running at await, the main thread is "paused" and waits for subthread 2 to return the result. At this time, sub-thread 2 starts to execute, and when it is also running at await, sub-thread 2 "pause ",

The output information shows that the main thread will be "awakened" when the sub-thread 3 is being executed (at this time, sub-thread 3 and the main thread are being executed ). When Sub-thread 3 is completed, sub-thread 2 continues to run.

Note: When the await keyword is encountered in the method, program execution can be divided into two parts.

1. The current thread is "paused" and the method after await is executed (for example, subthread 2 ).

2. If the main thread is "paused", the main thread will be "awakened" (there will be two threads running at this time, such as the main thread and subthread 3 ).

 

Related Article

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.