Prev | Current Page 347 | Next

Jon Skeet

"C# in Depth: What you need to master C# 2 and 3"

We do this by providing
the CCR with an implementation of IEnumerator (where ITask is an interface
defined by the CCR). Here??™s pseudo-code for our request handling in this style:
IEnumerator ComputeTotalStockValue(string user, string pass)
{
Token token = null;
yield return Ccr.ReceiveTask(
AuthService.CcrCheck(user, pass)
delegate(Token t){ token = t; }
);
Holdings stocks = null;
StockRates rates = null;
yield return Ccr.MultipleReceiveTask(
DbService.CcrGetStockHoldings(token),
StockService.CcrGetRates(token),
delegate(Stocks s, StockRates sr)
{ stocks = s; rates = sr; }
);
OnRequestComplete(ProcessStocks(stocks, rates));
}
Confused? I certainly was when I first saw it??”but now I??™m somewhat in awe of how neat
it is. The CCR will call into our code (with a call to MoveNext on the iterator), and we??™ll
execute until and including the first yield return statement. The CcrCheck method
within AuthService would kick off an asynchronous request, and the CCR would wait
181 Summary
(without using a dedicated thread) until it had completed, calling the supplied delegate
to handle the result. It would then call MoveNext again, and our method would
continue. This time we kick off two requests in parallel, and the CCR to call another delegate
with the results of both operations when they??™ve both finished. After that, Move-
Next is called for a final time and we get to complete the request processing.


Pages:
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359