From 38bc5e9f77fe1d79729963d375c677b8cc692dfe Mon Sep 17 00:00:00 2001 From: Etka Bayramoglu Date: Thu, 28 May 2020 11:52:50 -0500 Subject: [PATCH] fixed locking DispatchEventThread If MqttMsgPublishReceived event takes long time to process. It locks DispatchEventThread and because of that can't receive any other MQTT messages until DispatchEventThread finishes processing the event. --- M2Mqtt/MqttClient.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/M2Mqtt/MqttClient.cs b/M2Mqtt/MqttClient.cs index 3345aa50..806b2846 100644 --- a/M2Mqtt/MqttClient.cs +++ b/M2Mqtt/MqttClient.cs @@ -21,6 +21,7 @@ Paolo Patierno - initial API and implementation and/or initial documentation using System.Security.Cryptography.X509Certificates; #endif using System.Threading; +using System.Threading.Tasks; using uPLibrary.Networking.M2Mqtt.Exceptions; using uPLibrary.Networking.M2Mqtt.Messages; using uPLibrary.Networking.M2Mqtt.Session; @@ -885,7 +886,7 @@ private void OnConnectionClosing() } } - /// + /// /// Wrapper method for raising PUBLISH message received event /// /// PUBLISH message received @@ -893,8 +894,9 @@ private void OnMqttMsgPublishReceived(MqttMsgPublish publish) { if (this.MqttMsgPublishReceived != null) { - this.MqttMsgPublishReceived(this, - new MqttMsgPublishEventArgs(publish.Topic, publish.Message, publish.DupFlag, publish.QosLevel, publish.Retain)); + Task.Run(() => this.MqttMsgPublishReceived(this, + new MqttMsgPublishEventArgs(publish.Topic, publish.Message, publish.DupFlag, publish.QosLevel, publish.Retain))); + } }