Esempio n. 1
0
        public static void ThrottlingIncomingMessages()
        {
            int            msgCount        = 0;
            AsyncSemaphore messageThrottle = new AsyncSemaphore(2, 2);                                                          // allow 2 at a time so we dont bombard the thread pool

            Telegraph.Instance.Register(new LocalQueueOperator(new LocalSwitchboard(LocalConcurrencyType.ActorsOnThreadPool))); // performs a reset.

            Telegraph.Instance.Register <string>(message => {
                msgCount++;
                Console.WriteLine("ThrottlingIncomingMessages: " + message);
                System.Threading.Thread.Sleep(100);
            });

            for (int i = 0; i < 100; ++i)
            {
                messageThrottle.Queue(() => {
                    Task question = Telegraph.Instance.Ask("foo");
                    question.Wait();
                });
            }

            while (0 != messageThrottle.WaitingCount)
            {
                System.Threading.Thread.Sleep(1000);
            }

            System.Diagnostics.Debug.Assert(msgCount == 100);
        }