Thread Abort in netcore is not working

This commit is contained in:
BlubbFish 2019-12-01 17:53:57 +01:00
parent 35ba2b8135
commit 47cdef0959
4 changed files with 14 additions and 7 deletions

0
Changelog.md Normal file
View File

View File

@ -35,6 +35,7 @@
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Content Include="..\CHANGELOG.md" />
<Content Include="..\CONTRIBUTING.md" /> <Content Include="..\CONTRIBUTING.md" />
<Content Include="..\LICENSE" /> <Content Include="..\LICENSE" />
<Content Include="..\README.md" /> <Content Include="..\README.md" />

View File

@ -24,6 +24,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Content Include="../CHANGELOG.md" />
<Content Include="../CONTRIBUTING.md" /> <Content Include="../CONTRIBUTING.md" />
<Content Include="../LICENSE" /> <Content Include="../LICENSE" />
<Content Include="../README.md" /> <Content Include="../README.md" />

View File

@ -11,9 +11,10 @@ namespace BlubbFish.Utils.IoT.Connector.Data {
public class Mqtt : ADataBackend, IDisposable { public class Mqtt : ADataBackend, IDisposable {
private MqttClient client; private MqttClient client;
private Thread connectionWatcher; private Thread connectionWatcher;
private Boolean connectionWatcherRunning;
public Mqtt(Dictionary<String, String> settings) : base(settings) { public Mqtt(Dictionary<String, String> settings) : base(settings) {
Console.WriteLine("ConnectorDataMqtt_constr("+this.ToString()+")"); Console.WriteLine("BlubbFish.Utils.IoT.Connector.Data.Mqtt(" + this.ToString()+")");
Int32 port = 1883; Int32 port = 1883;
if(this.settings.ContainsKey("port")) { if(this.settings.ContainsKey("port")) {
port = Int32.Parse(this.settings["port"]); port = Int32.Parse(this.settings["port"]);
@ -44,22 +45,23 @@ namespace BlubbFish.Utils.IoT.Connector.Data {
#region ConectionManage #region ConectionManage
private void ConnectionWatcher() { private void ConnectionWatcher() {
this.connectionWatcher = new Thread(this.ConnectionWatcherRunner); this.connectionWatcher = new Thread(this.ConnectionWatcherRunner);
this.connectionWatcherRunning = true;
this.connectionWatcher.Start(); this.connectionWatcher.Start();
} }
private void ConnectionWatcherRunner() { private void ConnectionWatcherRunner() {
while(true) { while(this.connectionWatcherRunning) {
try { try {
if(!this.IsConnected) { if(!this.IsConnected) {
this.Reconnect(); this.Reconnect();
Thread.Sleep(1000);
} }
Thread.Sleep(500); Thread.Sleep(10);
} catch(Exception) { } } catch(Exception) { }
} }
} }
private void Reconnect() { private void Reconnect() {
Console.WriteLine("ConnectorDataMqtt_Reconnect(" + this.ToString() + ")");
if(this.IsConnected) { if(this.IsConnected) {
this.Disconnect(true); this.Disconnect(true);
} else { } else {
@ -69,7 +71,7 @@ namespace BlubbFish.Utils.IoT.Connector.Data {
} }
private void Disconnect(Boolean complete) { private void Disconnect(Boolean complete) {
Console.WriteLine("ConnectorDataMqtt_Disconnect(" + this.ToString() + ")"); Console.WriteLine("BlubbFish.Utils.IoT.Connector.Data.Disconnect(" + this.ToString() + ")");
this.client.MqttMsgPublishReceived -= this.Client_MqttMsgPublishReceived; this.client.MqttMsgPublishReceived -= this.Client_MqttMsgPublishReceived;
this.Unsubscripe(); this.Unsubscripe();
if(complete) { if(complete) {
@ -78,7 +80,7 @@ namespace BlubbFish.Utils.IoT.Connector.Data {
} }
private void Connect() { private void Connect() {
Console.WriteLine("ConnectorDataMqtt_Connect(" + this.ToString() + ")"); Console.WriteLine("BlubbFish.Utils.IoT.Connector.Data.Connect(" + this.ToString() + ")");
this.client.MqttMsgPublishReceived += this.Client_MqttMsgPublishReceived; this.client.MqttMsgPublishReceived += this.Client_MqttMsgPublishReceived;
_ = this.settings.ContainsKey("user") && this.settings.ContainsKey("pass") _ = this.settings.ContainsKey("user") && this.settings.ContainsKey("pass")
? this.client.Connect(Guid.NewGuid().ToString(), this.settings["user"], this.settings["pass"]) ? this.client.Connect(Guid.NewGuid().ToString(), this.settings["user"], this.settings["pass"])
@ -127,7 +129,10 @@ namespace BlubbFish.Utils.IoT.Connector.Data {
if(!this.disposedValue) { if(!this.disposedValue) {
if(disposing) { if(disposing) {
try { try {
this.connectionWatcher.Abort(); this.connectionWatcherRunning = false;
while(this.connectionWatcher.IsAlive) {
Thread.Sleep(10);
}
this.connectionWatcher = null; this.connectionWatcher = null;
this.Disconnect(true); this.Disconnect(true);
} catch (Exception) { } } catch (Exception) { }