Let’s say you have the service:
interface ISuessService {
Task<Thing> Thing1();
Task<Thing> Thing2();
}
And I have an extension method ContinueOnUIThread, where I can do cool stuff like:
myService.Thing1().ContinueOnUIThread(_ => label.Text = "Done!");
and interact with the UI thread easily.
What is the best way to implement a new ContinueWith extension method that does this:
myService.Thing1()
.ContinueWith(myService.Thing2())
.ContinueOnUIThread(_ => label.Text = "Done!");
Basically starts Thing2 after Thing1 is complete, followed by a call to the UI thread.
The closest I’ve come is something like this, but I really don’t like calling Wait:
myService.Thing1()
.ContinueWith(_ => {
var thing2 = myService.Thing2().Wait();
return thing2.Result;
})
.ContinueOnUIThread(_ => label.Text = "Done!");
Is there a clean way to do this?
PS – I don’t have .Net 4.5 so no await/async allowed – this code has to run on MonoTouch/Mono for Android so stick to 4.0
PS – note my use of _, this is just a shortcut for “I’m not really using this parameter”
I think what you might be looking for is the Unwrap method from System.Threading.Tasks.TaskExtensions