Exemple #1
0
        public static void WaitOnMultipleMessagesToComplete()
        {
            System.Diagnostics.Debug.WriteLine("WaitOnMultipleMessagesToComplete");
            Telegraph.Instance.Register(new LocalQueueOperator(new LocalSwitchboard(LocalConcurrencyType.OneActorPerThread, 20))); // performs a reset.
            string messageStr = "WaitOnMultipleMessagesToComplete.";

            int msgCount = 0;
            LazyInstantiationActor     lzyActor = new LazyInstantiationActor();
            Func <IActorMessage, bool> writeFcn = delegate(IActorMessage s) { ++msgCount;  lzyActor.Print(s); return(true); };

            // this is using the local threadpool concurrency type which will create a new Actor for each message.
            Telegraph.Instance.Register <byte[], DefaultActor>(() => new DefaultActor(writeFcn));

            for (int i = 0; i < 10; ++i)
            {
                Telegraph.Instance.Tell(Encoding.ASCII.GetBytes(messageStr + i.ToString()));
            }

            Task <IActorMessage>[] tasksToWaitOn = new Task <IActorMessage> [20];
            for (int i = 0; i < 20; ++i)
            {
                tasksToWaitOn[i] = Telegraph.Instance.Ask(new ControlMessages.HangUp());
            }

            Task.WaitAll(tasksToWaitOn);
            System.Threading.Thread.Sleep(100); // wait for items in the queue to be processed.
            System.Diagnostics.Debug.Assert(10 == msgCount);
        }
Exemple #2
0
        public static void LazyInstantiation()
        {
            System.Diagnostics.Debug.WriteLine("LazyInstantiation");
            Telegraph.Instance.Register(new LocalQueueOperator()); // performs a reset.
            string messageStr = "LazyInstantiationActor.";

            Telegraph.Instance.Register <byte[]>(message =>
            {
                LazyInstantiationActor la = new LazyInstantiationActor();
                la.Tell(message);
            });

            for (int i = 0; i < 10; ++i)
            {
                Telegraph.Instance.Tell(Encoding.ASCII.GetBytes(messageStr + i.ToString()));
            }

            Telegraph.Instance.MainOperator.WaitTillEmpty(new TimeSpan(0, 0, 20));
        }