1. Define a procedure
VaR task1 = task. Factory. startnew () => dosomework ());
The method is as follows:
View code
Private Static ObjectDosomework ()
{
Console. writeline ("Nihao");
System. Threading. thread. Sleep (3000);
Console. Write ("Nihao");
Return "Xiexie";
}
2. After multiple concurrent threads are used, merge another method.
VaR task1 = task. Factory. startnew () => dosomework (). continuewith (
Task =>{ console. writeline (task. Result. tostring () ;}). continuewith (
Task =>{ console. writeline (task. Result. tostring ());});
3. Wait for the end of all the pending processes
VaR task1 = task. Factory. startnew () => dosomework ());
VaR task2 = task. Factory. startnew () => dosomework ());
VaR task3 = task. Factory. startnew () => dosomework ());
Task. waitall (task1, task2, task3 );
4. Wait for the end of a processing process
VaR task1 = task. Factory. startnew () => dosomework ());
VaR task2 = task. Factory. startnew () => dosomework ());
VaR task3 = task. Factory. startnew () => dosomework ());
Task. waitany (task1, task2, task3 );
5. Method for waiting for all the finished processes to end the finished rows
VaR task1 = task. Factory. startnew () =>
{
Thread. Sleep (3000 );
Return
"Dummy value 1"
;
});
VaR task2 = task. Factory. startnew () =>
{
Thread. Sleep (3000 );
Return
"Dummy value 2"
;
});
VaR task3 = task. Factory. startnew () =>
{
Thread. Sleep (3000 );
Return
"Dummy value 3"
;
});
Task. Factory. continuewhenall (
New
[] {Task1, task2, task3}, tasks =>
{
Foreach
(Task <
String
> Task
In
Tasks)
{
Console. writeline (task. Result );
}
});