【问题标题】:Callback inside a callback C#回调 C# 中的回调
【发布时间】:2012-09-05 22:56:56
【问题描述】:

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

ClientSubscriber.cs

/// <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

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;
    }
}

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

【问题讨论】:

    标签: c# .net delegates callback


    【解决方案1】:

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

    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;
        }
    }
    

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

    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;
        }
    }
    

    【讨论】:

    • 那行得通,谢谢……原来订阅提供了一个我能够通过的对象类型的参数。我最终使用了一个委托,但我可以看到它在哪里工作是一样的。再次感谢!
    猜你喜欢
    • 2010-10-14
    • 1970-01-01
    • 2013-12-27
    • 1970-01-01
    • 1970-01-01
    • 2011-10-16
    • 1970-01-01
    • 2020-06-23
    相关资源
    最近更新 更多