"Multithreaded" Task

Source: Internet
Author: User

"Multithreaded" Task

A. Common methods:

1. ContinueWith: When the current task completes, the incoming task is executed

2. Delay: Creates a waiting Task that is blocked only when the wait method is called (note: different from Thread.Sleep)

3. Run: Create a Task and execute

4. Start: Execute Task

5. Wait: Blocks the thread for a certain amount of time until the Task completes or the wait time is exceeded

6. WaitAll: Blocking threads until all tasks have completed or exceeded wait time

7. WaitAny: Blocking threads until one Task completes or waits longer

Two. Common Properties:

1. Factory: Get the factory used to create the task, create a task with

2. Status of the Status:task

Three. Syntax

   Private Static voidMain (string[] args) {Console.WriteLine (DateTime.Now+"Start"); Task T1= Task.Factory.StartNew (dotask). ContinueWith (Continuetask);//Create a Task execution Dotask, Dotask execute continuetask after completionTask t2 =Task.Factory.StartNew (dotask); Task.waitany (New[] {t1, t2}, +); Console.WriteLine (DateTime.Now+"WaitAny Timeout 1 seconds"); Task.waitall (New[] {t1, t2},Ten* +); Console.WriteLine (DateTime.Now+"WaitAll timeout 10 seconds"); T1. Wait ( -* +);//Note: t here refers to ContinueWith, not StartNew, which is the final Task of the expressionConsole.WriteLine (DateTime.Now +"wait T1 timeout 50 seconds");        Console.ReadLine (); }        Private Static voidDotask () {Task.delay (3* +). Wait ();//blocked for 3 seconds, only valid after WaitConsole.WriteLine (DateTime.Now +"Dotask finished, worked for 3 seconds"); }        Private Static voidContinuetask (Task Task) {Task.delay (4* +). Wait ();//blocked for 4 seconds, only valid after WaitConsole.WriteLine (DateTime.Now +"Continuetask completed and worked for 4 seconds after dotask."); }

   

"Multithreaded" Task

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.