2019-05-15 21:27:56 +10:00
|
|
|
// Copyright © 2019 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.Threading.Tasks;
|
|
|
|
|
using System.Timers;
|
|
|
|
|
using CefSharp.Example.Handlers;
|
|
|
|
|
|
|
|
|
|
namespace CefSharp.WinForms.Example.Handlers
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Integreates CEF into the WinForms message loop,
|
2019-06-02 20:34:44 +10:00
|
|
|
/// This implementation is very simplistic, the timer fires roughly <see cref="BrowserProcessHandler.ThirtyTimesPerSecond"/>
|
2019-05-15 21:27:56 +10:00
|
|
|
/// times per second calling Cef.DoMessageLoopWork on the WinForms UI Thread. When OnScheduleMessagePumpWork
|
|
|
|
|
/// is called with a delay of less than or equal to 0 then Cef.DoMessageLoopWork is called as CEF has signaled
|
|
|
|
|
/// that it needs to perform work.
|
|
|
|
|
/// See the following link for the CEF reference implementation that containes a more complex example that maybe
|
|
|
|
|
/// required in some circumstances.
|
|
|
|
|
/// https://bitbucket.org/chromiumembedded/cef/commits/1ff26aa02a656b3bc9f0712591c92849c5909e04?at=2785
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class ScheduleMessagePumpBrowserProcessHandler : BrowserProcessHandler
|
|
|
|
|
{
|
|
|
|
|
private Timer timer;
|
|
|
|
|
private TaskFactory factory;
|
|
|
|
|
|
|
|
|
|
public ScheduleMessagePumpBrowserProcessHandler(TaskScheduler scheduler)
|
|
|
|
|
{
|
|
|
|
|
factory = new TaskFactory(scheduler);
|
2019-06-02 20:34:44 +10:00
|
|
|
timer = new Timer { Interval = ThirtyTimesPerSecond, AutoReset = true };
|
2019-05-15 21:27:56 +10:00
|
|
|
timer.Start();
|
|
|
|
|
timer.Elapsed += TimerTick;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void TimerTick(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
//Basically execute Cef.DoMessageLoopWork 30 times per second
|
|
|
|
|
//Execute DoMessageLoopWork on UI thread
|
|
|
|
|
factory.StartNew(() => Cef.DoMessageLoopWork());
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-11 20:24:48 +10:00
|
|
|
protected override void OnScheduleMessagePumpWork(long delay)
|
2019-05-15 21:27:56 +10:00
|
|
|
{
|
2021-05-11 20:24:48 +10:00
|
|
|
//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;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-15 21:27:56 +10:00
|
|
|
//when delay <= 0 queue the Task up for execution on the UI thread.
|
|
|
|
|
if (delay <= 0)
|
|
|
|
|
{
|
|
|
|
|
//Update the timer to execute almost immediately
|
|
|
|
|
factory.StartNew(() => Cef.DoMessageLoopWork());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-11 20:24:48 +10:00
|
|
|
protected override void Dispose(bool disposing)
|
2019-05-15 21:27:56 +10:00
|
|
|
{
|
2021-05-11 20:24:48 +10:00
|
|
|
if(disposing)
|
2019-05-15 21:27:56 +10:00
|
|
|
{
|
2021-05-11 20:24:48 +10:00
|
|
|
if (timer != null)
|
|
|
|
|
{
|
|
|
|
|
timer.Stop();
|
|
|
|
|
timer.Dispose();
|
|
|
|
|
timer = null;
|
|
|
|
|
}
|
2019-05-15 21:27:56 +10:00
|
|
|
}
|
2021-05-11 20:24:48 +10:00
|
|
|
|
|
|
|
|
base.Dispose(disposing);
|
2019-05-15 21:27:56 +10:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|