Tasks are created and maintained explicitly by developers. Developers can control the tasks or process the return values. So, they have control over the the behavior of Task. Microsoft provides bunch of methods to control current Tasks.
Below are the methods which are being used to control current Task:
Start()
It starts the task and schedules for the execution.
Example
- public void StartTask()
- {
- Task<int> task = new Task<int>(obj =>
- {
- int myTotal= 0;
- int max = (int)obj;
- for (int i = 0; i < max; i++)
- {
- myTotal += i;
- }
- return myTotal;
- }, 400);
- task.Start();
- int result = Convert.ToInt32(task.Result);
- Console.WriteLine("Total {0}", result);
- }
So, here task.Start() method will start the task and we specified that we want result from 0 to 400.
Wait()
It waits for the Task to complete execution. It is a synchronization method that causes the calling thread to wait until the current task has completed.
Wait()
It waits for the Task to complete execution. It is a synchronization method that causes the calling thread to wait until the current task has completed.
Below are the reasons for which we will apply Wait() method:
- If main thread depends on the final result completed by the given Task.
- If you want to handle exception that might be thrown by Tasks.
Rupali ShindePosted Feb 23, 2016, 5:10 AM
nice
Former memberPosted Feb 20, 2016, 2:01 AM
Welcome
Santhakumar MunuswamyPosted Feb 20, 2016, 2:00 AM
Thanks for nice share