Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Program
- {
- //Delegate
- private delegate int BinaryOperation(int x, int y);
- static void Main(string[] args)
- {
- //Current Thread
- Console.WriteLine("Add() anropas på tråd " + Thread.CurrentThread.ManagedThreadId.ToString());
- //Create delegate
- BinaryOperation b = new BinaryOperation(Add);
- //Call delegate async and save values
- IAsyncResult i= b.BeginInvoke(10, 10, null, null);
- //No waiting time
- do
- {
- Console.WriteLine("Waiting...");
- Thread.Sleep(500);
- }
- while (!i.IsCompleted);
- //Waiting until BeginInvoke is finished
- int answer = b.EndInvoke(i);
- //Output
- Console.WriteLine("10+10 = "+answer);
- }
- static int Add(int x, int y)
- {
- //Skriv ut aktuell tråd
- Console.WriteLine("Add() anropas på tråd " + Thread.CurrentThread.ManagedThreadId.ToString());
- //Sleep
- Thread.Sleep(2000);
- //Return summan
- return x + y;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement