1.概要
要点
public delegate int DelegateAdd(int a);
IAsyncResult iar = da.BeginInvoke(6, goBack, da);
static void goBack(IAsyncResult iar) {
。。。。
DelegateAdd da = iar.AsyncState as DelegateAdd;
int re = da.EndInvoke(iar);
。。。。。
}
记忆点:delegate ,IAsyncResult, BeginInvoke ,AsyncState, EndInvoke
2.代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Runtime.CompilerServices;
namespace ConsoleApp7
{
class Program
{
public delegate int DelegateAdd(int a);
static void Main(string[] args)
{
Console.WriteLine("hello word");
DelegateAdd da = fun;
IAsyncResult iar = da.BeginInvoke(6, goBack, da);
Console.ReadKey();
}
static void goBack(IAsyncResult iar) {
if (iar == null) throw new ArgumentNullException("iar");
DelegateAdd da = iar.AsyncState as DelegateAdd;
int re = da.EndInvoke(iar);
Console.WriteLine(re);
}
static int fun(int a) {
//Thread.Sleep(50);
return a * 2;
}
}
}
3.运行效果