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

回调C#中的回调
EN

Stack Overflow用户
提问于 2012-09-05 22:56:56
回答 1查看 300关注 0票数 1

尝试设置订阅服务的服务器工具,并在通过回调发生事件时通知我。我让订阅部分正常工作,当一个事件被触发时,它会命中回调,但我在一个库中有这个部分,并希望将它放到主项目中。我曾想过用委托来做这件事,但却想不出语法。

ClientSubscriber.cs

代码语言:javascript
复制
/// <summary>
/// Used by Clients to subscribe to a particular queue
/// <param name="type">Subscription type being subscribed to by client</param>
/// <returns></returns>
public bool Subscribe(string type,)
{
    bool IsSubscribed = false;
    try
    {
        switch (type)
        {
            case "Elements":
            {
                logger.Info("Subscribing toPublisher");
                Subscriber.SubscribeToElements(ElementResponse);
                logger.Info("Subscription Completed");
                IsSubscribed = true;
                break;
            }
        }
    }    
    catch (Exception ex)
    {
        logger.Error(ex);
    }

    return IsSubscribed;
}


public void ElementResponse(Element element, SubscriptionEvent.ActionType eventActionType)
{
    try
    {
        //  stuff to Do
        // Need to Callback to main application
    }
    catch (Exception ex)
    {
        logger.Error(ex);
        throw;
    }
}

Program.cs

代码语言:javascript
复制
static void Main(string[] args)
{
    SubscriberClient client = new SubscriberClient();
    client.Subscribe("SlateQueueElements");

    while (true)
    {
        ConsoleKeyInfo keyInfo = Console.ReadKey(true);

        if (keyInfo.Key == ConsoleKey.X)
            break;
    }
}

那么,如何将元素响应信息返回到主项目?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-09-05 23:39:15

如果您想要一个回调,那么您需要指定一个作为参数。我做了一些假设,但这应该是可行的:

代码语言:javascript
复制
public bool Subscribe(string type, Action callback)
{
    bool IsSubscribed = false;
    try
    {
        switch (type)
        {
            case "Elements":
            {
                logger.Info("Subscribing toPublisher");
                Subscriber.SubscribeToElements((e,t) => ElementResponse(e,t,callback));
                logger.Info("Subscription Completed");
                IsSubscribed = true;
                break;
            }
        }
    }    
    catch (Exception ex)
    {
        logger.Error(ex);
    }

    return IsSubscribed;
}

public void ElementResponse(Element element, SubscriptionEvent.ActionType eventActionType, Action callback)
{
    try
    {
        //  stuff to Do
        callback();
    }
    catch (Exception ex)
    {
        logger.Error(ex);
        throw;
    }
}

我使用了一个动作委托,但是只要您有方法调用它,任何东西都可以放在它的位置上。程序代码将是:

代码语言:javascript
复制
static void Main(string[] args)
{
    SubscriberClient client = new SubscriberClient();
    client.Subscribe("SlateQueueElements", () => 
    {
       Console.WriteLine("Calling back...");
    });

    while (true)
    {
        ConsoleKeyInfo keyInfo = Console.ReadKey(true);

        if (keyInfo.Key == ConsoleKey.X)
            break;
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12290894

复制
相关文章

相似问题

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