C # Asynchronous Programming (II): Asynchronous Base replenishment

Source: Internet
Author: User

This time to introduce some usage:

Await a batch of tasks, do not wait for other tasks to complete, direct follow-up tasks.

    classProgram {Static voidMain (string[] args) {            varTaska = Delayandrerutnasync (2); varTASKB = Delayandrerutnasync (3); varTASKC = Delayandrerutnasync (1); vartasks =New[] {Taska, TASKB, TASKC}; varProcessingtasks= ( fromTinchTasksSelectAwaitandprocessasync (t)).            ToArray ();            Task.whenall (Processingtasks);        Console.read (); }        Static Asynctask<int> Delayandrerutnasync (intval) {            awaitTask.delay (Timespan.fromseconds (Val)); Console.WriteLine ("waiting for {0}", Val); returnVal; }        Static AsyncTask Awaitandprocessasync (task<int>Task) {            varresult =awaittask;        Console.WriteLine (result); }    }

As you can see from the printed results, after the execution of the method waiting for 1 seconds, there is no waiting for the execution of other methods, but the following async methods are executed directly.

Here's a way to use anonymous methods

    classProgram {Static voidMain (string[] args) {            varTaska = Delayandrerutnasync (2); varTASKB = Delayandrerutnasync (3); varTASKC = Delayandrerutnasync (1); vartasks =New[] {Taska, TASKB, TASKC}; varProcessingtasks = tasks. Select (AsyncTask =            {                varresult =awaittask;            Console.WriteLine (result);            });            Task.whenall (Processingtasks);        Console.read (); }        Static Asynctask<int> Delayandrerutnasync (intval) {            awaitTask.delay (Timespan.fromseconds (Val)); Console.WriteLine ("waiting for {0}", Val); returnVal; }            }

C # Asynchronous Programming (II): Asynchronous Base replenishment

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.