Asynchronous Invocation

So far, you’ve seen delegates that return nearly instantaneously when they are being invoked. For example:

Func<int, int, int> add = (a, b) => a + b;int three = add(1, 2); // returns in nanoseconds

However, delegates can invoke long-running operations, too. If the method being referred to takes a long while to complete, the invocation site of the delegate will be blocked until the call returns. This is an important realization for APIs that accept as their input a delegate that gets called subsequently: The API can’t know how the call will perform given certain inputs. For example, in our calculator example, we could add the following custom function:

calc.AddOperator("F1", ...

Get C# 5.0 Unleashed now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.