Picking up from where I left off , I had promised to show you the code behind “both” button .The initial code is
Now, the reduction in time can be felt the most in this part. Because we will designate 2 different threads to work on the 2 services.
Now to do this,if we blindly follow the previous method, we will end up with this .
But this will not run. Infact this will freeze your application indefinitely? Why so?
The reason being, T1_next.Result executes before , T1 is actually finished . Now this is straight forward as we know that <task_name>.Result implicitly calls Task.Wait() and hence, it will freeze the UI thread and wait for the results.
But at the same time after T1 finishes, T1_next will be invoked, which is waiting at the local queue to be run on the UI thread. While the UI thread isn’t finished because it’s waiting for T1_next. Thus we have a deadlock. Always watch out for such deadlock, when you are dealing with synchronization contexts. So we need to do away with the waiting .
We need to change our code a bit
If you go through the code,you would see I have used a continueWhenAll method.I have explained the reason.If you want to know more visit .
As I had promised, I have uploaded the entire app code here.