コード例 #1
0
        public Task ReceiveAsync(IContext context)
        {
            if (context.Message is Started)
            {
                var pid = context.Spawn(Actor.FromProducer(() => new ScheduleGreetActor()));
                scheduler.ScheduleTellOnce(TimeSpan.FromMilliseconds(100), context.Self, new SimpleMessage("test 1"))
                .ScheduleTellOnce(TimeSpan.FromMilliseconds(200), context.Self, new SimpleMessage("test 2"))
                .ScheduleTellOnce(TimeSpan.FromMilliseconds(300), context.Self, new SimpleMessage("test 3"))
                .ScheduleTellOnce(TimeSpan.FromMilliseconds(400), context.Self, new SimpleMessage("test 4"))
                .ScheduleTellOnce(TimeSpan.FromMilliseconds(500), context.Self, new SimpleMessage("test 5"))
                .ScheduleRequestOnce(TimeSpan.FromSeconds(1), context.Self, pid, new Greet("Daniel"))
                .ScheduleTellOnce(TimeSpan.FromSeconds(5), context.Self, new Hello());
            }
            else if (context.Message is Hello)
            {
                var hl = context.Message as Hello;
                Console.WriteLine($"Hello Once,let's give you a hick");
                scheduler.ScheduleTellRepeatedly(TimeSpan.FromSeconds(3), TimeSpan.FromMilliseconds(500), context.Self, new HickUp(), out timer);
            }
            else if (context.Message is HickUp)
            {
                var hu = context.Message as HickUp;
                counter++;
                Console.WriteLine("Hello!");

                if (counter == 5)
                {
                    timer.Cancel();
                    context.Self.Tell(new AbortHickUp());
                }
            }
            else if (context.Message is AbortHickUp)
            {
                Console.WriteLine($"Aborted hickup after {counter} times");
                Console.WriteLine($"All this was scheduled calls, have fun!");
            }
            else if (context.Message is Greet)
            {
                var msg = context.Message as Greet;
                Console.WriteLine($"Thanks {msg.Who}");
            }
            else if (context.Message is SimpleMessage)
            {
                var sm = context.Message as SimpleMessage;
                Console.WriteLine(sm.Msg);
            }
            return(Actor.Done);
        }
コード例 #2
0
        public Task ReceiveAsync(IContext context)
        {
            switch (context.Message)
            {
            case Started _:

                var pid = context.Spawn(Props.FromProducer(() => new ScheduleGreetActor()));

                _scheduler
                .ScheduleTellOnce(TimeSpan.FromMilliseconds(100), context.Self, new SimpleMessage("test 1"))
                .ScheduleTellOnce(TimeSpan.FromMilliseconds(200), context.Self, new SimpleMessage("test 2"))
                .ScheduleTellOnce(TimeSpan.FromMilliseconds(300), context.Self, new SimpleMessage("test 3"))
                .ScheduleTellOnce(TimeSpan.FromMilliseconds(400), context.Self, new SimpleMessage("test 4"))
                .ScheduleTellOnce(TimeSpan.FromMilliseconds(500), context.Self, new SimpleMessage("test 5"))
                .ScheduleRequestOnce(TimeSpan.FromSeconds(1), context.Self, pid, new Greet("Daniel"))
                .ScheduleTellOnce(TimeSpan.FromSeconds(5), context.Self, new Hello());

                break;

            case Hello _:

                Console.WriteLine($"Hello Once, let's give you a hickup every 0.5 second starting in 3 seconds!");

                _scheduler.ScheduleTellRepeatedly(TimeSpan.FromSeconds(3), TimeSpan.FromMilliseconds(500), context.Self, new HickUp(), out _timer);

                break;

            case HickUp _:

                _counter++;

                Console.WriteLine("Hello!");

                if (_counter == 5)
                {
                    _timer.Cancel();

                    context.Send(context.Self, new AbortHickUp());
                }

                break;

            case AbortHickUp _:

                Console.WriteLine($"Aborted hickup after {_counter} times");

                Console.WriteLine("All this was scheduled calls, have fun!");

                break;

            case Greet msg:

                Console.WriteLine($"Thanks {msg.Who}");

                break;

            case SimpleMessage sm:

                Console.WriteLine(sm.Msg);

                break;
            }

            return(Actor.Done);
        }