Files
cefsharp/CefSharp.Wpf.Example/Handlers/WpfBrowserProcessHandler.cs
T
2026-08-08 07:32:32 +10:00

87 lines
2.8 KiB
C#

// Copyright © 2016 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.Timers;
using System.Windows.Threading;
using CefSharp.Example.Handlers;
namespace CefSharp.Wpf.Example.Handlers
{
/// <summary>
/// EXPERIMENTAL - this implementation is very simplistic and not ready for production use
/// See the following link for the CEF reference implementation.
/// https://github.com/chromiumembedded/cef/commit/1ff26aa02a656b3bc9f0712591c92849c5909e04
/// </summary>
public class WpfBrowserProcessHandler : BrowserProcessHandler
{
private Timer timer;
private Dispatcher dispatcher;
public WpfBrowserProcessHandler(Dispatcher dispatcher)
{
timer = new Timer { Interval = ThirtyTimesPerSecond, AutoReset = true };
timer.Start();
timer.Elapsed += TimerTick;
this.dispatcher = dispatcher;
this.dispatcher.ShutdownStarted += DispatcherShutdownStarted;
}
private void DispatcherShutdownStarted(object sender, EventArgs e)
{
//If the dispatcher is shutting down then we stop the timer
if (timer != null)
{
timer.Stop();
}
}
private void TimerTick(object sender, EventArgs e)
{
//Basically execute Cef.DoMessageLoopWork 30 times per second, on the UI Thread
dispatcher.BeginInvoke((Action)(() => Cef.DoMessageLoopWork()), DispatcherPriority.Render);
}
protected override void OnScheduleMessagePumpWork(long delay)
{
//If the delay is greater than the Maximum then use ThirtyTimesPerSecond
//instead - we do this to achieve a minimum number of FPS
if (delay > ThirtyTimesPerSecond)
{
delay = ThirtyTimesPerSecond;
}
//When delay <= 0 we'll execute Cef.DoMessageLoopWork immediately
// if it's greater than we'll just let the Timer which fires 30 times per second
// care of the call
if (delay <= 0)
{
dispatcher.BeginInvoke((Action)(() => Cef.DoMessageLoopWork()), DispatcherPriority.Normal);
}
}
protected override void Dispose(bool disposing)
{
if(disposing)
{
if (dispatcher != null)
{
dispatcher.ShutdownStarted -= DispatcherShutdownStarted;
dispatcher = null;
}
if (timer != null)
{
timer.Stop();
timer.Dispose();
timer = null;
}
}
base.Dispose(disposing);
}
}
}