Преглед на файлове

Execute disconnected handler in new task.

release/3.x.x
Christian Kratky преди 5 години
родител
ревизия
c3c0dcf313
променени са 2 файла, в които са добавени 22 реда и са изтрити 2 реда
  1. +5
    -2
      Source/MQTTnet/Client/MqttClient.cs
  2. +17
    -0
      Source/MQTTnet/Internal/TaskExtensions.cs

+ 5
- 2
Source/MQTTnet/Client/MqttClient.cs Целия файл

@@ -13,6 +13,7 @@ using MQTTnet.Client.Subscribing;
using MQTTnet.Client.Unsubscribing;
using MQTTnet.Diagnostics;
using MQTTnet.Exceptions;
using MQTTnet.Internal;
using MQTTnet.PacketDispatcher;
using MQTTnet.Packets;
using MQTTnet.Protocol;
@@ -287,7 +288,9 @@ namespace MQTTnet.Client
var disconnectedHandler = DisconnectedHandler;
if (disconnectedHandler != null)
{
await disconnectedHandler.HandleDisconnectedAsync(new MqttClientDisconnectedEventArgs(clientWasConnected, exception, authenticateResult)).ConfigureAwait(false);
// This handler must be executed in a new thread because otherwise a dead lock may happen
// when trying to reconnect in that handler etc.
Task.Run(() => disconnectedHandler.HandleDisconnectedAsync(new MqttClientDisconnectedEventArgs(clientWasConnected, exception, authenticateResult))).Forget(_logger);
}
}
}
@@ -648,4 +651,4 @@ namespace MQTTnet.Client
return Interlocked.CompareExchange(ref _disconnectGate, 1, 0) != 0;
}
}
}
}

+ 17
- 0
Source/MQTTnet/Internal/TaskExtensions.cs Целия файл

@@ -0,0 +1,17 @@
using System.Threading.Tasks;
using MQTTnet.Diagnostics;

namespace MQTTnet.Internal
{
public static class TaskExtensions
{
public static void Forget(this Task task, IMqttNetChildLogger logger)
{
task?.ContinueWith(t =>
{
logger.Error(t.Exception, "Unhandled exception.");
},
TaskContinuationOptions.OnlyOnFaulted);
}
}
}

Зареждане…
Отказ
Запис