static void CallbackMethod(IAsyncResult ar) { // ar -> delegate which comes from BeginInvoke method // so we retrieve it AsyncResult async = (AsyncResult)ar; AsyncMethodHandler asyncCaller = (AsyncMethodHandler)async.AsyncDelegate; // these are parameters that we passed to our async method string formatString = (string)ar.AsyncState; int threadId = 0; Console.WriteLine(formatString, threadId, asyncCaller.EndInvoke(out threadId, ar)); }
static void Main(string[] args) { int threadId; AsyncMethodHandler ah = new AsyncMethodHandler(Program.AsyncMethod); ah.BeginInvoke(9000, out threadId, new AsyncCallback(CallbackMethod), "The call executed on thread {0}, with return value: \n\"{1}\"."); Console.WriteLine($"The main thread {System.Threading.Thread.CurrentThread.ManagedThreadId} continues to execute..."); System.Threading.Thread.Sleep(4000); Console.WriteLine($"Main thread {System.Threading.Thread.CurrentThread.ManagedThreadId} ends."); }
protected void CallBackBuildControls(IAsyncResult ar) { try { // Retrieve the delegate. AsyncResult result = (AsyncResult)ar; AsyncMethodHandler caller = (AsyncMethodHandler)result.AsyncDelegate; // Call EndInvoke to retrieve the results. _PACTControlData = caller.EndInvoke(ar); // Still on secondary thread, must update ui on primary thread UpdateUI(_PACTControlData); } catch (Exception ex) { string exMessage = null; exMessage = "Error: " + ex.Message; //UpdateUI(exMessage); } }
public void DodajDelegata(AsyncMethodHandler handler) { Event += handler; }