编程语言
首页 > 编程语言> > c#-异步回调-简单实验

c#-异步回调-简单实验

作者:互联网

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.运行效果 

标签:异步,c#,System,da,int,using,回调,DelegateAdd,iar
来源: https://blog.csdn.net/xie__jin__cheng/article/details/113801003