• IAsyncResult


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace asyncApp
    {
        public partial class FrmAsync : Form
        {
            public FrmAsync()
            {
                InitializeComponent();
            }
            public int ExecuteTask1(int num)
            {
                Thread.Sleep(5000);
                return num * num;
            }
            public int ExecuteTask2(int num)
            {
                return num * num;
            }
            private void btnExectue1_Click(object sender, EventArgs e)
            {
                this.lblCount1.Text = ExecuteTask1(10).ToString();
                this.lblCount2.Text = ExecuteTask2(10).ToString();
            }
    
            private void btnExecute2_Click(object sender, EventArgs e)
            {
                MyDelegate dete = ExecuteTask1;
                //异步操作执行状态借口
                IAsyncResult result = dete.BeginInvoke(10,null,null);
                this.lblCount1.Text = "正在计算......";
                this.lblCount2.Text = ExecuteTask2(10).ToString();
                //EndInvoke方法借助IAsyncResult借口对象,不断地查询异步调用是否结束;
                //该方法知道异步调用的方法所有参数,所以异步调用完毕以后,取出异步调用结果作为返回值
                int res = dete.EndInvoke(result);
                this.lblCount1.Text = res.ToString();
            }
            public delegate int MyDelegate(int num);
        }
    }
    

      

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace asyncAppCallback
    {
        public partial class FrmAppCallback : Form
        {
            public FrmAppCallback()
            {
                InitializeComponent();
                this.objCal = new MyDelegate(ExecuteTask);//初始化成员变量
                //this.objCal = (num, ms)=>
                //{
                //    Thread.Sleep(ms);
                //    return num * num;
                //};
    
            }
            //【1】声明一个委托
            public delegate int MyDelegate(int num, int ms);
            //【2】根据委托声明一个方法 
            private int ExecuteTask(int num, int ms)
            {
                Thread.Sleep(ms);
                return num * num;
            }
            //【3】创建委托变量
            MyDelegate objCal = null;//ExecuteTask;
            //【4】同步执行多个任务
            private void btnExecu_Click(object sender, EventArgs e)
            {
                for (int i = 1; i <= 10; i++)
                {
                    objCal.BeginInvoke(10 * i, 1000 * i, MyCallback,i);
                }
            }
            //【5】创建回调函数
            private void MyCallback(IAsyncResult result)
            {
               int res= objCal.EndInvoke(result);
                //异步显示结果
                Console.WriteLine("第{0}个计算结果:{1}",result.AsyncState,res);
            }
        }
    }
    

      

  • 相关阅读:
    CentOS下部署Django线上运行环境
    MAC 下安装mysqlclient、Homebrew踩坑记
    kali基本命令(-)
    关于Django出现“__str__ returned non-string (type NoneType)”错误的处理
    deepin升级时出现“E: 无法获得锁 /var/lib/apt/lists/lock
    RCNN训练过程
    Jupyter Notebook中使用虚拟环境
    pytorch(cuda)的安装
    conda的基本操作
    第一周、深度学习概念
  • 原文地址:https://www.cnblogs.com/Jeely/p/11001987.html
Copyright © 2020-2023  润新知