0

I'm using the MQTTnet client in a console app. In my use case it seems like I do not need asynchrouns execution and I'd like to ease design by using synchronous execution. However in case I'd have to switch to an asynchrous design I'd like to prevent from having to change the mqtt client package.

As far as I know the MqttClient has asynchronous versions of methods for connecting, publishing, disconnecting, etc. only. Are there synchronous versions of client methods as well? In case there are just asynchronous versions of the methods: Can I wrap the asynchronous methods somehow to make them synchronous?

thinwybk
  • 4,193
  • 2
  • 40
  • 76
  • Calling [`Task.Wait`](https://learn.microsoft.com/en-us/dotnet/api/system.threading.tasks.task.wait?view=net-5.0) on the `Task` returned by the async method will block until the task completes. – Emanuel Vintilă Jan 27 '21 at 09:24
  • There is no synchronous API for `MqttClient`, right? Thanks for the hint. If you answer I'll accept it. – thinwybk Jan 27 '21 at 09:32
  • @EmanuelVintilă I think in addition to wait for the Task one has to check if it was cancelled (`Task.IsCancelled`), completed (`Task.IsCompleted`) and if there was a fault (`Task.IsFaulted`) as well. This should be contained in a combined manner in the status (`Task.Status`) as well, right? In the good case I'd expect `TaskStatus.RanToCompletion` as `Task.Status`. – thinwybk Jan 27 '21 at 09:51

0 Answers0