C#委托学习之回调

    委托:

    在c#中定义为:委托就像一个函数的指针,在程序运行时可以使用它们来调用不同的函数。

    我的理解就是一个方法,但是需要传入的参数类型与普通参数不同,需要传入另一个方法作为参数。

  委托的使用:

    首先定义一个委托类:public delegate int MethodDel();

    然后实例化一个委托:public MethodDel addMethod = new MethodDel(Add);其中Add();方法的返回值与参数必须与定义的委托类的返回值和参数相同。

    使用时可以添加委托链:addMethod += Add2;同理也可以进行减等操作

委托的调用:

    委托的调用分为同步调用、异步调用和异步回调

    同步调用:即使用Invoke();方法。同步调用又称阻塞调用,会阻塞当前线程。

    异步调用:一步调用不会阻塞线程,而是把线程放到线程池中,程序会继续执行。通过BeginInvoke();EndInvoke();来实现。

但是如果运行到EndInvoke();时,调用没有执行完毕,依然会阻塞线程。

    异步回调:当调用结束时会自动调用回到函数,解决了为等待调用结果,而让线程依旧被阻塞的局面。

测试代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Collections;
using System.Runtime.Remoting.Messaging;

namespace StudyTest
{
    class Program
    {
        public delegate int MethodDel();
        public static MethodDel addMethod = new MethodDel(Add);
        static void Main(string[] args)
        {
            Console.ReadKey();
            addMethod.Invoke();
            Console.WriteLine("同步调用完成");

            Console.ReadKey();

            IAsyncResult resule = addMethod.BeginInvoke(null, null);
            Console.WriteLine("做别的事情");
            Console.ReadKey();//代表做别的事情,再次停顿5秒以上
            addMethod.EndInvoke(resule);
            Console.WriteLine("异步调用完成");

            Console.ReadKey();

            IAsyncResult resulet = addMethod.BeginInvoke(new AsyncCallback(回调函数), "AsycState:OK");
            Console.WriteLine("做别的事情");
            Console.ReadKey();
        }
        public static int Add()
        {
            Thread.Sleep(5000);//代表正在调用,停止5秒
            return 3;
        }
        public static void 回调函数(IAsyncResult result)
        {
            addMethod= (MethodDel)((AsyncResult)result).AsyncDelegate;
            Console.WriteLine(addMethod.EndInvoke(result));
            Console.WriteLine(result.AsyncState);
        }
    }
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#委托中,回调函数指的是将一个函数作为参数传递给另一个函数,并在需要的时候调用该函数。回调函数在执行完一些操作后,可以重新调用不属于它的某些东西。 举个例子,假设有两个类Text1和Text2,Text1类中有一个静态方法Begin,它接受一个委托类型的参数makesum add。在Begin方法中,我们可以看到在调用add时传入了a和b两个参数。而这个add方法实际上是Text2类中的静态方法with,它接受a和b两个参数,并在其中调用了Text1类中的shenm方法。这里with方法就是一个回调函数。 在使用委托时,我们可以通过实例化一个委托对象并传入对应的方法作为参数,然后在需要的时候通过委托对象调用回调函数。这种方式可以让我们在调用方法时动态地指定不同的操作逻辑,提高了代码的灵活性和可重用性。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [C#回调函数](https://blog.csdn.net/lfanyize/article/details/103150584)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [C# 中的委托事件和回调函数](https://blog.csdn.net/weixin_42105843/article/details/121243956)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值