Advertisement
ivorakitin

AsyncTask

Apr 9th, 2025
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.Remoting.Messaging;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8.  
  9. namespace ConsoleApp25
  10. {
  11.     class Program
  12.     {
  13.         delegate string MyDelegate(int a, out int b);
  14.         static void Main(string[] args)
  15.         {
  16.             WithPolling();
  17.         }
  18.  
  19.         public static void MyCallback(IAsyncResult ar)
  20.         {
  21.             //int b = 0;
  22.             //string str = ((AsyncResult)ar).AsyncDelegate.EndInvoke(out b, ar);
  23.             //Console.WriteLine(str + ": the b returned is: " + b);
  24.         }
  25.  
  26.         public static void WithPolling()
  27.         {
  28.             MyDelegate dlg = new MyDelegate(MyMethod);
  29.             int b = 0;
  30.             IAsyncResult ar = dlg.BeginInvoke(2000, out b, null, null);
  31.             Console.WriteLine("Some additional work - {0}", b);
  32.             int count = 0;
  33.             while(!ar.IsCompleted)
  34.             {
  35.                 count++;
  36.                 Thread.Sleep(100);
  37.                 //Console.WriteLine("Continue doing other work");
  38.                 //continue doing other work
  39.             }
  40.             Console.WriteLine("Counter: {0}", count);
  41.  
  42.             //end work
  43.             ar.AsyncWaitHandle.WaitOne(9000);
  44.             String str = dlg.EndInvoke(out b, ar);
  45.             Console.WriteLine(str + ": the b returned is: " + b);
  46.             Console.ReadKey();
  47.         }
  48.  
  49.         public static void WithWaitable()
  50.         {
  51.             MyDelegate dlg = new MyDelegate(MyMethod);
  52.             int b = 0;
  53.             IAsyncResult ar = dlg.BeginInvoke(1000, out b, null, null);
  54.             Console.WriteLine("Some additional work - {0}", b);
  55.             //end work
  56.             ar.AsyncWaitHandle.WaitOne(9000);
  57.             Console.WriteLine("End of waitone()");
  58.             String str = dlg.EndInvoke(out b, ar);
  59.             Console.WriteLine(str + ": the b returned is: " + b);
  60.             Console.ReadKey();
  61.         }
  62.  
  63.         public static void WithEndInvoke()
  64.         {
  65.             MyDelegate dlg = new MyDelegate(MyMethod);
  66.             int b = 0;
  67.             IAsyncResult ar = dlg.BeginInvoke(3000, out b, null, null);
  68.             Console.WriteLine("Some additional work - {0}", b);
  69.             //end work
  70.             String str = dlg.EndInvoke(out b, ar);
  71.             Console.WriteLine(str + ": the b returned is: " + b);
  72.             Console.ReadKey();
  73.         }
  74.  
  75.         public static string MyMethod(int a, out int b)
  76.         {
  77.             Thread.Sleep(a);
  78.             b = Thread.CurrentThread.GetHashCode();
  79.             return "Returned from my asynchronous method";
  80.         }
  81.     }
  82. }
  83.  
Tags: Task
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement