Hugo Rocha Hugo Rocha - 1 month ago 24
C# Question

Akka.net how do I stop an actor without waiting for the current message to finish?

Was wondering if I could stop an actor without necessarily waiting for the current message to be processed, or just cancel it.

Here explains the ways of doing it

https://petabridge.com/blog/how-to-stop-an-actor-akkadotnet/

But they all wait for the current message to be processed.

Stop() the actor:


stops the actor immediately after it finishes
processing the current message.


Kill the actor:


this throws an ActorKilledException which will be logged and handled. The actor will
stop immediately after it finishes processing the current message.


Send the actor a PoisonPill:


the actor will finish processing the
messages currently in its mailbox, and then Stop.

Answer

I would question why you have a long running process inside the actors message receive. It would be better practice to place any code that may take some time and that you want to be able to cancel into a Task within the receive of the message.

The actor would then be freed up and be able to accept the message from one of the stop methods you list above, and terminate the task accordingly.

Additionally, point 4 on the following page shows some example code https://petabridge.com/blog/top-7-akkadotnet-stumbling-blocks/