Some records of C # async and awaiit,

Source: Internet
Author: User

Some records of C # async and awaiit,

I. Recently I have been learning about the content of the ABC. We found that the authors used async and awaiit in large quantities when obtaining data. Well, because I don't know much about it, I search for it online and record it here.

Let's take a look at a piece of code. When UpdateUserAsync is called, a Task is returned. What is a task. According to Jesse, task is the uncle of async and awaiit.

 protected virtual async Task UpdateUserAsync(CreateOrUpdateUserInput input)        {            var user = await UserManager.FindByIdAsync(input.User.Id.Value);                     if (!input.User.Password.IsNullOrEmpty())            {                CheckErrors(await UserManager.ChangePasswordAsync(user, input.User.Password));            }            CheckErrors(await UserManager.UpdateAsync(user));            //Update roles            CheckErrors(await UserManager.SetRoles(user, input.AssignedRoleNames));            if (input.SendActivationEmail)            {                user.SetNewEmailConfirmationCode();                await _userEmailer.SendEmailActivationLinkAsync(user, input.User.Password);            }        }

Ii. Operating Mechanism

The async flag will tell the editor that the following method may be used asynchronously. Of course, it is not necessarily used. It will wait until awaite is reached, and await will not start a new thread, the current thread continues until it encounters a real Async method (for example, HttpClient. getStringAsync), the internal method will use the Task. run or Task. factory. startNew to enable the thread. That is, if the method is not the Async method provided by. NET, we needCreate a Task by yourself to create a thread.

This is probably the case. We welcome criticism and correction.

Reference

Https://www.cnblogs.com/jesse2013/p/async-and-await.html#realawait

Https://docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/concepts/async/index

 

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.