Skip to content

Commit f589624

Browse files
authored
Improve disconnection and reconnection (#253)
1 parent 295d334 commit f589624

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

nanoFramework.Azure.Devices.Client/DeviceClient.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class DeviceClient : IDisposable
3232
private readonly string _deviceMessageTopic;
3333
private Twin _twin;
3434
private bool _twinReceived;
35-
private MqttClient _mqttc;
35+
private MqttClient _mqttc = null;
3636
private readonly IoTHubStatus _ioTHubStatus = new IoTHubStatus();
3737
private readonly ArrayList _methodCallback = new ArrayList();
3838
private readonly ArrayList _waitForConfirmation = new ArrayList();
@@ -279,19 +279,24 @@ private void TimerCallbackReconnect(object state)
279279
/// </summary>
280280
public void Close()
281281
{
282-
if (_mqttc.IsConnected)
282+
if (_mqttc != null)
283283
{
284-
_mqttc.Unsubscribe(new[] {
284+
if (_mqttc.IsConnected)
285+
{
286+
_mqttc.Unsubscribe(new[] {
285287
$"devices/{_deviceId}/messages/devicebound/#",
286288
"$iothub/twin/#",
287289
"$iothub/methods/POST/#"
288290
});
291+
}
292+
289293
_mqttc.Disconnect();
294+
_mqttc.Close();
290295
// Make sure all get disconnected, cleared
291296
Thread.Sleep(1000);
292297
}
293298

294-
_timerTokenRenew.Dispose();
299+
_timerTokenRenew?.Dispose();
295300
}
296301

297302
/// <summary>

0 commit comments

Comments
 (0)