@@ -51,7 +51,7 @@ public static void Main(string[] args)
5151 Cef . Shutdown ( ) ;
5252 }
5353
54- private static async void BrowserLoadingStateChanged ( object sender , LoadingStateChangedEventArgs e )
54+ private static void BrowserLoadingStateChanged ( object sender , LoadingStateChangedEventArgs e )
5555 {
5656 // Check to see if loading is complete - this event is called twice, one when loading starts
5757 // second time when it's finished
@@ -61,49 +61,38 @@ private static async void BrowserLoadingStateChanged(object sender, LoadingState
6161 // Remove the load event handler, because we only want one snapshot of the initial page.
6262 browser . LoadingStateChanged -= BrowserLoadingStateChanged ;
6363
64- const string script = "(function(){ var elm = document.getElementById('lst-ib'); elm .value = 'CefSharp Was Here!'; return document.getElementsByName('btnK')[0].getBoundingClientRect(); })();" ;
64+ var scriptTask = browser . EvaluateScriptAsync ( " document.getElementById('lst-ib').value = 'CefSharp Was Here!'" ) ;
6565
66- var jsResponse = await browser . EvaluateScriptAsync ( script ) ;
67-
68- if ( jsResponse . Success )
66+ scriptTask . ContinueWith ( t =>
6967 {
70- dynamic domRect = jsResponse . Result ;
71-
72- var x = ( int ) ( domRect . x + ( domRect . width / 2 ) ) ;
73- var y = ( int ) ( domRect . y + ( domRect . height / 2 ) ) ;
74-
75- var host = browser . GetBrowserHost ( ) ;
76-
77- host . SendMouseClickEvent ( x , y , MouseButtonType . Left , false , 1 , CefEventFlags . None ) ;
78- Thread . Sleep ( 15 ) ;
79- host . SendMouseClickEvent ( x , y , MouseButtonType . Left , true , 1 , CefEventFlags . None ) ;
80-
8168 //Give the browser a little time to render
82- Thread . Sleep ( 2500 ) ;
69+ Thread . Sleep ( 500 ) ;
8370 // Wait for the screenshot to be taken.
84- var bitmap = await browser . ScreenshotAsync ( ) ;
85-
86- // Make a file to save it to (e.g. C:\Users\jan\Desktop\CefSharp screenshot.png)
87- var screenshotPath = Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . Desktop ) , "CefSharp screenshot.png" ) ;
71+ var task = browser . ScreenshotAsync ( ) ;
72+ task . ContinueWith ( x =>
73+ {
74+ // Make a file to save it to (e.g. C:\Users\jan\Desktop\CefSharp screenshot.png)
75+ var screenshotPath = Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . Desktop ) , "CefSharp screenshot.png" ) ;
8876
89- Console . WriteLine ( ) ;
90- Console . WriteLine ( "Screenshot ready. Saving to {0}" , screenshotPath ) ;
77+ Console . WriteLine ( ) ;
78+ Console . WriteLine ( "Screenshot ready. Saving to {0}" , screenshotPath ) ;
9179
92- // Save the Bitmap to the path.
93- // The image type is auto-detected via the ".png" extension.
94- bitmap . Save ( screenshotPath ) ;
80+ // Save the Bitmap to the path.
81+ // The image type is auto-detected via the ".png" extension.
82+ task . Result . Save ( screenshotPath ) ;
9583
96- // We no longer need the Bitmap.
97- // Dispose it to avoid keeping the memory alive. Especially important in 32-bit applications.
98- bitmap . Dispose ( ) ;
84+ // We no longer need the Bitmap.
85+ // Dispose it to avoid keeping the memory alive. Especially important in 32-bit applications.
86+ task . Result . Dispose ( ) ;
9987
100- Console . WriteLine ( "Screenshot saved. Launching your default image viewer..." ) ;
88+ Console . WriteLine ( "Screenshot saved. Launching your default image viewer..." ) ;
10189
102- // Tell Windows to launch the saved image.
103- Process . Start ( screenshotPath ) ;
90+ // Tell Windows to launch the saved image.
91+ Process . Start ( screenshotPath ) ;
10492
105- Console . WriteLine ( "Image viewer launched. Press any key to exit." ) ;
106- }
93+ Console . WriteLine ( "Image viewer launched. Press any key to exit." ) ;
94+ } , TaskScheduler . Default ) ;
95+ } ) ;
10796 }
10897 }
10998 }
0 commit comments