PostAndReply throws timeout exception for default values (int = 0 for example)
This code fails with TimeoutException MailboxProcessor PostAndReply timed out:
void Main()
{
var printerAgent = MailboxProcessor.Start<Message>(async mb =>
{
var sharedResource = 0;
while (true)
{
var value = await mb.Receive();
value.Dump("Received");
if (value is Fetch f)
{
f.Channel.Reply(sharedResource);
}
}
});
using (printerAgent)
{
printerAgent.DefaultTimeout = int.MaxValue;
printerAgent.PostAndReply<int>(channel => new Fetch(channel)).Dump("Reply");
}
}
// Define other methods and classes here
public abstract class Message
{
}
public class Fetch : Message
{
public Fetch(IReplyChannel<int> channel)
{
Channel = channel;
}
public IReplyChannel<int> Channel { get; }
}
I am not sure why the exception is thrown. Why are default values not accepted?
This is an unfortunate translation of the original F# code. The F# version uses the option type to indicate whether the message was handled and replied to within the given timeout. There's no such (built-in) type in .NET other than Nullable<T>, which only works for value types.
@kthompson Are you OK with changing the signature of TryPostAndReply? We could make it more "C# like" and return a bool and put the return value into an out variable.
I don't actively work on this but would be happy to accept any PRs. If that is something you are interested in let me know.
I believe the problem is the following.