Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.Remoting.Messaging;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace ConsoleApp25
- {
- class Program
- {
- delegate string MyDelegate(int a, out int b);
- static void Main(string[] args)
- {
- WithPolling();
- }
- public static void MyCallback(IAsyncResult ar)
- {
- //int b = 0;
- //string str = ((AsyncResult)ar).AsyncDelegate.EndInvoke(out b, ar);
- //Console.WriteLine(str + ": the b returned is: " + b);
- }
- public static void WithPolling()
- {
- MyDelegate dlg = new MyDelegate(MyMethod);
- int b = 0;
- IAsyncResult ar = dlg.BeginInvoke(2000, out b, null, null);
- Console.WriteLine("Some additional work - {0}", b);
- int count = 0;
- while(!ar.IsCompleted)
- {
- count++;
- Thread.Sleep(100);
- //Console.WriteLine("Continue doing other work");
- //continue doing other work
- }
- Console.WriteLine("Counter: {0}", count);
- //end work
- ar.AsyncWaitHandle.WaitOne(9000);
- String str = dlg.EndInvoke(out b, ar);
- Console.WriteLine(str + ": the b returned is: " + b);
- Console.ReadKey();
- }
- public static void WithWaitable()
- {
- MyDelegate dlg = new MyDelegate(MyMethod);
- int b = 0;
- IAsyncResult ar = dlg.BeginInvoke(1000, out b, null, null);
- Console.WriteLine("Some additional work - {0}", b);
- //end work
- ar.AsyncWaitHandle.WaitOne(9000);
- Console.WriteLine("End of waitone()");
- String str = dlg.EndInvoke(out b, ar);
- Console.WriteLine(str + ": the b returned is: " + b);
- Console.ReadKey();
- }
- public static void WithEndInvoke()
- {
- MyDelegate dlg = new MyDelegate(MyMethod);
- int b = 0;
- IAsyncResult ar = dlg.BeginInvoke(3000, out b, null, null);
- Console.WriteLine("Some additional work - {0}", b);
- //end work
- String str = dlg.EndInvoke(out b, ar);
- Console.WriteLine(str + ": the b returned is: " + b);
- Console.ReadKey();
- }
- public static string MyMethod(int a, out int b)
- {
- Thread.Sleep(a);
- b = Thread.CurrentThread.GetHashCode();
- return "Returned from my asynchronous method";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement