简体   繁体   English

WPF中的Actor系统生存期

[英]Actor system lifetime in WPF

In the past, using Console apps, I've kept Akka.NET actor systems alive like this: 在过去,使用Console应用程序,我保持Akka.NET actor系统活着如下:

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? 没有Console.ReadKey(),什么是管理WPF应用程序的actor系统生命周期的正确方法?

( bonus points: I've heard that Shutdown and AwaitTermination are obsolete, but I'm not sure of the new best practice ) 奖励积分:我听说Shutdown和AwaitTermination已经过时,但我不确定新的最佳做法

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. 目前,如果您希望关闭ActorSystem您应该使用ActorSystem.Terminate() ,它返回一个在关闭完成后完成的Task

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. 还有在ActorSystem,另一个属性WhenTerminated ,它看起来像它只存在,这样,如果你需要它,而不告诉系统你终止可以访问结束任务。

In your example, Console.ReadKey() is used to block and prevent the process from ending, which isn't necessary in a WPF app. 在您的示例中, Console.ReadKey()用于阻止和阻止进程结束,这在WPF应用程序中是不必要的。 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. 如果您不关心系统状态,可以让wpf应用程序关闭,或者如果需要系统干净地关闭,则终止并等待终止任务完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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