fork download
  1. using System;
  2. using System.Threading;
  3.  
  4. namespace Recetas.Cap04
  5. {
  6. public class AsyncDemo
  7. {
  8. string LongRunningMethod (int iCallTime, out int iExecThread)
  9. {
  10. Thread.Sleep (iCallTime) ;
  11. iExecThread = AppDomain.GetCurrentThreadId ();
  12. return "MyCallTime was " + iCallTime.ToString() ;
  13. }
  14.  
  15. delegate string MethodDelegate(int iCallTime, out int iExecThread) ;
  16.  
  17. public void DemoCallback()
  18. {
  19. MethodDelegate dlgt = new MethodDelegate (this.LongRunningMethod) ;
  20. string s ;
  21. int iExecThread;
  22.  
  23. // Create the callback delegate.
  24. AsyncCallback cb = new AsyncCallback(MyAsyncCallback);
  25.  
  26. // Initiate the Asynchronous call passing in the callback delegate
  27. // and the delegate object used to initiate the call.
  28. IAsyncResult ar = dlgt.BeginInvoke(3000, out iExecThread, cb, dlgt);
  29. }
  30.  
  31. public void MyAsyncCallback(IAsyncResult ar)
  32. {
  33. string s ;
  34. int iExecThread ;
  35.  
  36. // Because you passed your original delegate in the asyncState parameter
  37. // of the Begin call, you can get it back here to complete the call.
  38. MethodDelegate dlgt = (MethodDelegate) ar.AsyncState;
  39.  
  40. // Complete the call.
  41. s = dlgt.EndInvoke (out iExecThread, ar) ;
  42.  
  43. Console.WriteLine (string.Format ("The delegate call returned the string: \"{0}\", and the number {1}", s, iExecThread.ToString() ) );
  44. }
  45.  
  46. static void Main(string[] args)
  47. {
  48. AsyncDemo ad = new AsyncDemo () ;
  49. ad.DemoCallback() ;
  50. }
  51. }
  52. }
Success #stdin #stdout 0.01s 34744KB
stdin
Standard input is empty
stdout
Standard output is empty