首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >C#中的回调

C#中的回调
EN

Stack Overflow用户
提问于 2009-03-20 20:01:22
回答 5查看 41K关注 0票数 17

我想有一个库,里面有一个函数,它接受一个对象作为它的参数。

有了这个对象,当X完成时,我希望能够调用指定的函数。将被调用的函数将由调用者指定,X将由库完成和监视。

我该怎么做呢?

作为参考,我使用C#和.NET 3.5

EN

Stack Overflow用户

发布于 2015-11-05 05:45:39

您可以使用C#.NET中提供的System.Action来实现回调函数。请查看此示例:

代码语言:javascript
运行
复制
    //Say you are calling some FUNC1 that has the tight while loop and you need to 
    //get updates on what percentage the updates have been done.
    private void ExecuteUpdates()
    {
        Func1(Info => { lblUpdInfo.Text = Info; });
    }

    //Now Func1 would keep calling back the Action specified in the argument
    //This System.Action can be returned for any type by passing the Type as the template.
    //This example is returning string.
    private void Func1(System.Action<string> UpdateInfo)
    {
        int nCount = 0;
        while (nCount < 100)
        {
            nCount++;
            if (UpdateInfo != null) UpdateInfo("Counter: " + nCount.ToString());
            //System.Threading.Thread.Sleep(1000);
        }
    }
票数 -1
EN
查看全部 5 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/667742

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档