C# 如何在Akka.net中只杀死一名演员
我有一个演员和一个实例,然后不止一次。当一个参与者试图杀死一个指定的ActorRef时,我的所有参与者都会被杀死 这是我的演员:C# 如何在Akka.net中只杀死一名演员,c#,.net,akka.net,C#,.net,Akka.net,我有一个演员和一个实例,然后不止一次。当一个参与者试图杀死一个指定的ActorRef时,我的所有参与者都会被杀死 这是我的演员: public class MyActor: ReceiveActor { public MyActor() { Receive<long>(id => Handler(id)); } public void Handler(long id) { Console.WriteLine(id)
public class MyActor: ReceiveActor {
public MyActor() {
Receive<long>(id => Handler(id));
}
public void Handler(long id) {
Console.WriteLine(id)
}
}
但是,它正在杀死我所有的演员。
怎么了
--编辑
public IActorRef ScheduleDelay(时间跨度初始延迟、时间跨度间隔、对象消息){
var actorRef=ActorSystem.ActorOf(ActorSystem.DI().Props(typeof(T));
ActorSystem.ScheduleTell(initialDelay、interval、actorRef、message、ActorRefs.NoSender)
返回actorRef;
}
var actor1=计划时间(TimeSpan.FromSeconds(5)、TimeSpan.FromSeconds(30)、1);
var actor2=调度时间(时间跨度从秒(5),时间跨度从秒(30),2);
var actor3=调度程序(时间跨度从秒(5)、时间跨度从秒(30)、3);
var actor4=调度程序(时间跨度从秒(5),时间跨度从秒(30),4);
var-actor5=调度程序(TimeSpan.FromSeconds(5),TimeSpan.FromSeconds(30),5);
actor3.Tell(Kill.Instance);
如果不在实例化参与者的地方查看代码,就很难准确判断发生了什么。但是,如果您的命令处理程序按现在的方式设置,我猜这是因为您只有一个演员。您可以启动同一参与者的多个实例,只要它们都具有唯一的名称/引用。下面的代码将杀死其中一个参与者,但保持其余参与者的生命
// Spin up five actors, each with a unique name/reference
// Fill in Props as needed
var actor1 = Context.ActorOf(MyActor.Props(), "MyActor_1");
var actor2 = Context.ActorOf(MyActor.Props(), "MyActor_2");
var actor3 = Context.ActorOf(MyActor.Props(), "MyActor_3");
var actor4 = Context.ActorOf(MyActor.Props(), "MyActor_4");
var actor5 = Context.ActorOf(MyActor.Props(), "MyActor_5");
// Kill the third actor by sending it a poison pill
actor3.Tell(PoisonPill.Instance);
// Send a message to any of the other actors
actor4.Tell((long)123); // Will write 123 to the console
注意:在上面的代码中,ActorOf
方法的第二个(可选)参数只是一个用户友好的名称,用于为您创建的参与者命名。如果您选择不指定演员的名字,Akka将自动为演员生成一个唯一的名字
更新
您的最新代码已正确设置为仅杀死您指定的参与者(
actor3
)。我复制/粘贴了您的代码,运行了它,并验证了其他四个参与者仍在运行。我确实注意到您的MyActor
类正在侦听一个long
,但您正在向它传递一个int
。这可能会使其他参与者看起来好像被关闭了,而实际上他们并没有关闭。请告诉我如何实例化该参与者。有可能你正在杀死“家长”演员,而所有其他演员都是siblings@monstertjie_za描述被省略了。你能检查一下吗?嗨,我更改了描述,你能检查一下吗?@ThiagoMaia,你不能从void
方法返回东西。另外,它是Kill.Instance
,而不是Kill.Instantiate
。修复你的代码并试着运行它看看它是否有效。@ThiagoMaia,当你现在运行它时会发生什么?所有参与者都在运行,但当我杀死actor3时。我所有的演员都停止了这只是一个演示代码,好吗?
public IActorRef ScheduleTellRepeatedly<T>(TimeSpan initialDelay, TimeSpan interval, object message) {
var actorRef = ActorSystem.ActorOf(ActorSystem.DI().Props(typeof(T)));
ActorSystem.ScheduleTellRepeatedly(initialDelay, interval, actorRef, message, ActorRefs.NoSender)
return actorRef;
}
var actor1 = ScheduleTellRepeatedly<MyActor>(TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(30), 1);
var actor2 = ScheduleTellRepeatedly<MyActor>(TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(30), 2);
var actor3 = ScheduleTellRepeatedly<MyActor>(TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(30), 3);
var actor4 = ScheduleTellRepeatedly<MyActor>(TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(30), 4);
var actor5 = ScheduleTellRepeatedly<MyActor>(TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(30), 5);
actor3.Tell(Kill.Instance);
// Spin up five actors, each with a unique name/reference
// Fill in Props as needed
var actor1 = Context.ActorOf(MyActor.Props(), "MyActor_1");
var actor2 = Context.ActorOf(MyActor.Props(), "MyActor_2");
var actor3 = Context.ActorOf(MyActor.Props(), "MyActor_3");
var actor4 = Context.ActorOf(MyActor.Props(), "MyActor_4");
var actor5 = Context.ActorOf(MyActor.Props(), "MyActor_5");
// Kill the third actor by sending it a poison pill
actor3.Tell(PoisonPill.Instance);
// Send a message to any of the other actors
actor4.Tell((long)123); // Will write 123 to the console