OffScreen - Add async example

- Old example is still there for reference, just not used by default
- Add AsyncContext/SingleThreadSynchronizationContext to ensure async calls continue on main thread.
This commit is contained in:
Alex Maitland 2021-11-15 09:45:57 +10:00
parent 3cc3e642e9
commit a5d2bbe145
4 changed files with 246 additions and 55 deletions

View File

@ -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);
}
}
}
}

View File

@ -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>
@ -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" />

View File

@ -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.
@ -11,10 +11,19 @@ using System.Threading.Tasks;
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
@ -28,6 +37,106 @@ namespace CefSharp.MinimalExample.OffScreen
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));
}
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
@ -38,69 +147,76 @@ namespace CefSharp.MinimalExample.OffScreen
Cef.Initialize(settings, performDependencyCheck: true, browserProcessHandler: null);
// Create the offscreen Chromium browser.
browser = new ChromiumWebBrowser(testUrl);
var browser = new ChromiumWebBrowser(testUrl);
EventHandler<LoadingStateChangedEventArgs> handler = null;
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;
var scriptTask = browser.EvaluateScriptAsync("document.querySelector('[name=q]').value = 'CefSharp Was Here!'");
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");
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.
task.Result.Save(screenshotPath);
// 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("Image viewer launched. Press any key to exit.");
}, TaskScheduler.Default);
});
}
};
// An event that is fired when the first page is finished loading.
// This returns to us from another thread.
browser.LoadingStateChanged += BrowserLoadingStateChanged;
browser.LoadingStateChanged += handler;
// 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
// 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();
return 0;
}
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;
var scriptTask = browser.EvaluateScriptAsync("document.querySelector('[name=q]').value = 'CefSharp Was Here!'");
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");
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.
task.Result.Save(screenshotPath);
// 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("Image viewer launched. Press any key to exit.");
}, TaskScheduler.Default);
});
}
}
}
}

View File

@ -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();
}
}
}