gpt4 book ai didi

c# - 停止接收来自 SubscriptionClient 的消息

转载 作者:太空狗 更新时间:2023-10-29 21:18:01 25 4
gpt4 key购买 nike

如何停止从设置为事件驱动消息泵的订阅客户端接收消息?我目前有一些代码可以工作,但是当我连续运行两个测试时,它们会第二次中断。我相当确定消息仍在从我创建的第一个实例中的订阅中提取。

http://msdn.microsoft.com/en-us/library/dn130336.aspx

OnMessageOptions options = new OnMessageOptions();
options.AutoComplete = true; // Indicates if the message-pump should call complete on messages after the callback has completed processing.

options.MaxConcurrentCalls = 1; // Indicates the maximum number of concurrent calls to the callback the pump should initiate

options.ExceptionReceived += LogErrors; // Enables you to be notified of any errors encountered by the message pump

// Start receiveing messages
Client.OnMessage((receivedMessage) => // Initiates the message pump and callback is invoked for each message that is received. Calling Close() on the client will stop the pump.

{
// Process the message
Trace.WriteLine("Processing", receivedMessage.SequenceNumber.ToString());
}, options);

最佳答案

你需要做两件事。

首先,调用 subscriptionClient.Close(),这最终将(但不会立即)停止消息泵。

其次,在您的消息收到回调时,检查客户端是否已关闭,如下所示:

if (subscriptionClient.IsClosed)
return;

关于c# - 停止接收来自 SubscriptionClient 的消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17025338/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com