@@ -69,70 +69,80 @@ public async Task InitializeAsync()
69
69
// First, find the game processes. This will always return at least one process,
70
70
// or throw.
71
71
var processes = this . environmentInterface . FindProcesses ( ) ;
72
- if ( processes . Count == 1 )
72
+ try
73
73
{
74
- if ( lastInitializingEventParameter != false )
74
+ if ( processes . Count == 1 )
75
75
{
76
- lastInitializingEventParameter = false ;
77
- this . simulator . OnSimulatorInitializing ( lastInitializingEventParameter ) ;
78
- }
76
+ if ( lastInitializingEventParameter != false )
77
+ {
78
+ lastInitializingEventParameter = false ;
79
+ this . simulator . OnSimulatorInitializing ( lastInitializingEventParameter ) ;
80
+ }
79
81
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
+ }
88
90
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 ) ;
91
93
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 ;
104
99
}
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
110
101
{
111
- try
102
+ if ( lastInitializingEventParameter != true )
112
103
{
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 ;
116
110
117
- if ( isInForeground )
111
+ foreach ( var process in processes )
112
+ {
113
+ try
118
114
{
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
123
131
}
124
132
}
125
- catch
126
- {
127
- // Ignore
128
- }
129
- }
130
133
131
- if ( foundWindow )
132
- break ;
134
+ if ( foundWindow )
135
+ break ;
133
136
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 ( ) ;
136
146
}
137
147
}
138
148
0 commit comments