@@ -28,6 +28,11 @@ internal class StandardInteractionProvider : IInteractionProvider, IDisposable
28
28
// Window-relative (when using backgroundMode) or absolute mouse coordinates
29
29
private Coordinates ? lastSetMouseCoordinates ;
30
30
31
+ // Specifies we could already take the first sceenshot successfully.
32
+ private bool firstWindowScreenshotSuccessful ;
33
+
34
+ private bool canRetryOnException = true ;
35
+
31
36
public StandardInteractionProvider (
32
37
Simulator simulator ,
33
38
AbstractWindowsEnvironment environmentInterface ,
@@ -61,6 +66,7 @@ public void Dispose()
61
66
public async Task InitializeAsync ( )
62
67
{
63
68
this . lastSetMouseCoordinates = null ;
69
+ this . firstWindowScreenshotSuccessful = false ;
64
70
65
71
while ( true )
66
72
{
@@ -278,11 +284,30 @@ public IScreenshotContent GetCurrentWindowScreenshot()
278
284
// won't allow us to access the pixel data). If this will generally no longer work
279
285
// with a future verison of the game, we may need to revert using the screen copy
280
286
// also for background mode, but set the game window as topmost window.
287
+ bool fromScreen = ! this . backgroundMode ;
281
288
this . environmentInterface . CreateWindowScreenshot (
282
289
this . windowHandle ,
283
290
ref this . currentScreenshot ,
284
291
failIfNotInForeground : ! this . backgroundMode ,
285
- fromScreen : ! this . backgroundMode ) ;
292
+ fromScreen : fromScreen ) ;
293
+
294
+ if ( ! fromScreen && ! this . firstWindowScreenshotSuccessful )
295
+ {
296
+ // If we took the first screenshot from the window rather than the screen,
297
+ // check whether it only contains black pixels. In that case, throw an
298
+ // exception to inform the user that background mode won't work.
299
+ if ( this . currentScreenshot . ContainsOnlyBlackPixels ( ) )
300
+ {
301
+ // Don't allow to retry in this case since it would lead to the same
302
+ // exception.
303
+ this . canRetryOnException = false ;
304
+ throw new InvalidOperationException (
305
+ "Couldn't capture screenshot from window. " +
306
+ "Please disable background mode and try again." ) ;
307
+ }
308
+
309
+ this . firstWindowScreenshotSuccessful = true ;
310
+ }
286
311
287
312
return this . currentScreenshot ;
288
313
}
@@ -493,7 +518,7 @@ await Task.Delay(
493
518
494
519
private async ValueTask CheckRetryForExceptionAsync ( Exception ex , bool reinitialize )
495
520
{
496
- if ( this . simulator . AsyncRetryHandler is null )
521
+ if ( ! this . canRetryOnException || this . simulator . AsyncRetryHandler is null )
497
522
{
498
523
// Simply rethrow the exception.
499
524
ExceptionDispatchInfo . Throw ( ex ) ;
0 commit comments