I have an IPClient actor which manages/owns a connection which is expensive to open/close.
Before the actor finishes handling a message, I'd like to peek at the top of the actor's Mailbox to check if there is another message pending. - If the actor has got more work, leave the connection open. - If the actor has emptied it's mailbox, close the connection.
Here's the gist of what i'm thinking:
public void Handle(PollDevice message)
{
if (!_client.IsConnected)
_client.Connect();
var results = _client.GetData()
var actorHasMoreWork = Context.Dispatcher.Mailboxes.???
if (!actorHasMoreWork)
_client.Disconnect();
Sender.Tell(true);
}
Is this possible? Is it the best way?