From 50d1485c5dc9c2656fe842462a960327f3ba13ac Mon Sep 17 00:00:00 2001 From: Mike Loss Date: Tue, 12 Jul 2022 21:16:40 -0700 Subject: [PATCH 1/2] Removed Environment.Exit to facilitate use with inlineExecute-Assembly etc --- SnaffCore/SnaffCon.cs | 2 +- Snaffler/Config.cs | 4 ++-- Snaffler/SnaffleRunner.cs | 31 +++++++++++++++++++++---------- Snaffler/Snaffler.cs | 1 + 4 files changed, 25 insertions(+), 13 deletions(-) diff --git a/SnaffCore/SnaffCon.cs b/SnaffCore/SnaffCon.cs index 5c34283..b7d79b4 100644 --- a/SnaffCore/SnaffCon.cs +++ b/SnaffCore/SnaffCon.cs @@ -82,7 +82,7 @@ public void Execute() StartTime = DateTime.Now; // This is the main execution thread. Timer statusUpdateTimer = - new Timer(TimeSpan.FromMinutes(1) + new Timer(TimeSpan.FromMinutes(0.2) .TotalMilliseconds) { AutoReset = true }; // Set the time (1 min in this case) statusUpdateTimer.Elapsed += TimedStatusUpdate; diff --git a/Snaffler/Config.cs b/Snaffler/Config.cs index cc8b7ad..05389c9 100644 --- a/Snaffler/Config.cs +++ b/Snaffler/Config.cs @@ -123,7 +123,7 @@ private static Options ParseImpl(string[] args) if ((args.Contains("--help") || args.Contains("/?") || args.Contains("help") || args.Contains("-h") || args.Length == 0)) { parser.ShowUsage(); - Environment.Exit(0); + return null; } TomlSettings settings = TomlSettings.Create(cfg => cfg @@ -295,7 +295,7 @@ private static Options ParseImpl(string[] args) Console.WriteLine("Wrote config values to .\\default.toml"); parsedConfig.LogToConsole = true; Mq.Degub("Enabled logging to stdout."); - Environment.Exit(0); + return null; } else { diff --git a/Snaffler/SnaffleRunner.cs b/Snaffler/SnaffleRunner.cs index 18fe17c..9482b2d 100644 --- a/Snaffler/SnaffleRunner.cs +++ b/Snaffler/SnaffleRunner.cs @@ -10,6 +10,7 @@ using System.IO; using System.Threading.Tasks; using System.Text.RegularExpressions; +using System.Threading; namespace Snaffler { @@ -177,12 +178,20 @@ public void Run(string[] args) } controller = new SnaffCon(Options); - Task thing = Task.Factory.StartNew(() => { controller.Execute(); }); - while (true) + var tokenSource = new CancellationTokenSource(); + var token = tokenSource.Token; + Task thing = Task.Factory.StartNew(() => { controller.Execute(); }, token); + bool exit = false; + + while (exit == false) { - HandleOutput(); + if (HandleOutput() == true) + { + exit = true; + } } + return; } catch (Exception e) { @@ -203,10 +212,9 @@ private void DumpQueue() { Console.ReadKey(); } - Environment.Exit(1); } - private void HandleOutput() + private bool HandleOutput() { BlockingMq Mq = BlockingMq.GetMq(); foreach (SnafflerMessage message in Mq.Q.GetConsumingEnumerable()) @@ -218,8 +226,15 @@ private void HandleOutput() else if (Options.LogType == LogType.JSON) { ProcessMessageJSON(message); - } + } + + // catch terminating messages and bail out of the master 'while' loop + if ((message.Type == SnafflerMessageType.Fatal) || (message.Type == SnafflerMessageType.Finish)) + { + return true; + } } + return false; } private void ProcessMessage(SnafflerMessage message) @@ -256,7 +271,6 @@ private void ProcessMessage(SnafflerMessage message) { Console.ReadKey(); } - Environment.Exit(1); break; case SnafflerMessageType.Finish: Logger.Info("Snaffler out."); @@ -266,7 +280,6 @@ private void ProcessMessage(SnafflerMessage message) Console.WriteLine("Press any key to exit."); Console.ReadKey(); } - Environment.Exit(0); break; } } @@ -313,7 +326,6 @@ private void ProcessMessageJSON(SnafflerMessage message) { Console.ReadKey(); } - Environment.Exit(1); break; case SnafflerMessageType.Finish: Logger.Info("Snaffler out."); @@ -328,7 +340,6 @@ private void ProcessMessageJSON(SnafflerMessage message) Logger.Info("Normalising output, please wait..."); FixJSONOutput(); } - Environment.Exit(0); break; } } diff --git a/Snaffler/Snaffler.cs b/Snaffler/Snaffler.cs index 35b0e93..dff0446 100644 --- a/Snaffler/Snaffler.cs +++ b/Snaffler/Snaffler.cs @@ -8,6 +8,7 @@ public static void Main(string[] args) { SnaffleRunner runner = new SnaffleRunner(); runner.Run(args); + Console.WriteLine("I snaffled 'til the snafflin was done."); } } } \ No newline at end of file From 373dce6adb623c2eb5c40878ea24ddd1e1c962ab Mon Sep 17 00:00:00 2001 From: Mike Loss Date: Wed, 13 Jul 2022 12:37:18 +0800 Subject: [PATCH 2/2] Update SnaffCon.cs --- SnaffCore/SnaffCon.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SnaffCore/SnaffCon.cs b/SnaffCore/SnaffCon.cs index b7d79b4..5c34283 100644 --- a/SnaffCore/SnaffCon.cs +++ b/SnaffCore/SnaffCon.cs @@ -82,7 +82,7 @@ public void Execute() StartTime = DateTime.Now; // This is the main execution thread. Timer statusUpdateTimer = - new Timer(TimeSpan.FromMinutes(0.2) + new Timer(TimeSpan.FromMinutes(1) .TotalMilliseconds) { AutoReset = true }; // Set the time (1 min in this case) statusUpdateTimer.Elapsed += TimedStatusUpdate;