Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. class Program
  2. {
  3. //Delegate
  4. private delegate int BinaryOperation(int x, int y);
  5.  
  6. static void Main(string[] args)
  7. {
  8. //Current Thread
  9. Console.WriteLine("Add() anropas på tråd " + Thread.CurrentThread.ManagedThreadId.ToString());
  10.  
  11. //Create delegate
  12. BinaryOperation b = new BinaryOperation(Add);
  13.  
  14. //Call delegate async and save values
  15. IAsyncResult i= b.BeginInvoke(10, 10, null, null);
  16.  
  17. //No waiting time
  18. do
  19. {
  20. Console.WriteLine("Waiting...");
  21. Thread.Sleep(500);
  22.  
  23. }
  24. while (!i.IsCompleted);
  25.  
  26. //Waiting until BeginInvoke is finished
  27. int answer = b.EndInvoke(i);
  28.  
  29. //Output
  30. Console.WriteLine("10+10 = "+answer);
  31. }
  32.  
  33. static int Add(int x, int y)
  34. {
  35. //Skriv ut aktuell tråd
  36. Console.WriteLine("Add() anropas på tråd " + Thread.CurrentThread.ManagedThreadId.ToString());
  37.  
  38. //Sleep
  39. Thread.Sleep(2000);
  40.  
  41. //Return summan
  42. return x + y;
  43. }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement