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#-异步回调-简单实验

上一篇:基于机智云AIoT开发平台的智能仓库


下一篇:亚马逊云科技AIoT助力家居智能化,构建智联生活新未来