I have an infinite while loop. I want to have a print of number of messages at every second. It should print after every second the count of the messages. for example if I have 500 messages in 1 second then it should print the count as 500 after the messages and then in if it is 500 in next 1 sec then it should print 500 again after the messages Here is my code:
static void SimulatePublish()
{
var counter = 1;
while (true)
{
counter ;
var testMessage = new MqttApplicationMessageBuilder()
.WithTopic("MqttData")
.WithPayload($"Payload: This is my Code")
.WithAtMostOnceQoS()
.WithRetainFlag()
.Build();
if (_client.IsConnected)
{
Console.WriteLine($"publishing at {DateTime.UtcNow}");
_client.PublishAsync(testMessage);
}
}
}
CodePudding user response:
int messagesDiff = 0;
long startTime = DateTimeOffset.Now.ToUnixTimeSeconds(), timeDiff;
TimerCallback printingLambda = (o) =>
{
timeDiff = DateTimeOffset.Now.ToUnixTimeSeconds() - startTime;
Console.WriteLine("Messages per second: " messagesDiff / (double)timeDiff);
startTime = DateTimeOffset.Now.ToUnixTimeSeconds();
messagesDiff = 0;
};
Timer timer = new Timer(printingLambda, null, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1));
while (true)
{
//public message
messagesDiff ;
}