简体   繁体   中英

Actor system lifetime in WPF

In the past, using Console apps, I've kept Akka.NET actor systems alive like this:

class Program {
    static void Main(string[] args) {

        using (var actorSystem = ActorSystem.Create("ExampleSystem")) {
            var exampleActor = actorSystem.ActorOf(Props.Create(() => new ExampleActor()), name: "Example");
            Console.WriteLine("Akka.NET ActorSystem is now running, press any key to shut down");

            Console.ReadKey();
            actorSystem.Shutdown();
            actorSystem.AwaitTermination(timeout: TimeSpan.FromSeconds(5));
        }
    }
}

Without the Console.ReadKey(), what's the right way to manage the lifetime of an actor system for a WPF application?

( bonus points: I've heard that Shutdown and AwaitTermination are obsolete, but I'm not sure of the new best practice )

Currently, if you wish to shut down an ActorSystem you should use ActorSystem.Terminate() , which returns a Task that completes once the shutdown has finished.

There is also another property on the ActorSystem, WhenTerminated , which looks like it only exists so that you can get access to the termination task if you need it, without telling the system to terminate.

In your example, Console.ReadKey() is used to block and prevent the process from ending, which isn't necessary in a WPF app. You can either just let your wpf application close if you don't care about the state of the system, or terminate and wait for the termination task to complete if you need the system to shutdown cleanly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM