浅谈AsyncState与AsyncDelegate使用的异同

对于AsyncState来说,其MSDN的解释为:得到BeginInvoke方法的最后一个参数。而对于AsyncDelegate来说,其MSDN的解释为:得到异步调用的委托对象。也就是异步调用的委托源。

对于委托的异步调用来说,其BeginInvoke函数无非包括以下内容,BeginInvoke(调用参数,回调函数,Object对象)

如果想利用AsyncState来还原对象的话,这里的Object对象必须是源委托;如果利用AsyncDelegate的话,这里可以为空,可以为源委托。具体区别请看下面的例子:

复制代码
//AsyncState方式还原委托对象
chatDelegate.BeginInvoke(this, e, new AsyncCallback((iar) =>
{

     ChatDelegate thisDelegate = (ChatDelegate)iar.AsyncState;
     thisDelegate.EndInvoke(iar);
}), chatDelegate);

 //AsyncDelegate方式还原委托对象
chatDelegate.BeginInvoke(this, e, new AsyncCallback((iar) =>
{
     AsyncResult ar = (AsyncResult)iar;
     ChatDelegate thisDelegate = (ChatDelegate)ar.AsyncDelegate;
     thisDelegate.EndInvoke(iar);
}), null);
复制代码

 

可以看到,当利用AsyncState时候,最后一个对象必须为源委托;当利用AsyncDelegate的时候,最后一个对象可以为null.

全部代码如下:

复制代码
using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private delegate void ChatDelegate(object sender, MyEventArgs e);
        private static event ChatDelegate ChatEvent;

        private void Form1_Load(object sender, EventArgs e)
        {
            MyEventArgs myEventArgs = new MyEventArgs();
            myEventArgs.Message = "this is test args";
           
            ChatEvent+=new ChatDelegate(Form1_ChatEvent);

            ThrowEvent(myEventArgs);
        }

        private void Form1_ChatEvent(object sender, MyEventArgs e)
        {
            MessageBox.Show(e.Message);
        }

        private void ThrowEvent(MyEventArgs e)
        {
            ChatDelegate tempDelegate = ChatEvent;
            if(tempDelegate != null)
            {
                foreach (ChatDelegate chatDelegate in tempDelegate.GetInvocationList())
                {
                    //AsyncState方式还原委托对象
                    chatDelegate.BeginInvoke(this, e, new AsyncCallback((iar) =>
                    {
                        ChatDelegate thisDelegate = (ChatDelegate)iar.AsyncState;
                        thisDelegate.EndInvoke(iar);
                    }), chatDelegate);

                    //AsyncDelegate方式还原委托对象
                    //chatDelegate.BeginInvoke(this, e, new AsyncCallback((iar) =>
                    //{
                    //    AsyncResult ar = (AsyncResult)iar;
                    //    ChatDelegate thisDelegate = (ChatDelegate)ar.AsyncDelegate;
                    //    thisDelegate.EndInvoke(iar);
                    //}), null);
                }
            }
        }
    }

    public class MyEventArgs : EventArgs
    {
        public string Message { get; set; }
    }
}
复制代码

转载于:https://www.cnblogs.com/wenzhongqigz/archive/2012/11/26/2789250.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值