"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