private static void AsyncCallbackSample() { TakesaWhileDelegate takesaWhile = TakesaWhile; takesaWhile.BeginInvoke(1, 3000, TakesaWhileCompleted, takesaWhile); // NOTE: Или так: //takesaWhile.BeginInvoke(1, 3000, ar => //{ // int result = takesaWhile.EndInvoke(ar); // Console.WriteLine("result: {0}", result); //}, null); }
private static void Pooling() { TakesaWhileDelegate takesaWhileDelegate = TakesaWhile; IAsyncResult asyncResult = takesaWhileDelegate.BeginInvoke(1, 3000, null, null); while (!asyncResult.IsCompleted) { Console.Write("."); Thread.Sleep(50); } int result = takesaWhileDelegate.EndInvoke(asyncResult); Console.WriteLine("result: {0}", result); }
private static void WaitHandlePooling() { TakesaWhileDelegate whileDelegate = TakesaWhile; IAsyncResult asyncResult = whileDelegate.BeginInvoke(1, 3000, null, null); while (true) { Console.Write("."); if (asyncResult.AsyncWaitHandle.WaitOne(50, false)) { Console.WriteLine("Can get the result now"); break; } } int result = whileDelegate.EndInvoke(asyncResult); Console.WriteLine("result: {0}", result); }