protected override void OnStart(string[] args) { Task.Run(() => { AsyncContext.Run(async () => { XmlConfigurator.Configure(); while (!_stop) { try { _cancellationTokenSource.Token.ThrowIfCancellationRequested(); _client?.Stop(); _client = new ComfoBoxMqttClient(ComfoBoxMqtt.Properties.Settings.Default.MqttBrokerAddresses.OfType<string>().ToArray(), new ComfoBoxClient(Settings.Default.Port, Settings.Default.Baudrate, Settings.Default.BacnetClientId)); await _client.StartAsync(); await _client.StartPollingAsync(); } catch (OperationCanceledException) { Logger.Info($"OnStart(): Received Cancel"); return; } catch (Exception ex) { string message = $"ComfoBoxService(): Excpetion {ex.Message},\r\n{ex.StackTrace}}}"; Logger.Error(message); await Task.Delay(1000); } } }); }, _cancellationTokenSource.Token); base.OnStart(args); }
private static void Run() { AsyncContext.Run(async () => { while (true) { try { var client = new ComfoBoxMqttClient(Settings.Default.MqttBrokerAddresses.OfType<string>().ToArray(), new ComfoBoxClient(LibSettings.Default.Port, LibSettings.Default.Baudrate, LibSettings.Default.BacnetClientId)); await client.StartAsync(); await client.StartPollingAsync(); } catch (Exception ex) { Logger.Error($"Run() has thrown an exception: {ex.Message}"); Thread.Sleep(5000); } } }); Console.ReadLine(); }