The task await keyword called by an asynchronous thread

Source: Internet
Author: User

There is a scenario where an async method a, called by the Foreach Loop b , does not have time to process the asynchronous returned data C, it executes the next loop,

When all loop b finishes executing, return to Async method aagain, because the void keyword will not catch the previous data C.

eg

private void Getsecondinfo (ienumerable<info> info)        {            foreach (var info1 in Info)            {                Secondresult (info1.category);            }        } Private async void Secondresult (String category)        {            string url = "Http://xxx" + category;            String content = await publicmethod.asynccallbac1 (URL);            When foreach executes the following method, the content is already null if (!string) due to the void keyword            . IsNullOrEmpty (content))            {           //json parsing            }        }

The analysis looks like the keyword Void is important, so there are 2 ways we can handle it:

First, transform 2 methods into one, and move the second method inside. Because the next judgment operation is performed after the foreach await, rather than continuing the next loop, the problem can be resolved,

Note that you want to change the synchronization method to asynchronous, that is, add async. This processing logic is: The Foreach loop, the asynchronous request network, then JSON parsing, and then start the second round of the Foreach Loop.

eg

        Private async void GetSecondinfo1 (ienumerable<info> info)        {            foreach (var info1 in Info)            {                string url = "Http://xxx" + category;                String content = await publicmethod.asynccallbac1 (URL);                When foreach executes, the following method is executed, because there is no other method call that can only execute the following method                if (!string. IsNullOrEmpty (content))                {                    //json parsing                }            }        }

The second method: because you know the problem is void (for specific reasons, it may be that the void keyword masks all values by default), and for async methods, you can change the void to a task. This is a way for friends in the group,

the logic of this processing is: When the foreach, the asynchronous request network, unequal results, continue to request the second thread, the last moment to process the JSON parsing .

eg

private void Getsecondinfo (ienumerable<info> info)        {            foreach (var info1 in Info)            {                Secondresult (info1.category);            }        }        Private Async Task Secondresult (String category)        {            string url = "Http://xxx" + category;            String content = await publicmethod.asynccallbac1 (URL);            foreach executes the following method, but this time because the wood has void, but the task, so the content will not be empty because of the asynchronous operation;            if (!string. IsNullOrEmpty (content))            {                //json parsing            }        }

  

The task await keyword called by an asynchronous thread

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.