Csharp/C Sharp/Language Basics/Delegate Async

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

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;
    }
}


Async Delegates Callback

 
using System;
using System.Threading;
   
class AsyncDelegatesCallback
{
    public static int Add(int op1, int op2, out int result)
    {
        Thread.Sleep(1000); 
        return (result = op1 + op2);
    }
    public delegate int AddDelegate(int op1, int op2,
        out int result);
   
    public static void AnnounceSum(IAsyncResult iar)
    {
        AddDelegate add = (AddDelegate)iar.AsyncState;
   
        int result;
        add.EndInvoke(out result, iar);
   
        Console.WriteLine("[AnnounceSum] The result is {0}", result);
    }
   
    static void Main()
    {
        int result;
        AddDelegate add = new AddDelegate(Add);
        add.BeginInvoke(6, 42, out result, new AsyncCallback(AnnounceSum), add);
   
        Thread.Sleep(1000); 
    }
}


delegate BeginInvoke of IAsyncResult

 
using System;
using System.Threading;
   
class AsyncDelegatesBlocked
{
    public static int Add(int op1, int op2, out int result)
    {
        Thread.Sleep(3000);
        return (result = op1 + op2);
    }
    public delegate int AddDelegate(int op1, int op2,out int result);
   
    static void Main()
    {
        int result;
        AddDelegate add = new AddDelegate(Add);
   
        IAsyncResult iAR = add.BeginInvoke(6, 42, out result,null, null);
   
        for (int i = 0; i < 10; i++) 
        {
            Thread.Sleep(200);
            Console.Write(".");
        }
        iAR.AsyncWaitHandle.WaitOne();    
        add.EndInvoke(out result, iAR);
   
        Console.WriteLine("[Main] The result is {0}", result);
    }
}


What happens if an unhandled exception is raised in a multicast delegate?

 
using System;
using System.Threading;

public delegate void DelegateClass();
public class Starter {
    public static void Main() {
        try {
            DelegateClass del = MethodA;
            IAsyncResult ar = del.BeginInvoke(null, null);
            del.EndInvoke(ar);
        } catch (Exception except) {
            Console.WriteLine("Exception caught: "+ except.Message);
        }
    }
    public static void MethodA() {
        if (Thread.CurrentThread.IsThreadPoolThread == true) {
            Console.WriteLine("Running on a thread pool thread");
        } else {
            Console.WriteLine("Running on primary thread");
        }
        throw new Exception("failure");
    }
}