// Sync. private static void FetchStockQuotesSync(WebService svc) { // This blocks. You don't know when the FetchStockQuotes // method returns. It may takes from minutes, to hours or // it may not return at all. IStockQuote qt = svc.FetchStockQuotes(); }
private static void Main() { IStockQuote qt = new StockQuote(); WebService svc = new WebService(qt); FetchStockQuotesSync(svc); FetchStockQuotesApm(svc); #if AsyncEnumerator FetchStockQuotesAsyncEnumerator(svc); #endif FetchStockQuotesAsyncCtp(svc); Console.ReadLine(); }
// Async CTP private static async void FetchStockQuotesAsyncCtp(WebService svc) { IStockQuote qt = await svc.FetchStockQuotesTaskAsync(); }
private static IEnumerator<Int32> FetchStockQuotesAsyncEnumerator(AsyncEnumerator ae, WebService svc) { svc.BeginFetchStockQuotes(ae.End(), null); yield return 1; IStockQuote qt = svc.EndFetchStockQuotes(ae.DequeueAsyncResult()); }
// AsyncEnumerator class. private static void FetchStockQuotesAsyncEnumerator(WebService svc) { AsyncEnumerator ae = new AsyncEnumerator(); ae.BeginExecute(FetchStockQuotesAsyncEnumerator(ae, svc), ae.EndExecute); }
private static void FetchStockQuotesApm(WebService svc) { Func<IStockQuote> @delegate = svc.FetchStockQuotes; // This never blocks. Your code returns immediately. @delegate.BeginInvoke(FetchStockQuotesApmCallback, @delegate); }
// IAsyncResult interface (APM). #if AsyncEnumerator private static void FetchStockQuotesApm(WebService svc) { // This never blocks. Your code returns immediately. svc.BeginFetchStockQuotes(FetchStockQuotesApmCallback, svc); }