First of all I assume you need to do asynchronous job:
public async void Step1(){ /* bla*/ }
public async void Step2(){ /* bla*/ }
Then call should be:
public async void taskRunner(){
await Task.Run(() => { await Step1(); await Step2(); });
}
In second place to be sure it will work like Continue with
you have to add exception handling (second task is always executed even if first ends with exception.
public async void taskRunner(){
await Task.Run(() => {
try{
await Step1();
}catch(Exception e){
}
await Step2();
});
}
Another point is that tasks can be lambdas, and as such they can just take previous task result and unpack it into the next task, that requires an extra variable if done without ContinueWith
:
double[] nums = { 3,5,2,6,5,4,3 };
await Task.Run(() => { //still missing exception handling here ;)
double result = await GetSum( nums);
await SubtractValue(nums, result);
});
With ContinueWith
double[] nums = { 3,5,2,6,5,4,3 };
await Task.Run( () => await GetSum(nums))
.ContinueWith( t => await SubtractValue(nums, t.Result));
In this particular case there's no syntax gain, but there could be even more complex examples that is just unfeasible write without the help of ContinueWith:
Also worth to note that the compiler is smart enough to optimize the code in 2 different cases to be the same so you have to choose (unless you require a particular behaviour) wich way is the more clear to you.
I hope I made no error, I not checked if the code compile, sorry for that but is late here, I will correct the answer tomorrow in case.
Task.ContinueWith
is a special mechanism to combine asynchronous methods into synchronous job. – Sinatr Aug 19 '15 at 9:23