diff --git a/README.md b/README.md index 7b53704..cfaa47c 100644 --- a/README.md +++ b/README.md @@ -347,6 +347,10 @@ The complete usage of the application can be shown using the `--help` command li --vc, --verboseconsole=VALUE the output of publisher is shown on the console. Default: False + --ns, --noshutdown=VALUE + publisher could not be stopped by pressing a key + on the console, but will run forever. + Default: False --ih, --iothubprotocol=VALUE the protocol to use for communication with Azure IoTHub (allowed values: Amqp, Http1, Amqp_ diff --git a/src/Diagnostics.cs b/src/Diagnostics.cs index 2efb171..5ab5a4e 100644 --- a/src/Diagnostics.cs +++ b/src/Diagnostics.cs @@ -61,14 +61,14 @@ namespace OpcPublisher { while (true) { + if (ct.IsCancellationRequested) + { + return; + } + try { - await Task.Delay((int)_diagnosticsInterval * 1000, ct); - if (ct.IsCancellationRequested) - { - return; - } Trace("=========================================================================="); Trace($"OpcPublisher status @ {System.DateTime.UtcNow} (started @ {PublisherStartTime})"); diff --git a/src/IotHubMessaging.cs b/src/IotHubMessaging.cs index 6d286ce..599218b 100644 --- a/src/IotHubMessaging.cs +++ b/src/IotHubMessaging.cs @@ -505,7 +505,7 @@ namespace OpcPublisher // if we are in shutdown do not wait, else wait infinite if send interval is not set millisecondsTillNextSend = ct.IsCancellationRequested ? 0 : Timeout.Infinite; } - bool gotItem = _monitoredItemsDataQueue.TryTake(out messageData, (int)millisecondsTillNextSend); + bool gotItem = _monitoredItemsDataQueue.TryTake(out messageData, (int)millisecondsTillNextSend, ct); // the two commandline parameter --ms (message size) and --si (send interval) control when data is sent to IoTHub // pls see detailed comments on performance and memory consumption at https://github.com/Azure/iot-edge-opc-publisher @@ -629,7 +629,11 @@ namespace OpcPublisher } catch (Exception e) { - Trace(e, "Error while processing monitored item messages."); + if (!(e is OperationCanceledException)) + { + + Trace(e, "Error while processing monitored item messages."); + } } } } diff --git a/src/Program.cs b/src/Program.cs index 33a2bfd..c60162f 100644 --- a/src/Program.cs +++ b/src/Program.cs @@ -100,6 +100,8 @@ namespace OpcPublisher { "di|diagnosticsinterval=", $"shows publisher diagnostic info at the specified interval in seconds. 0 disables diagnostic output.\nDefault: {DiagnosticsInterval}", (uint u) => DiagnosticsInterval = u }, { "vc|verboseconsole=", $"the output of publisher is shown on the console.\nDefault: {VerboseConsole}", (bool b) => VerboseConsole = b }, + + { "ns|noshutdown=", $"publisher could not be stopped by pressing a key on the console, but will run forever.\nDefault: {_noShutdown}", (bool b) => _noShutdown = b }, // IoTHub specific options { "ih|iothubprotocol=", $"the protocol to use for communication with Azure IoTHub (allowed values: {string.Join(", ", Enum.GetNames(IotHubProtocol.GetType()))}).\nDefault: {Enum.GetName(IotHubProtocol.GetType(), IotHubProtocol)}", @@ -459,16 +461,26 @@ namespace OpcPublisher // stop on user request WriteLine(""); WriteLine(""); - WriteLine("Publisher is running. Press any key to quit."); - try + if (_noShutdown) { - ReadKey(true); - } - catch - { - // wait forever if there is no console + // wait forever if asked to do so + WriteLine("Publisher is running infinite..."); await Task.Delay(Timeout.Infinite); } + else + { + WriteLine("Publisher is running. Press any key to quit."); + try + { + ReadKey(true); + } + catch + { + // wait forever if there is no console + WriteLine("There is no console. Publisher is running infinite..."); + await Task.Delay(Timeout.Infinite); + } + } WriteLine(""); WriteLine(""); ShutdownTokenSource.Cancel(); @@ -543,7 +555,11 @@ namespace OpcPublisher { Trace(e, $"Failed to connect and monitor a disconnected server. {(e.InnerException != null ? e.InnerException.Message : "")}"); } - await Task.Delay(_publisherSessionConnectWaitSec * 1000, ct); + try + { + await Task.Delay(_publisherSessionConnectWaitSec * 1000, ct); + } + catch { } if (ct.IsCancellationRequested) { return; @@ -681,5 +697,6 @@ namespace OpcPublisher private static PublisherServer _publisherServer; private static bool _opcTraceInitialized = false; private static int _publisherSessionConnectWaitSec = 10; + private static bool _noShutdown = false; } }