Skip to content

Commit 67c4a87

Browse files
committed
Dispose of the processes after using them.
1 parent 7b839ef commit 67c4a87

File tree

2 files changed

+62
-52
lines changed

2 files changed

+62
-52
lines changed

TTMouseclickSimulator/Core/Environment/AbstractWindowsEnvironment.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ protected List<Process> FindProcessesByName(string processname)
3636
}
3737
catch
3838
{
39-
// Ignore
40-
// Dispose of the process since we won't use it.
39+
// We cannot access the process, so dispose of it since we
40+
// won't use it.
4141
p.Dispose();
4242
}
4343
}

TTMouseclickSimulator/Core/Environment/StandardInteractionProvider.cs

+60-50
Original file line numberDiff line numberDiff line change
@@ -69,70 +69,80 @@ public async Task InitializeAsync()
6969
// First, find the game processes. This will always return at least one process,
7070
// or throw.
7171
var processes = this.environmentInterface.FindProcesses();
72-
if (processes.Count == 1)
72+
try
7373
{
74-
if (lastInitializingEventParameter != false)
74+
if (processes.Count == 1)
7575
{
76-
lastInitializingEventParameter = false;
77-
this.simulator.OnSimulatorInitializing(lastInitializingEventParameter);
78-
}
76+
if (lastInitializingEventParameter != false)
77+
{
78+
lastInitializingEventParameter = false;
79+
this.simulator.OnSimulatorInitializing(lastInitializingEventParameter);
80+
}
7981

80-
// When there is only one process, we simply bring the window to the
81-
// foreground (if we didn't do it already).
82-
this.windowHandle = this.environmentInterface.FindMainWindowHandleOfProcess(processes[0]);
83-
if (this.windowHandle != previousWindowToBringToForeground)
84-
{
85-
previousWindowToBringToForeground = this.windowHandle;
86-
this.environmentInterface.BringWindowToForeground(this.windowHandle);
87-
}
82+
// When there is only one process, we simply bring the window to the
83+
// foreground (if we didn't do it already).
84+
this.windowHandle = this.environmentInterface.FindMainWindowHandleOfProcess(processes[0]);
85+
if (this.windowHandle != previousWindowToBringToForeground)
86+
{
87+
previousWindowToBringToForeground = this.windowHandle;
88+
this.environmentInterface.BringWindowToForeground(this.windowHandle);
89+
}
8890

89-
// Wait a bit so that the window can go into foreground.
90-
await WaitSemaphoreInternalAsync(250, false);
91+
// Wait a bit so that the window can go into foreground.
92+
await WaitSemaphoreInternalAsync(250, false);
9193

92-
// If the window isn't in foreground, try again.
93-
bool isInForeground;
94-
this.environmentInterface.GetWindowPosition(this.windowHandle, out isInForeground, false);
95-
if (isInForeground)
96-
break;
97-
}
98-
else
99-
{
100-
if (lastInitializingEventParameter != true)
101-
{
102-
lastInitializingEventParameter = true;
103-
this.simulator.OnSimulatorInitializing(lastInitializingEventParameter);
94+
// If the window isn't in foreground, try again.
95+
bool isInForeground;
96+
this.environmentInterface.GetWindowPosition(this.windowHandle, out isInForeground, false);
97+
if (isInForeground)
98+
break;
10499
}
105-
106-
// When there are multiple processes, wait until on of the windows goes into foreground.
107-
bool foundWindow = false;
108-
109-
foreach (var process in processes)
100+
else
110101
{
111-
try
102+
if (lastInitializingEventParameter != true)
112103
{
113-
var hWnd = this.environmentInterface.FindMainWindowHandleOfProcess(process);
114-
bool isInForeground;
115-
this.environmentInterface.GetWindowPosition(hWnd, out isInForeground, false);
104+
lastInitializingEventParameter = true;
105+
this.simulator.OnSimulatorInitializing(lastInitializingEventParameter);
106+
}
107+
108+
// When there are multiple processes, wait until on of the windows goes into foreground.
109+
bool foundWindow = false;
116110

117-
if (isInForeground)
111+
foreach (var process in processes)
112+
{
113+
try
118114
{
119-
// OK, we found our window to use.
120-
this.windowHandle = hWnd;
121-
foundWindow = true;
122-
break;
115+
var hWnd = this.environmentInterface.FindMainWindowHandleOfProcess(process);
116+
117+
bool isInForeground;
118+
this.environmentInterface.GetWindowPosition(hWnd, out isInForeground, false);
119+
120+
if (isInForeground)
121+
{
122+
// OK, we found our window to use.
123+
this.windowHandle = hWnd;
124+
foundWindow = true;
125+
break;
126+
}
127+
}
128+
catch
129+
{
130+
// Ignore
123131
}
124132
}
125-
catch
126-
{
127-
// Ignore
128-
}
129-
}
130133

131-
if (foundWindow)
132-
break;
134+
if (foundWindow)
135+
break;
133136

134-
// If non of the windows is in foreground, wait a bit and try again.
135-
await WaitSemaphoreInternalAsync(250, false);
137+
// If non of the windows is in foreground, wait a bit and try again.
138+
await WaitSemaphoreInternalAsync(250, false);
139+
}
140+
}
141+
finally
142+
{
143+
// Dispose of the processes after using them.
144+
foreach (var process in processes)
145+
process.Dispose();
136146
}
137147
}
138148

0 commit comments

Comments
 (0)