-
-
Save amaitland/3fbcc2b8a0316efabba6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright © 2010-2015 The CefSharp Authors. All rights reserved. | |
// | |
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. | |
using System; | |
using System.Diagnostics; | |
using System.Threading.Tasks; | |
using CefSharp.Example; | |
namespace CefSharp.OffScreen.Example | |
{ | |
public class Program | |
{ | |
private const string TestUrl = "https://www.google.com/"; | |
public static void Main(string[] args) | |
{ | |
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(); | |
MainAsync(); | |
// 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 async void MainAsync() | |
{ | |
// Create the offscreen Chromium browser. | |
using (var browser = new ChromiumWebBrowser(TestUrl)) | |
{ | |
await LoadPageAsync(browser); | |
var response = await browser.EvaluateScriptAsync("1 + 1"); | |
Debug.Assert(response.Result.ToString() == "2"); | |
Debugger.Break(); | |
} | |
} | |
public static Task LoadPageAsync(IWebBrowser browser, string address = null) | |
{ | |
var tcs = new TaskCompletionSource<bool>(); | |
EventHandler<LoadingStateChangedEventArgs> 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; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment