//method that receives a callback when the results are available void ResultsReady(IAsyncResult result) { List <string> keys_matched = new List <string>(); // Extract the delegate from the // System.Runtime.Remoting.Messaging.AsyncResult. GetResultsDelegate getResultDelegate = (GetResultsDelegate)((AsyncResult)result).AsyncDelegate; int g = (int)result.AsyncState; // Obtain the result bool return_val = getResultDelegate.EndInvoke(ref keys_matched, result); waiter.Set(); }
//Method2: asynchronous pattern using a BeginInvoke, followed by waiting with a time-out public void GetResultsAndWait(string key, ref List <string> keys_matched, int waitTime = 10000) { //declare instance of delegate GetResultsDelegate getResultDelegate = new GetResultsDelegate(ResultsGetter.GetResults); //asynchonously invoke the GetResult method IAsyncResult result = getResultDelegate.BeginInvoke(key, ref keys_matched, null, null); while (!result.IsCompleted) { //do any work that can done before waiting result.AsyncWaitHandle.WaitOne(waitTime, false); } result.AsyncWaitHandle.Close(); //the asynchronous operation has completed bool return_val = getResultDelegate.EndInvoke(ref keys_matched, result); }