zoukankan      html  css  js  c++  java
  • 事件的三种类型,应用篇

    同步事件
    1.声明事件
    //负责传递消息
    public delegate void MethodCall(string message);
    public static event MethodCall requestdata;

    2.注册事件
    Page_Load事件中注册事件
     requestdata+= new MethodCall(FormDataGridViewDataTable_requestdata);

    3.显式的触发事件
    private void button8_Click(object sender, EventArgs e)
    {
        if (requestdata != null) requestdata("Click Me"+DateTime.Now.ToString());
    }

    4.执行事件
     private void FormDataGridViewDataTable_requestdata(string message)
    {
         Text = message;
    }

    异步事件
    1.声明事件

    public event EventHandler AnalyseFileDataCompleted;
    public void OnAnalyseFileDataCompleted()
    {
         EventHandler handler = (EventHandler)AnalyseFileDataCompleted;
         if (handler != null)
         {
             handler.Invoke(this, null);
          }
    }
    2.注册事件
     AnalyseFileDataCompleted+=new EventHandler(FormDataGridViewDataTable_AnalyseFileDataCompleted);

    3.显式的触发事件
    private void button8_Click(object sender, EventArgs e)
    {
        OnAnalyseFileDataCompleted();
    }
    4.执行事件
    private void FormDataGridViewDataTable_AnalyseFileDataCompleted(object sender,EventArgs e)
    {
        for (int i = 0; i < 12000000; i++)
        {
            Text = "异步事件";
         }
         MessageBox.Show("00");
    }


    泛型异步事件
    1.声明事件
    public event EventHandler<FileAnalysisEventArgs<T>> AnalyseFileDataChanged;
    public void OnAnalyseFileDataChanged(FileAnalysisEventArgs<T> arg)
    {
        EventHandler<FileAnalysisEventArgs<T>> handler = (EventHandler<FileAnalysisEventArgs<T>>)AnalyseFileDataChanged;
        if (handler != null)
        {
            handler.Invoke(this, arg);      //使用异步传送消息
         }
    }

    2.注册事件
    _fileAnalyse.FileEvent.AnalyseFileDataCompleted += new EventHandler<FileAnalysisEventArgs<T>>(FileEvent_AnalyseFileDataCompleted);

    3.显式的触发事件
    private void button8_Click(object sender, EventArgs e)
    {
      _fileAnalyseEvent.OnAnalyseFileDataChanged(new FileAnalysisEventArgs<T> { AnalyseEntity = tmpEty, LineNum = lineNum });

    4.执行事件
    protected void FileEvent_AnalyseFileDataChanged(object sender, FileAnalysisEventArgs<T> e)
    {
      _form.Invoke((MethodInvoker)delegate
       {
                          
        });

  • 相关阅读:
    yolo_to_onnx ValueError: need more tan 1 value to unpack
    yolo_to_onnx killed
    C++ 实现二维矩阵的加减乘等运算
    Leetcode 1013. Partition Array Into Three Parts With Equal Sum
    Leetcode 1014. Best Sightseeing Pair
    Leetcode 121. Best Time to Buy and Sell Stock
    Leetcode 219. Contains Duplicate II
    Leetcode 890. Find and Replace Pattern
    Leetcode 965. Univalued Binary Tree
    Leetcode 700. Search in a Binary Search Tree
  • 原文地址:https://www.cnblogs.com/qiaojun/p/2055744.html
Copyright © 2011-2022 走看看