Skip to content

Commit

Permalink
OffScreen - Add async example
Browse files Browse the repository at this point in the history
- Old example is still there for reference, just not used by default
- Add AsyncContext/SingleThreadSynchronizationContext to ensure async calls continue on main thread.
  • Loading branch information
amaitland committed Nov 15, 2021
1 parent 3cc3e64 commit d3e8938
Show file tree
Hide file tree
Showing 4 changed files with 247 additions and 54 deletions.
38 changes: 38 additions & 0 deletions CefSharp.MinimalExample.OffScreen/AsyncContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/// https://devblogs.microsoft.com/pfxteam/await-synchronizationcontext-and-console-apps/

using System;
using System.Threading;
using System.Threading.Tasks;

namespace CefSharp.MinimalExample.OffScreen
{
public static class AsyncContext
{
public static void Run(Func<Task> func)
{
var prevCtx = SynchronizationContext.Current;

try
{
var syncCtx = new SingleThreadSynchronizationContext();

SynchronizationContext.SetSynchronizationContext(syncCtx);

var t = func();

t.ContinueWith(delegate
{
syncCtx.Complete();
}, TaskScheduler.Default);

syncCtx.RunOnCurrentThread();

t.GetAwaiter().GetResult();
}
finally
{
SynchronizationContext.SetSynchronizationContext(prevCtx);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@
<PropertyGroup>
<ApplicationManifest>app.manifest</ApplicationManifest>
</PropertyGroup>
<PropertyGroup>
<StartupObject>CefSharp.MinimalExample.OffScreen.Program</StartupObject>
</PropertyGroup>
<ItemGroup>
<Reference Include="CefSharp, Version=95.7.141.0, Culture=neutral, PublicKeyToken=40c4b6fc221f4138">
<HintPath>..\packages\CefSharp.Common.95.7.141\lib\net452\CefSharp.dll</HintPath>
Expand All @@ -103,8 +106,10 @@
<Reference Include="Microsoft.CSharp" />
</ItemGroup>
<ItemGroup>
<Compile Include="AsyncContext.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SingleThreadSynchronizationContext.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
Expand Down
226 changes: 172 additions & 54 deletions CefSharp.MinimalExample.OffScreen/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2010-2015 The CefSharp Authors. All rights reserved.
// Copyright © 2010-2021 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.

Expand All @@ -11,10 +11,19 @@

namespace CefSharp.MinimalExample.OffScreen
{
/// <summary>
/// CefSharp.OffScreen Minimal Example
/// </summary>
public class Program
{
private static ChromiumWebBrowser browser;

/// <summary>
/// Asynchronous demo using CefSharp.OffScreen
/// Loads google.com, uses javascript to fill out the search box then takes a screenshot which is opened
/// in the default image viewer.
/// For a synchronous demo see <see cref="MainSync(string[])"/> below.
/// </summary>
/// <param name="args">args</param>
/// <returns>exit code</returns>
public static int Main(string[] args)
{
#if ANYCPU
Expand All @@ -28,6 +37,108 @@ public static int Main(string[] args)
Console.WriteLine("You may see Chromium debugging output, please wait...");
Console.WriteLine();

//Console apps don't have a SynchronizationContext, so to ensure our await calls continue on the main thread we use a super simple implementation from
//https://devblogs.microsoft.com/pfxteam/await-synchronizationcontext-and-console-apps/
//Continuations will happen on the main thread. Cef.Initialize/Cef.Shutdown must be called on the same Thread.
//The Nito.AsyncEx.Context Nuget package has a more advanced implementation
//should you wish to use a pre-build implementation.
//https://github.com/StephenCleary/AsyncEx/blob/8a73d0467d40ca41f9f9cf827c7a35702243abb8/doc/AsyncContext.md#console-example-using-asynccontext
//NOTE: This is only required if you use await

AsyncContext.Run(async delegate
{
var settings = new CefSettings()
{
//By default CefSharp will use an in-memory cache, you need to specify a Cache Folder to persist data
CachePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "CefSharp\\Cache")
};

//Perform dependency check to make sure all relevant resources are in our output directory.
var success = await Cef.InitializeAsync(settings, performDependencyCheck: true, browserProcessHandler: null);

if (!success)
{
throw new Exception("Unable to initialize CEF, check the log file.");
}

// Create the CefSharp.OffScreen.ChromiumWebBrowser instance
using (var browser = new ChromiumWebBrowser(testUrl))
{
var initialLoadResponse = await browser.WaitForInitialLoadAsync();

if (!initialLoadResponse.Success)
{
throw new Exception(string.Format("Page load failed with ErrorCode:{0}, HttpStatusCode:{1}", initialLoadResponse.ErrorCode, initialLoadResponse.HttpStatusCode));
}

browser.ShowDevTools();

var response = await browser.EvaluateScriptAsync("document.querySelector('[name=q]').value = 'CefSharp Was Here!'");

//Give the browser a little time to render
await Task.Delay(500);
// Wait for the screenshot to be taken.
var bitmap = await browser.ScreenshotAsync();

// File path to save our screenshot e.g. C:\Users\{username}\Desktop\CefSharp screenshot.png
var screenshotPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "CefSharp screenshot.png");

Console.WriteLine();
Console.WriteLine("Screenshot ready. Saving to {0}", screenshotPath);

// Save the Bitmap to the path.
// The image type is auto-detected via the ".png" extension.
bitmap.Save(screenshotPath);

// We no longer need the Bitmap.
// Dispose it to avoid keeping the memory alive. Especially important in 32-bit applications.
bitmap.Dispose();

Console.WriteLine("Screenshot saved. Launching your default image viewer...");

// Tell Windows to launch the saved image.
Process.Start(new ProcessStartInfo(screenshotPath)
{
// UseShellExecute is false by default on .NET Core.
UseShellExecute = true
});

Console.WriteLine("Image viewer launched. Press any key to exit.");
}

// Wait for user to press a key before exit
Console.ReadKey();

// Clean up Chromium objects. You need to call this in your application otherwise
// you will get a crash when closing.
Cef.Shutdown();
});

return 0;
}

/// <summary>
/// Synchronous demo using CefSharp.OffScreen
/// Loads google.com, uses javascript to fill out the search box then takes a screenshot which is opened
/// in the default image viewer.
/// For a asynchronous demo see <see cref="Main(string[])"/> above.
/// To use this demo simply delete the <see cref="Main(string[])"/> method and rename this method to Main.
/// </summary>
/// <param name="args">args</param>
/// <returns>exit code</returns>
public static int MainSync(string[] args)
{
#if ANYCPU
//Only required for PlatformTarget of AnyCPU
CefRuntime.SubscribeAnyCpuAssemblyResolver();
#endif

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("You may see Chromium debugging output, please wait...");
Console.WriteLine();

var settings = new CefSettings()
{
//By default CefSharp will use an in-memory cache, you need to specify a Cache Folder to persist data
Expand All @@ -38,69 +149,76 @@ public static int Main(string[] args)
Cef.Initialize(settings, performDependencyCheck: true, browserProcessHandler: null);

// Create the offscreen Chromium browser.
browser = new ChromiumWebBrowser(testUrl);
var browser = new ChromiumWebBrowser(testUrl);

// An event that is fired when the first page is finished loading.
// This returns to us from another thread.
browser.LoadingStateChanged += BrowserLoadingStateChanged;
EventHandler<LoadingStateChangedEventArgs> handler = null;

// We have to wait for something, otherwise the process will exit too soon.
Console.ReadKey();
handler = (s, e) =>
{
// Check to see if loading is complete - this event is called twice, one when loading starts
// second time when it's finished
if (!e.IsLoading)
{
// Remove the load event handler, because we only want one snapshot of the page.
browser.LoadingStateChanged -= handler;
// Clean up Chromium objects. You need to call this in your application otherwise
// you will get a crash when closing.
Cef.Shutdown();
var scriptTask = browser.EvaluateScriptAsync("document.querySelector('[name=q]').value = 'CefSharp Was Here!'");
return 0;
}
scriptTask.ContinueWith(t =>
{
if(!t.Result.Success)
{
throw new Exception("EvaluateScriptAsync failed:" + t.Result.Message);
}
//Give the browser a little time to render
Thread.Sleep(500);
// Wait for the screenshot to be taken.
var task = browser.ScreenshotAsync();
task.ContinueWith(x =>
{
// File path to save our screenshot e.g. C:\Users\{username}\Desktop\CefSharp screenshot.png
var screenshotPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "CefSharp screenshot.png");
private static void BrowserLoadingStateChanged(object sender, LoadingStateChangedEventArgs e)
{
// Check to see if loading is complete - this event is called twice, one when loading starts
// second time when it's finished
// (rather than an iframe within the main frame).
if (!e.IsLoading)
{
// Remove the load event handler, because we only want one snapshot of the initial page.
browser.LoadingStateChanged -= BrowserLoadingStateChanged;
Console.WriteLine();
Console.WriteLine("Screenshot ready. Saving to {0}", screenshotPath);
var scriptTask = browser.EvaluateScriptAsync("document.querySelector('[name=q]').value = 'CefSharp Was Here!'");
// Save the Bitmap to the path.
// The image type is auto-detected via the ".png" extension.
task.Result.Save(screenshotPath);
scriptTask.ContinueWith(t =>
{
//Give the browser a little time to render
Thread.Sleep(500);
// Wait for the screenshot to be taken.
var task = browser.ScreenshotAsync();
task.ContinueWith(x =>
{
// 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");
// We no longer need the Bitmap.
// Dispose it to avoid keeping the memory alive. Especially important in 32-bit applications.
task.Result.Dispose();
Console.WriteLine("Screenshot saved. Launching your default image viewer...");
// Tell Windows to launch the saved image.
Process.Start(new ProcessStartInfo(screenshotPath)
{
// UseShellExecute is false by default on .NET Core.
UseShellExecute = true
});
Console.WriteLine();
Console.WriteLine("Screenshot ready. Saving to {0}", screenshotPath);
Console.WriteLine("Image viewer launched. Press any key to exit.");
}, TaskScheduler.Default);
});
}
};

// Save the Bitmap to the path.
// The image type is auto-detected via the ".png" extension.
task.Result.Save(screenshotPath);
// An event that is fired when the first page is finished loading.
// This returns to us from another thread.
browser.LoadingStateChanged += handler;

// We no longer need the Bitmap.
// Dispose it to avoid keeping the memory alive. Especially important in 32-bit applications.
task.Result.Dispose();
// We have to wait for something, otherwise the process will exit too soon.
Console.ReadKey();

Console.WriteLine("Screenshot saved. Launching your default image viewer...");
// Clean up Chromium objects. You need to call this in your application otherwise
// you will get a crash when closing.
//The ChromiumWebBrowser instance will be disposed
Cef.Shutdown();

// Tell Windows to launch the saved image.
Process.Start(new ProcessStartInfo(screenshotPath)
{
// UseShellExecute is false by default on .NET Core.
UseShellExecute = true
});
Console.WriteLine("Image viewer launched. Press any key to exit.");
}, TaskScheduler.Default);
});
}
return 0;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/// https://devblogs.microsoft.com/pfxteam/await-synchronizationcontext-and-console-apps/

using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading;

namespace CefSharp.MinimalExample.OffScreen
{
public sealed class SingleThreadSynchronizationContext : SynchronizationContext
{
private readonly BlockingCollection<KeyValuePair<SendOrPostCallback, object>> queue =
new BlockingCollection<KeyValuePair<SendOrPostCallback, object>>();

public override void Post(SendOrPostCallback d, object state)
{
queue.Add(new KeyValuePair<SendOrPostCallback, object>(d, state));
}

public void RunOnCurrentThread()
{
while (queue.TryTake(out var workItem, Timeout.Infinite))
{
workItem.Key(workItem.Value);
}
}

public void Complete()
{
queue.CompleteAdding();
}
}
}

0 comments on commit d3e8938

Please sign in to comment.