From 4f930a26d83187ba17bcb62b61d5ec84fec540b4 Mon Sep 17 00:00:00 2001 From: dbuechel Date: Wed, 13 Nov 2019 11:43:34 +0100 Subject: [PATCH] SEBWIN-312: Fixed event handling for external application instances. --- .../ExternalApplication.cs | 8 +-- .../ExternalApplicationInstance.cs | 59 +++++++++++-------- 2 files changed, 40 insertions(+), 27 deletions(-) diff --git a/SafeExamBrowser.Applications/ExternalApplication.cs b/SafeExamBrowser.Applications/ExternalApplication.cs index ec2a8cb0..2bf134bb 100644 --- a/SafeExamBrowser.Applications/ExternalApplication.cs +++ b/SafeExamBrowser.Applications/ExternalApplication.cs @@ -58,11 +58,11 @@ namespace SafeExamBrowser.Applications if (instances.Any()) { logger.Info("Terminating application..."); - } - foreach (var instance in instances) - { - instance.Terminate(); + foreach (var instance in instances) + { + instance.Terminate(); + } } } } diff --git a/SafeExamBrowser.Applications/ExternalApplicationInstance.cs b/SafeExamBrowser.Applications/ExternalApplicationInstance.cs index da6e7ea9..3f3dd083 100644 --- a/SafeExamBrowser.Applications/ExternalApplicationInstance.cs +++ b/SafeExamBrowser.Applications/ExternalApplicationInstance.cs @@ -50,12 +50,7 @@ namespace SafeExamBrowser.Applications public void Initialize() { - process.Terminated += Process_Terminated; - - timer = new Timer(ONE_SECOND); - timer.Elapsed += Timer_Elapsed; - timer.Start(); - + InitializeEvents(); logger.Info("Initialized application instance."); } @@ -64,37 +59,55 @@ namespace SafeExamBrowser.Applications const int MAX_ATTEMPTS = 5; const int TIMEOUT_MS = 500; - timer.Elapsed -= Timer_Elapsed; - timer?.Stop(); - var terminated = process.HasTerminated; - for (var attempt = 0; attempt < MAX_ATTEMPTS && !terminated; attempt++) + if (!terminated) { - terminated = process.TryClose(TIMEOUT_MS); - } + FinalizeEvents(); - for (var attempt = 0; attempt < MAX_ATTEMPTS && !terminated; attempt++) - { - terminated = process.TryKill(TIMEOUT_MS); - } + for (var attempt = 0; attempt < MAX_ATTEMPTS && !terminated; attempt++) + { + terminated = process.TryClose(TIMEOUT_MS); + } - if (terminated) - { - logger.Info("Successfully terminated application instance."); - } - else - { - logger.Warn("Failed to terminate application instance!"); + for (var attempt = 0; attempt < MAX_ATTEMPTS && !terminated; attempt++) + { + terminated = process.TryKill(TIMEOUT_MS); + } + + if (terminated) + { + logger.Info("Successfully terminated application instance."); + } + else + { + logger.Warn("Failed to terminate application instance!"); + } } } private void Process_Terminated(int exitCode) { logger.Info($"Application instance has terminated with exit code {exitCode}."); + FinalizeEvents(); Terminated?.Invoke(Id); } + private void InitializeEvents() + { + timer = new Timer(ONE_SECOND); + timer.Elapsed += Timer_Elapsed; + timer.Start(); + process.Terminated += Process_Terminated; + } + + private void FinalizeEvents() + { + timer.Elapsed -= Timer_Elapsed; + timer.Stop(); + process.Terminated -= Process_Terminated; + } + private void Timer_Elapsed(object sender, ElapsedEventArgs e) { var success = process.TryGetWindowTitle(out var title);