当前位置: 首页>>代码示例>>C#>>正文


C# MessageBus.Dispose方法代码示例

本文整理汇总了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());
            });
        }
开发者ID:rustd,项目名称:SignalR,代码行数:51,代码来源:MessageBusRun.cs

示例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);
            }
        }
开发者ID:nonintanon,项目名称:SignalR,代码行数:46,代码来源:MessageBusFacts.cs


注:本文中的MessageBus.Dispose方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。