When doing a webrequest, or any other long running task, I should do it in a separate thread (let’s call this thread B) so as not to block the UI. But what’s the standard way to take action on the results once the task completes?
I’ve come up with these options so far:
1) Set up a custom event that fires in thread B when the request completes and that the main thread listen for and handles properly.
2) When the request completes make a InvokeOnMainThread call from thread B.
3) Pass along a reference to the ViewController when starting thread B. And then when the web request completes take whatever action is needed on the viewcontroller in thread B itself.
I use the ThreadPool to carry out these requests. This has the advantage that the runtime automatically adds and removes threads based on their usage and you do not have to manage their life cycle:
In this scenario, I use a couple of anonymous methods, by using nested anonymous methods (or lambdas), you can easily propagate the parameters (in this case “username”) from the original thread that calls PlusOne” to the callback m