zoukankan      html  css  js  c++  java
  • IAsyncResult异步执行回调方法的简单使用

    private void Delcalmethod(MethodInvoker method)
    {
        if (InvokeRequired)
        Invoke(method);
        else
        method();
    }

    private delegate int getMax(); //委托

    getMax delegateMax;

    private int mathbig() //方法
    {
      List<int> list = new List<int>() { 1, 100, 2, 3, 22, 99, 1000, 23, 4000, 44, 300, 3000 };
      int temp = 0; //中间介质
      var arr = list.Select(x => x).ToArray();
      for (int i = arr.Length - 1; i >0; i--)
      {
        for (int j = 0; j < i; j++)
        {

          if (arr[j] > arr[j + 1])
          {
            temp = arr[j];
            arr[j] = arr[j + 1];
            arr[j + 1] = temp;
          }
        }
      }
      return arr[arr.Length - 1];

    }

    ********************** 方法调用*****************************

    private void button1_Click(object sender, EventArgs e)
    {

        delegateMax = mathbig;
        IAsyncResult ar = delegateMax.BeginInvoke(CompleteCallback, "异步执行完毕");
        while (!ar.IsCompleted)
        {
          txtAsyncResult.AppendText("继续算...");
        }

        txtAsyncResult.AppendText("计算其他方法");
        txtAsyncResult.AppendText("等待主方法计算完毕");
    }

    private void CompleteCallback(IAsyncResult iar)
    {
        Delcalmethod(() =>
        {
          int result = delegateMax.EndInvoke(iar);
          txtAsyncResult.AppendText(result.ToString());
          MessageBox.Show(iar.AsyncState as string); //此方法执行异步状态
        });
    }

  • 相关阅读:
    理解爬虫原理
    中文词频统计与词云生成
    复合数据类型,英文词频统计
    字符串操作、文件操作,英文词频统计预处理
    了解大数据的特点、来源与数据呈现方式
    为Bootstrap模态对话框添加拖拽移动功能
    前端进阶学习笔记
    前端基础学习笔记
    MySQL学习笔记(模块二)
    MySQL学习笔记(模块一)
  • 原文地址:https://www.cnblogs.com/it888/p/3541935.html
Copyright © 2011-2022 走看看