Improving dispose in DeviceClient and ProvisioningDeviceClient (#304)
This commit is contained in:
Родитель
41d021b722
Коммит
087d7b564e
|
@ -69,15 +69,12 @@
|
|||
</Reference>
|
||||
<Reference Include="nanoFramework.Json, Version=2.2.101.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
|
||||
<HintPath>..\packages\nanoFramework.Json.2.2.101\lib\nanoFramework.Json.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="nanoFramework.M2Mqtt, Version=5.1.96.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
|
||||
<HintPath>..\packages\nanoFramework.M2Mqtt.5.1.96\lib\nanoFramework.M2Mqtt.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="nanoFramework.M2Mqtt.Core, Version=0.0.0.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
|
||||
<HintPath>..\packages\nanoFramework.M2Mqtt.5.1.96\lib\nanoFramework.M2Mqtt.Core.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="nanoFramework.Runtime.Events">
|
||||
<HintPath>..\packages\nanoFramework.Runtime.Events.1.11.6\lib\nanoFramework.Runtime.Events.dll</HintPath>
|
||||
|
|
|
@ -50,6 +50,8 @@ namespace nanoFramework.Azure.Devices.Client
|
|||
private readonly object _lock = new object();
|
||||
private Timer _timerTokenRenew;
|
||||
private bool _hasClientCertificate;
|
||||
private bool _isDisposed = false;
|
||||
private bool _isClosed = true;
|
||||
|
||||
/// <summary>
|
||||
/// Device twin updated event.
|
||||
|
@ -359,6 +361,7 @@ namespace nanoFramework.Azure.Devices.Client
|
|||
_timerTokenRenew = new Timer(TimerCallbackReconnect, null, new TimeSpan(23, 50, 0), TimeSpan.MaxValue);
|
||||
}
|
||||
|
||||
_isClosed = !_mqttc.IsConnected;
|
||||
return _mqttc.IsConnected;
|
||||
}
|
||||
|
||||
|
@ -382,6 +385,11 @@ namespace nanoFramework.Azure.Devices.Client
|
|||
/// </summary>
|
||||
public void Close()
|
||||
{
|
||||
if (_isClosed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (_mqttc != null)
|
||||
{
|
||||
if (_mqttc.IsConnected)
|
||||
|
@ -394,13 +402,21 @@ namespace nanoFramework.Azure.Devices.Client
|
|||
}
|
||||
|
||||
_mqttc.Disconnect();
|
||||
_mqttc.Close();
|
||||
try
|
||||
{
|
||||
_mqttc.Close();
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Nothing on purpose, we want to make sure we can continue
|
||||
}
|
||||
|
||||
// Make sure all get disconnected, cleared
|
||||
Thread.Sleep(1000);
|
||||
}
|
||||
|
||||
_timerTokenRenew?.Dispose();
|
||||
_isClosed = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -804,6 +820,12 @@ namespace nanoFramework.Azure.Devices.Client
|
|||
/// <inheritdoc/>
|
||||
public void Dispose()
|
||||
{
|
||||
if (_isDisposed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_isDisposed = true;
|
||||
if (_mqttc != null)
|
||||
{
|
||||
// Making sure we unregister the registered events
|
||||
|
@ -815,7 +837,6 @@ namespace nanoFramework.Azure.Devices.Client
|
|||
while (_mqttc.IsConnected)
|
||||
{
|
||||
Thread.Sleep(100);
|
||||
|
||||
}
|
||||
|
||||
// Cleaning
|
||||
|
|
|
@ -34,6 +34,7 @@ namespace nanoFramework.Azure.Devices.Provisioning.Client
|
|||
private ProvisioningRegistrationStatusType _status = ProvisioningRegistrationStatusType.Unassigned;
|
||||
private DeviceRegistrationResult _result = null;
|
||||
private bool _isMessageProcessed = false;
|
||||
private bool _isDisposed = false;
|
||||
|
||||
/// <summary>
|
||||
/// Creates an instance of the Device Provisioning Client.
|
||||
|
@ -228,9 +229,17 @@ namespace nanoFramework.Azure.Devices.Provisioning.Client
|
|||
{
|
||||
// We need to clean everything now
|
||||
_mqttc.MqttMsgPublishReceived -= ClientMqttMsgReceived;
|
||||
_mqttc.Unsubscribe(new[] {
|
||||
try
|
||||
{
|
||||
_mqttc.Unsubscribe(new[] {
|
||||
DpsSubscription
|
||||
});
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Nothing on purpose, just cleaning
|
||||
}
|
||||
|
||||
_mqttc.Disconnect();
|
||||
while (_mqttc.IsConnected)
|
||||
{
|
||||
|
@ -337,6 +346,12 @@ namespace nanoFramework.Azure.Devices.Provisioning.Client
|
|||
/// <inheritdoc/>
|
||||
public void Dispose()
|
||||
{
|
||||
if(_isDisposed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_isDisposed = true;
|
||||
if (_mqttc != null)
|
||||
{
|
||||
CleanAll();
|
||||
|
|
Загрузка…
Ссылка в новой задаче