本文整理汇总了C#中MessageBus.Dispose方法的典型用法代码示例。如果您正苦于以下问题:C# MessageBus.Dispose方法的具体用法?C# MessageBus.Dispose怎么用?C# MessageBus.Dispose使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MessageBus
的用法示例。
在下文中一共展示了MessageBus.Dispose方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Run
public static IDisposable Run(int connections, int senders, string payload, int messageBufferSize = 10)
{
var resolver = new DefaultDependencyResolver();
var bus = new MessageBus(resolver);
var countDown = new CountdownEvent(senders);
var subscriptions = new List<IDisposable>();
var cancellationTokenSource = new CancellationTokenSource();
// Initialize performance counters for this run
Utility.InitializePerformanceCounters(resolver, cancellationTokenSource.Token);
for (int i = 0; i < connections; i++)
{
string identity = i.ToString();
var subscriber = new Subscriber(identity, new[] { "a", "b", "c" });
IDisposable subscription = bus.Subscribe(subscriber,
cursor: null,
callback: _ => TaskAsyncHelper.True,
maxMessages: messageBufferSize);
subscriptions.Add(subscription);
}
for (var i = 0; i < senders; i++)
{
ThreadPool.QueueUserWorkItem(_ =>
{
while (!cancellationTokenSource.IsCancellationRequested)
{
string source = i.ToString();
bus.Publish(source, "a", payload);
}
countDown.Signal();
});
}
return new DisposableAction(() =>
{
cancellationTokenSource.Cancel();
// Wait for all senders to stop
countDown.Wait(TimeSpan.FromMilliseconds(1000 * senders));
// Shut the bus down and wait for workers to stop
bus.Dispose();
// Dispose of all the subscriptions
subscriptions.ForEach(s => s.Dispose());
});
}
示例2: DisposingBusShutsWorkersDown
public void DisposingBusShutsWorkersDown()
{
var dr = new DefaultDependencyResolver();
var bus = new MessageBus(dr);
var subscriber = new TestSubscriber(new[] { "key" });
var wh = new ManualResetEventSlim(initialState: false);
IDisposable subscription = null;
try
{
subscription = bus.Subscribe(subscriber, null, result =>
{
if (!result.Terminal)
{
var m = result.GetMessages().Single();
Assert.Equal("key", m.Key);
Assert.Equal("value", m.Value);
wh.Set();
return TaskAsyncHelper.True;
}
return TaskAsyncHelper.False;
}, 10);
bus.Publish("test", "key", "value").Wait();
Assert.True(wh.Wait(TimeSpan.FromSeconds(5)));
}
finally
{
if (subscription != null)
{
subscription.Dispose();
}
Assert.Equal(bus.AllocatedWorkers, 1);
bus.Dispose();
Assert.Equal(bus.AllocatedWorkers, 0);
}
}