I have the following (simplified) asynchronous method:
void Transform<X,Y>(X x, Action<Y> resultCallback) {...}
and what I want to do is transform a list of Xs into a list of Ys.
The problem is that even though the Transform method is asynchronous, it has to be called serially (i.e. I have to wait for the callback before calling it with the next value).
Is there any way to do this elegantly? (I’m on .Net 4.0)
I’m guessing there might be some way to do it with continuation passing…
UPDATE I forgot to specify that I don’t want to block the calling (GUI) thread.
If you wrap this in a helper class, you could make the helper “synchronize” your values:
You could then use this like:
Edit:
Since you say you’re trying to do this to keep everything running on a background thread, you can use my code above, and do:
This will start the entire (now synchronous) process on a background thread, and disable your “throbber” (from comment) on the UI thread once it completes.
If you control all of this code, you can make your Transform process synchronous from the start, and just move it into a background thread as above, avoiding the need for the wrapper.