NET中级课--浅谈委托,事件,异步调用,回调等概念

时间:2023-03-08 15:39:40
NET中级课--浅谈委托,事件,异步调用,回调等概念

直接说题。

委托

        首先明确它是什么,其实就是一个类,定义一个委托即定义一个类,那么它是什么类?用来说明方法的类型的类。字段有类型,那么方法其实也有类型,就是委托。

      委托是某一类方法的总定义。

   事件

          事件是某个类用于传递消息的方式。事件之余委托,犹如属性之余变量即是委托的封装。

    好了,明确了概念,接下来说示例。

   示例

        有3个对象  鼠标,button,winform 。鼠标点击button对象,触发button的click事件,winform捕获到了事件然后进行了处理。

       含义:button是类,有个click事件,其被点击后,向外部发送了一个消息“我被点击了”,winform说“我来处理”,winform是怎么捕获的呢?连接它们之间的桥梁

其实就是委托。winform通过某一类委托向button的click事件注册自己的方法,当事件发生的时候,方法自然就被调用了。

       举个现实中的例子,可能不太恰当。 当我们开车,发生车祸事件时,交警会到现场进行处理,而不是消防或医生或其他人。其实背后就是一个逻辑,预定义好的,车祸是交警处理,火灾是消防处理,生病是医生处理。

        接下来说异步调用。

  异步调用和回调

        顾名思义,异步即不同步,如何体现不同步呢? .net的实现是采用一个新的线程中执行你的方法(不知道理解的对不对:))

       为什么要异步呢?简单说winform吧,就是不希望主线程被长时间执行的方法所阻塞,造成不好的用户体验,谁也不希望数据加载时窗体一拉,界面卡死,是吧。

      那如何实现异步呢?首先定义委托。你肯定会说为什么?  因为委托类中有begininvoke方法,我猜测begininvoke方法的实现里可能包含了开辟新线程,然后在新线程中执行你的需要异步执行的方法。

     好了,方法开始异步执行了。那我怎么知道它执行完了没有啊?使用回调。

    你要问了回调是什么玩意啊?回调即回过头来调用,那么是谁回过头?begininvoke开辟的新线程回头调主线程的方法。那目的呢?通知主线程“我执行结束了你看着办”

   那么怎么实现呢?我的方法是委托和事件。异步调用的方法在执行完成后触发一个完成的事件,这个事件定义在winform中,谁来处理呢?winform中的方法。

    接下来,贴上我的代码。例子比较简单:)

    NET中级课--浅谈委托,事件,异步调用,回调等概念

   

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public delegate void ProcessHandler(int i);
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //process();主线程直接调用会阻塞
            this.button1.Text = "加载中。。。。";
            new System.Threading.Thread(new System.Threading.ThreadStart(process)).Start();
            this.button1.Text="加载完成";
        }

        private void process()
        {
            ; i < ; i++)
            {
                System.Threading.Thread.Sleep();
                setlist(i);
            }
        }

        private void setlist(int i)
        {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new ProcessHandler(setlist), new object[] { i });
                return;
            }
            this.listBox1.Items.Add(i);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show("sssss");
        }

        private void process(ref bool isfinished)
        {
            ; i < ; i++)
            {
                System.Threading.Thread.Sleep();
                setlist(i);
            }
            isfinished = true;
            finished(ref isfinished);
        }

        public delegate void HeadProcess(ref bool isfinish);
        public event HeadProcess finished;
        private void button3_Click(object sender, EventArgs e)
        {
            this.button3.Text = "加载中。。。。";
            bool isfinished = false;
            this.listBox1.Items.Clear();
            HeadProcess hp = new HeadProcess(this.process);
            IAsyncResult ia = hp.BeginInvoke(ref isfinished, null, null);            finished += this.setstatus;//注册完成事件的处理方法

        }
        private void setstatus(ref bool isfinished)
        {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new HeadProcess(this.setstatus),new object[]{isfinished});
                return;
            }

            if (isfinished)
            {
                this.button3.Text = "加载完成";
                MessageBox.Show("加载完成");
            }
            else
            {
                this.button3.Text = "加载失败";
            }
        }

    }
}

第一次写文,比较粗糙,大家凑合看看, 以上解释均为我个人的理解,如有不对,请大家吐槽。:)