Esempio n. 1
0
    static void Main(string[] args)
    {
        // assign the deleate
        PerformCalc myDelegate = PerformCalcMethod;

        // call the method several times
        myDelegate.BeginInvoke(0, int.MaxValue, 1, AsyncMethodCallback, myDelegate);
        myDelegate.BeginInvoke(0, int.MaxValue / 2, 1, AsyncMethodCallback, myDelegate);
        myDelegate.BeginInvoke(0, int.MaxValue / 4, 4, AsyncMethodCallback, myDelegate);

        Console.WriteLine("Async methods are running...");

        // wait for input before exiting
        Console.WriteLine("Press enter to finish");
        Console.ReadLine();
    }
Esempio n. 2
0
    static void Main(string[] args)
    {
        Calculator calc = new Calculator();

        // get the delegate
        PerformCalc del = calc.CalcDelegate;

        // invoke the delegate to get a result
        int result = del(10, 20);

        // print out the result
        Console.WriteLine("Result: {0}", result);

        // wait for input before exiting
        Console.WriteLine("Press enter to finish");
        Console.ReadLine();
    }
Esempio n. 3
0
        static void Main(string[] args)
        {
            // create a new Calculator
            Calculator calc = new Calculator();
            // get a delegate
            PerformCalc del = calc.GetDelegate(Calculator.Modes.Normal);

            // use the delegate
            Console.WriteLine("Normal product: {0}", del(10, 20));
            // get a delegate
            del = calc.GetDelegate(Calculator.Modes.Iterative);
            // use the delegate
            Console.WriteLine("Iterative product: {0}", del(10, 20));
            // wait for input before exiting
            Console.WriteLine("Press enter to finish");
            Console.ReadLine();
        }
    static void Main(string[] args)
    {
        // assign the deleate
        PerformCalc myDelegate = PerformCalcMethod;

        // call the method several times
        IAsyncResult res1 = myDelegate.BeginInvoke(0, int.MaxValue, 1, AsyncMethodCallback, myDelegate);
        IAsyncResult res2 = myDelegate.BeginInvoke(0, int.MaxValue / 2, 1, AsyncMethodCallback, myDelegate);
        IAsyncResult res3 = myDelegate.BeginInvoke(0, int.MaxValue / 4, 4, AsyncMethodCallback, myDelegate);

        Console.WriteLine("Async methods are running...");

        // wait for each of the async methods to complete
        res1.AsyncWaitHandle.WaitOne();
        res2.AsyncWaitHandle.WaitOne();
        res3.AsyncWaitHandle.WaitOne();

        Console.WriteLine("Async methods have all completed");

        // wait for input before exiting
        Console.WriteLine("Press enter to finish");
        Console.ReadLine();
    }
    static void Main(string[] args)
    {
        // assign the deleate
        PerformCalc myDelegate = PerformCalcMethod;

        // call the method several times
        IAsyncResult res1 = myDelegate.BeginInvoke(0, int.MaxValue, 1, null, myDelegate);
        IAsyncResult res2 = myDelegate.BeginInvoke(0, int.MaxValue / 2, 1, null, myDelegate);
        IAsyncResult res3 = myDelegate.BeginInvoke(0, int.MaxValue / 4, 4, null, myDelegate);

        Console.WriteLine("Async methods are running...");

        foreach (IAsyncResult res in new IAsyncResult[] { res1, res2, res3 })
        {
            long result = myDelegate.EndInvoke(res);
            Console.WriteLine("Result: {0}", result);
        }

        Console.WriteLine("Async methods have all completed");

        // wait for input before exiting
        Console.WriteLine("Press enter to finish");
        Console.ReadLine();
    }
Esempio n. 6
0
 public Calculator()
 {
     perfCalc = CalculateProduct;
 }
Esempio n. 7
0
 public Calculator() 
 {
     perfCalc = CalculateProduct;
 }