From 67c4a87c1db7fd906f3dfc88aa6cc26c51dc6d4f Mon Sep 17 00:00:00 2001 From: kpreisser Date: Sun, 12 Aug 2018 19:17:20 +0200 Subject: [PATCH] Dispose of the processes after using them. --- .../Environment/AbstractWindowsEnvironment.cs | 4 +- .../StandardInteractionProvider.cs | 110 ++++++++++-------- 2 files changed, 62 insertions(+), 52 deletions(-) diff --git a/TTMouseclickSimulator/Core/Environment/AbstractWindowsEnvironment.cs b/TTMouseclickSimulator/Core/Environment/AbstractWindowsEnvironment.cs index c942768..92581e3 100644 --- a/TTMouseclickSimulator/Core/Environment/AbstractWindowsEnvironment.cs +++ b/TTMouseclickSimulator/Core/Environment/AbstractWindowsEnvironment.cs @@ -36,8 +36,8 @@ protected List FindProcessesByName(string processname) } catch { - // Ignore - // Dispose of the process since we won't use it. + // We cannot access the process, so dispose of it since we + // won't use it. p.Dispose(); } } diff --git a/TTMouseclickSimulator/Core/Environment/StandardInteractionProvider.cs b/TTMouseclickSimulator/Core/Environment/StandardInteractionProvider.cs index 2623a71..27cf56e 100644 --- a/TTMouseclickSimulator/Core/Environment/StandardInteractionProvider.cs +++ b/TTMouseclickSimulator/Core/Environment/StandardInteractionProvider.cs @@ -69,70 +69,80 @@ public async Task InitializeAsync() // First, find the game processes. This will always return at least one process, // or throw. var processes = this.environmentInterface.FindProcesses(); - if (processes.Count == 1) + try { - if (lastInitializingEventParameter != false) + if (processes.Count == 1) { - lastInitializingEventParameter = false; - this.simulator.OnSimulatorInitializing(lastInitializingEventParameter); - } + if (lastInitializingEventParameter != false) + { + lastInitializingEventParameter = false; + this.simulator.OnSimulatorInitializing(lastInitializingEventParameter); + } - // When there is only one process, we simply bring the window to the - // foreground (if we didn't do it already). - this.windowHandle = this.environmentInterface.FindMainWindowHandleOfProcess(processes[0]); - if (this.windowHandle != previousWindowToBringToForeground) - { - previousWindowToBringToForeground = this.windowHandle; - this.environmentInterface.BringWindowToForeground(this.windowHandle); - } + // When there is only one process, we simply bring the window to the + // foreground (if we didn't do it already). + this.windowHandle = this.environmentInterface.FindMainWindowHandleOfProcess(processes[0]); + if (this.windowHandle != previousWindowToBringToForeground) + { + previousWindowToBringToForeground = this.windowHandle; + this.environmentInterface.BringWindowToForeground(this.windowHandle); + } - // Wait a bit so that the window can go into foreground. - await WaitSemaphoreInternalAsync(250, false); + // Wait a bit so that the window can go into foreground. + await WaitSemaphoreInternalAsync(250, false); - // If the window isn't in foreground, try again. - bool isInForeground; - this.environmentInterface.GetWindowPosition(this.windowHandle, out isInForeground, false); - if (isInForeground) - break; - } - else - { - if (lastInitializingEventParameter != true) - { - lastInitializingEventParameter = true; - this.simulator.OnSimulatorInitializing(lastInitializingEventParameter); + // If the window isn't in foreground, try again. + bool isInForeground; + this.environmentInterface.GetWindowPosition(this.windowHandle, out isInForeground, false); + if (isInForeground) + break; } - - // When there are multiple processes, wait until on of the windows goes into foreground. - bool foundWindow = false; - - foreach (var process in processes) + else { - try + if (lastInitializingEventParameter != true) { - var hWnd = this.environmentInterface.FindMainWindowHandleOfProcess(process); - bool isInForeground; - this.environmentInterface.GetWindowPosition(hWnd, out isInForeground, false); + lastInitializingEventParameter = true; + this.simulator.OnSimulatorInitializing(lastInitializingEventParameter); + } + + // When there are multiple processes, wait until on of the windows goes into foreground. + bool foundWindow = false; - if (isInForeground) + foreach (var process in processes) + { + try { - // OK, we found our window to use. - this.windowHandle = hWnd; - foundWindow = true; - break; + var hWnd = this.environmentInterface.FindMainWindowHandleOfProcess(process); + + bool isInForeground; + this.environmentInterface.GetWindowPosition(hWnd, out isInForeground, false); + + if (isInForeground) + { + // OK, we found our window to use. + this.windowHandle = hWnd; + foundWindow = true; + break; + } + } + catch + { + // Ignore } } - catch - { - // Ignore - } - } - if (foundWindow) - break; + if (foundWindow) + break; - // If non of the windows is in foreground, wait a bit and try again. - await WaitSemaphoreInternalAsync(250, false); + // If non of the windows is in foreground, wait a bit and try again. + await WaitSemaphoreInternalAsync(250, false); + } + } + finally + { + // Dispose of the processes after using them. + foreach (var process in processes) + process.Dispose(); } }