In my code I'm using a mqtt client, when a message is received the event handler is raised and the message gets stored in a list.
private void MqttBroker_MqttMessagePublishedReceived(object sender, Events.MqttMessageEventArgs e)
{
_logger?.Information($"MqttBroker_MqttMessagePublishedReceived - message received by the watcher {e}");
try
{
if (e?.Payload != null)
{
_messages.AddMessage(e.Payload);
}
}
catch (Exception ex)
{
_logger?.Error($"MqttBroker_MqttMessagePublishedReceived - Something went wrong: {ex}");
}
}
In the meantime, I'm waiting for x minutes in an other function with a while loop. This should be done by another way but for the moment I let it rest. But my question now is. Is it possible that this while loop can block an event that is being raised? So that the mqtt broker publish a message just when I'm in that while loop?
My second question is the following, instead of a while loop I let my thread sleeping for x minutes does the event still get raised and will the messages still will be stored in my list?
It depends.
If your Event is called in another Thread, there wont be any problems, but if your EventThread and your LoopThread are the same, the event won't be "sucked in" it will wait till the while loop is over and then it will trigger the event.