c# - Async Delegates Callback Pattern leveraging Func -
all -
i have sample code async delegates utilizes callback pattern. using standard delegates , code below works fine. wanted convert func delegates (since expecting , int , returning int) reason - seem stuck. can please help.
class program { public delegate int somedelegate(int x); static void main(string[] args) { console.writeline("this callback pattern technique asynchronous delegates in threading"); console.writeline(""); somedelegate sd = squarenumber; console.writeline("[{0}] before squarenumber method invoked.", thread.currentthread.managedthreadid); iasyncresult asyncres = sd.begininvoke(10, new asynccallback(callbackmethod), null); console.writeline("[{0}] in main after squarenumber method invoked. doing processing.", thread.currentthread.managedthreadid); console.writeline("[{0}] main method processing completed.", thread.currentthread.managedthreadid); console.readline(); } static int squarenumber(int a) { console.writeline("[{0}] inside squarenumber - invoked. processing......", thread.currentthread.managedthreadid); thread.sleep(5000); return * a; } static void callbackmethod(iasyncresult asyncres) { console.writeline("[{0}] callback invoked", thread.currentthread.managedthreadid); asyncresult ares = (asyncresult)asyncres; somedelegate delg = (somedelegate)ares.asyncdelegate; int res = delg.endinvoke(asyncres); console.writeline("[{1}] result = {0}", res, thread.currentthread.managedthreadid); } }
thanks
class program { //public delegate int somedelegate(int x); static void main(string[] args) { console.writeline("this callback pattern technique asynchronous delegates in threading"); console.writeline(""); func<int,int> sd = squarenumber; console.writeline("[{0}] before squarenumber method invoked.", thread.currentthread.managedthreadid); iasyncresult asyncres = sd.begininvoke(10, new asynccallback(callbackmethod), null); console.writeline("[{0}] in main after squarenumber method invoked. doing processing.", thread.currentthread.managedthreadid); console.writeline("[{0}] main method processing completed.", thread.currentthread.managedthreadid); console.readline(); } static int squarenumber(int a) { console.writeline("[{0}] inside squarenumber - invoked. processing......", thread.currentthread.managedthreadid); thread.sleep(5000); return * a; } static void callbackmethod(iasyncresult asyncres) { console.writeline("[{0}] callback invoked", thread.currentthread.managedthreadid); asyncresult ares = (asyncresult)asyncres; func<int,int> delg = (func<int,int>)ares.asyncdelegate; int res = delg.endinvoke(asyncres); console.writeline("[{1}] result = {0}", res, thread.currentthread.managedthreadid); } }
works me.
Comments
Post a Comment