From cfb94fa078ddde781efd602888ad491d72255b52 Mon Sep 17 00:00:00 2001 From: amaitland Date: Fri, 27 Sep 2019 08:38:21 +1000 Subject: [PATCH] WPF Example - ChromiumWebBrowserWithScreenshotSupport.TakeScreenshot replace timeout with cancellationToken Remove the Timeout param and replace with CancellationToken Use CancellationTokenSource.CancelAfter to perform a timeout https://docs.microsoft.com/en-us/dotnet/api/system.threading.cancellationtokensource.cancelafter?view=netframework-4.8 --- ...ChromiumWebBrowserWithScreenshotSupport.cs | 29 ++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/CefSharp.Wpf.Example/Controls/ChromiumWebBrowserWithScreenshotSupport.cs b/CefSharp.Wpf.Example/Controls/ChromiumWebBrowserWithScreenshotSupport.cs index 30b779dd1..f90fa6fd7 100644 --- a/CefSharp.Wpf.Example/Controls/ChromiumWebBrowserWithScreenshotSupport.cs +++ b/CefSharp.Wpf.Example/Controls/ChromiumWebBrowserWithScreenshotSupport.cs @@ -8,13 +8,13 @@ using System.Diagnostics; using System.IO; using System.IO.MemoryMappedFiles; using System.Runtime.InteropServices; +using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Input; using System.Windows.Interop; using System.Windows.Media; using System.Windows.Media.Imaging; -using CefSharp.Internals; using GalaSoft.MvvmLight.Command; using Size = System.Windows.Size; @@ -29,13 +29,14 @@ namespace CefSharp.Wpf.Example.Controls private static extern void CopyMemory(IntPtr dest, IntPtr src, uint count); private static readonly PixelFormat PixelFormat = PixelFormats.Pbgra32; - private static int BytesPerPixel = PixelFormat.BitsPerPixel / 8; + private static readonly int BytesPerPixel = PixelFormat.BitsPerPixel / 8; private volatile bool isTakingScreenshot = false; private Size? screenshotSize; private int oldFrameRate; private int ignoreFrames = 0; private TaskCompletionSource screenshotTaskCompletionSource; + private CancellationTokenRegistration? cancellationTokenRegistration; public ICommand ScreenshotCommand { get; set; } @@ -44,7 +45,7 @@ namespace CefSharp.Wpf.Example.Controls ScreenshotCommand = new RelayCommand(TakeScreenshot); } - public Task TakeScreenshot(Size screenshotSize, int? frameRate = 1, int? ignoreFrames = 0, TimeSpan? timeout = null) + public Task TakeScreenshot(Size screenshotSize, int? frameRate = 1, int? ignoreFrames = 0, CancellationToken? cancellationToken = null) { if (screenshotTaskCompletionSource != null && screenshotTaskCompletionSource.Task.Status == TaskStatus.Running) { @@ -70,9 +71,16 @@ namespace CefSharp.Wpf.Example.Controls screenshotTaskCompletionSource = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - if (timeout.HasValue) + if (cancellationToken.HasValue) { - screenshotTaskCompletionSource = screenshotTaskCompletionSource.WithTimeout(timeout.Value); + var token = cancellationToken.Value; + cancellationTokenRegistration = token.Register(() => + { + screenshotTaskCompletionSource.TrySetCanceled(); + + cancellationTokenRegistration?.Dispose(); + + }, useSynchronizationContext: false); } if (frameRate.HasValue) @@ -142,15 +150,10 @@ namespace CefSharp.Wpf.Example.Controls //Let the browser know the size changes so normal rendering can continue browserHost.WasResized(); - if (viewAccessor != null) - { - viewAccessor.Dispose(); - } + viewAccessor?.Dispose(); + mappedFile?.Dispose(); - if (mappedFile != null) - { - mappedFile.Dispose(); - } + cancellationTokenRegistration?.Dispose(); })); } }