//声明一个委托
public delegate string AddHandler(int a, string b);//你要多少个参数在这里定义,此例只有两个参数

void 调用异步()
{

            AddHandler handler = new AddHandler(Add);

//用BeginInvoke开始异步操作 这里的 1,"字符串",这两个参数就是对应(int a, string b)
            IAsyncResult result = handler.BeginInvoke(1, "字符串", new AsyncCallback(AddComplete),"这里是随便你写个什么对象");

        }

        static int Add(int a, string b)
        {
           //这里是处理的的函数
            Thread.Sleep(3000);//模拟耗时操作
            return b+a.ToString();
        }

static void AddComplete(IAsyncResult result)
        {
            AddHandler handler = (AddHandler)((AsyncResult)result).AsyncDelegate;
           
//调试一下就会明白了
            MessageBox.Show(handler.EndInvoke(result));
            MessageBox.Show(result.AsyncState);
        }

相关文章:

  • 2021-08-12
  • 2021-06-07
  • 2022-12-23
  • 2022-12-23
  • 2021-09-29
  • 2021-06-01
  • 2021-10-03
  • 2021-06-04
猜你喜欢
  • 2022-12-23
  • 2022-02-08
  • 2021-08-22
  • 2022-12-23
  • 2022-12-23
  • 2022-02-08
  • 2022-12-23
相关资源
相似解决方案