Skip to content

Commit

Permalink
Dispose of the processes after using them.
Browse files Browse the repository at this point in the history
  • Loading branch information
kpreisser committed Aug 12, 2018
1 parent 7b839ef commit 67c4a87
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ protected List<Process> 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();
}
}
Expand Down
110 changes: 60 additions & 50 deletions TTMouseclickSimulator/Core/Environment/StandardInteractionProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down

0 comments on commit 67c4a87

Please sign in to comment.