Subscribe(ISharedPubSubConnection,String,Action<PubSubMessage>) Method
- Last UpdatedApr 08, 2026
- 1 minute read
Subscribes to the indicated topic using an asynchronous callback with infinite timeout.
[Extension()]
[CLSCompliant(false)]
public static IPubSubSubscription Subscribe(
ISharedPubSubConnection connection,
string topic,
Action<PubSubMessage> callback
)
[Extension()]
[CLSCompliant(false)]
public:
static IPubSubSubscription^ Subscribe(
ISharedPubSubConnection^ connection,
String^ topic,
Action<PubSubMessage^>^ callback
)
Parameters
- connection
- The ISharedPubSubConnection to extend.
- topic
- The topic string to subscribe to.
- callback
- The callback to be invoked when a new PubSub message has been received.
| Exception | Description |
|---|---|
| PubSubConnection.BadCallException | Previous calls to blocking subscribe were made. |
| PubSubConnection.BadTopicException | Topic string is invalid. |
| PubSubConnection.NotInitException | Initialize was not called. |
| PubSubConnection.NoMemException | Memory allocation failed. |
Note: This method is capable of dealing with the serialization required by System.Windows.Forms.Control and all descendants. That is, if the subscriber is a Form Control, then the System.Windows.Forms.Control.Invoke(System.Delegate,System.Object[]) method will be used to execute the callback in a serialized manner. If the Subscriber is not a Forms Control, then the callback is made preemptively.
using (var connection = new PubSubConnection())
using (var subscription = connection.Subscribe("mySystem.MyTopic", TimeSpan.FromSeconds(30), withIntegrity: true, callback: (message) => { /* Process Message */ }))
{
// Wait for shutdown condition.
}
using (var connection = new PubSubConnection())
using (var subscription = connection.Subscribe("mySystem.MyTopic", TimeSpan.FromSeconds(30), withIntegrity: true, callback: (message) => { /* Process Message */ }))
{
// Wait for shutdown condition.
}