diff --git a/CefSharp.OffScreen.Example/Program.cs b/CefSharp.OffScreen.Example/Program.cs
index a5ed1578d..953ccfff2 100644
--- a/CefSharp.OffScreen.Example/Program.cs
+++ b/CefSharp.OffScreen.Example/Program.cs
@@ -6,7 +6,6 @@ using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
-using System.Text;
using System.Threading.Tasks;
using CefSharp.Example;
@@ -14,63 +13,73 @@ namespace CefSharp.OffScreen.Example
{
public class Program
{
- private static ChromiumWebBrowser browser;
- private static bool captureFirstRenderedImage = false;
+ private const string TestUrl = "https://www.google.com/";
public static void Main(string[] args)
{
- const string testUrl = "https://www.google.com/";
-
- Console.WriteLine("This example application will load {0}, take a screenshot, and save it to your desktop.", testUrl);
+ Console.WriteLine("This example application will load {0}, take a screenshot, and save it to your desktop.", TestUrl);
Console.WriteLine("You may see a lot of Chromium debugging output, please wait...");
Console.WriteLine();
// You need to replace this with your own call to Cef.Initialize();
CefExample.Init();
- // Create the offscreen Chromium browser.
- using (browser = new ChromiumWebBrowser(testUrl))
- {
+ MainAsync();
- // An event that is fired when the first page is finished loading.
- // This returns to us from another thread.
- if (captureFirstRenderedImage)
- {
- browser.ResourceHandlerFactory.RegisterHandler(testUrl, ResourceHandler.FromString("
CefSharp OffScreen
"));
- browser.ScreenshotAsync().ContinueWith(DisplayBitmap);
- }
- else
- {
- browser.FrameLoadEnd += BrowserFrameLoadEnd;
- }
-
- // We have to wait for something, otherwise the process will exit too soon.
- Console.ReadKey();
- }
+ // We have to wait for something, otherwise the process will exit too soon.
+ Console.ReadKey();
// Clean up Chromium objects. You need to call this in your application otherwise
// you will get a crash when closing.
Cef.Shutdown();
}
- private static void BrowserFrameLoadEnd(object sender, FrameLoadEndEventArgs e)
+ private static async void MainAsync()
{
- // Check to ensure it is the main frame which has finished loading
- // (rather than an iframe within the main frame).
- if (e.IsMainFrame)
+
+ // Create the offscreen Chromium browser.
+ using (var browser = new ChromiumWebBrowser(TestUrl))
{
- // Remove the load event handler, because we only want one snapshot of the initial page.
- browser.FrameLoadEnd -= BrowserFrameLoadEnd;
+ await LoadPageAsync(browser);
// Wait for the screenshot to be taken.
- browser.ScreenshotAsync().ContinueWith(DisplayBitmap);
+ await browser.ScreenshotAsync().ContinueWith(DisplayBitmap);
+
+ await LoadPageAsync(browser, "http://github.com");
+
+ // Wait for the screenshot to be taken.
+ await browser.ScreenshotAsync().ContinueWith(DisplayBitmap);
}
}
+ public static Task LoadPageAsync(IWebBrowser browser, string address = null)
+ {
+ var tcs = new TaskCompletionSource();
+
+ EventHandler handler = null;
+ handler += (sender, args) =>
+ {
+ //Wait for while page to finish loading not just the first frame
+ if (!args.IsLoading)
+ {
+ browser.LoadingStateChanged -= handler;
+ tcs.TrySetResult(true);
+ }
+ };
+
+ browser.LoadingStateChanged += handler;
+
+ if (!string.IsNullOrEmpty(address))
+ {
+ browser.Load(address);
+ }
+ return tcs.Task;
+ }
+
private static void DisplayBitmap(Task task)
{
// Make a file to save it to (e.g. C:\Users\jan\Desktop\CefSharp screenshot.png)
- var screenshotPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "CefSharp screenshot.png");
+ var screenshotPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "CefSharp screenshot" + DateTime.Now.Ticks + ".png");
Console.WriteLine();
Console.WriteLine("Screenshot ready. Saving to {0}", screenshotPath);