From d8a27e9298c4e4254e26cf3619a706eda175f409 Mon Sep 17 00:00:00 2001 From: dbuechel Date: Mon, 2 Dec 2019 17:00:55 +0100 Subject: [PATCH] SEBWIN-312: Ensured already known application instances are not initialized twice. --- .../ExternalApplication.cs | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/SafeExamBrowser.Applications/ExternalApplication.cs b/SafeExamBrowser.Applications/ExternalApplication.cs index 708ef0cb..33ccd63b 100644 --- a/SafeExamBrowser.Applications/ExternalApplication.cs +++ b/SafeExamBrowser.Applications/ExternalApplication.cs @@ -108,10 +108,15 @@ namespace SafeExamBrowser.Applications private void ApplicationMonitor_InstanceStarted(Guid applicationId, IProcess process) { - if (applicationId == Id) + lock (@lock) { - logger.Info("New application instance was started."); - InitializeInstance(process); + var isNewInstance = instances.All(i => i.Id != process.Id); + + if (applicationId == Id && isNewInstance) + { + logger.Info("New application instance was started."); + InitializeInstance(process); + } } } @@ -127,15 +132,15 @@ namespace SafeExamBrowser.Applications private void InitializeInstance(IProcess process) { - var instanceLogger = logger.CloneFor($"{Name} ({process.Id})"); - var instance = new ExternalApplicationInstance(Icon, instanceLogger, nativeMethods, process); - - instance.Terminated += Instance_Terminated; - instance.WindowsChanged += () => WindowsChanged?.Invoke(); - instance.Initialize(); - lock (@lock) { + var instanceLogger = logger.CloneFor($"{Name} ({process.Id})"); + var instance = new ExternalApplicationInstance(Icon, instanceLogger, nativeMethods, process); + + instance.Terminated += Instance_Terminated; + instance.WindowsChanged += () => WindowsChanged?.Invoke(); + instance.Initialize(); + instances.Add(instance); } }