Async Delegate Invocation : Delegate Async : Language Basics C# Source Code


Custom Search

C# Source Code » Language Basics » Delegate Async »

 

Async Delegate Invocation








    
 

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

public delegate int BinaryOp(int x, int y);

class Program {
    static void Main(string[] args) {
        BinaryOp b = new BinaryOp(Add);
        IAsyncResult iftAR = b.BeginInvoke(10, 10, null, null);

        while (!iftAR.AsyncWaitHandle.WaitOne(2000, true)) {
            Console.WriteLine("Doing more work in Main()!");
        }

        int answer = b.EndInvoke(iftAR);
        Console.WriteLine("10 + 10 is {0}.", answer);
    }

    static int Add(int x, int y) {
        Console.WriteLine("Add() invoked on thread {0}.", Thread.CurrentThread.GetHashCode());
        Thread.Sleep(5000);
        return x + y;
    }
}

 
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo Language Basics
» Delegate Async