zoukankan      html  css  js  c++  java
  • C# 事件 委托 同步 异步

    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;
    using System.Configuration;
    using System.IO.Pipes;
    using System.Threading;
    namespace WindowsFormsApplication1
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    test += new Testdelegate(test1);//事件赋值
    test+=new Testdelegate( test2);//事件赋值
    }
    string msg = "";
    string test1(DelegateEventArgs e)
    {
    for (int i = 1; i <= 100;i++ )
    {
    msg += i.ToString() + ",";
    Thread.Sleep(100);
    }
    return "";
    }
    string test2(DelegateEventArgs e)
    {
    for (int i = 101; i <= 200;i++ )
    {
    msg += i.ToString() + ",";
    Thread.Sleep(100);
    }
    return "";
    }
    //定义事件
    private event Testdelegate test;
    private void button2_Click(object sender, EventArgs e)
    {
    msg = "";
    if (test!=null)
    {
    //异步执行事件里的委托链
    foreach (Testdelegate t in test.GetInvocationList())
    {
    DelegateEventArgs a = new DelegateEventArgs("");
    t.BeginInvoke(a, null, null);
    }
     
    //同步执行事件里的委托
    //DelegateEventArgs b = new DelegateEventArgs("");
    //test(b);
    }
    }
    private void button3_Click(object sender, EventArgs e)
    {
    MessageBox.Show(msg);
    }
    }

    //定义委托参数 

    public class DelegateEventArgs : EventArgs
    {
    string str_response;
    public DelegateEventArgs(string ls_res)
    {
    this.str_response = ls_res;
    }
    }

    //定义委托规格 

    public delegate string Testdelegate(DelegateEventArgs e);
    }
  • 相关阅读:
    看完动画你还会不懂 快速排序么
    看动画轻松理解时间复杂度(一)
    LeetCode Animation 题目图解汇总(持续更新中...)
    分而治之,归并排序的动画演示
    在Object-C中学习数据结构与算法之排序算法
    iOS面试准备之思维导图
    .net core跨域设置
    .NET Core WebAPI Swagger使用
    vue-resource emulateJSON的作用
    百度地图API简单初始化
  • 原文地址:https://www.cnblogs.com/kuailewangzi1212/p/2223422.html
Copyright © 2011-2022 走看看