Feature Request - Add IBrowserProcessHandler.OnScheduleMessagePumpWork
CEF Now provides support for integrating into an existing message loop.
Resolved with https://github.com/cefsharp/CefSharp/commit/63ef6beb5f52853bebfec2c234352ca577bcef19
Breaking Change:
Cef.OnContextInitialized has been removed and is now part of the IBrowserProcessHandler interface.
Notes:
- Must set
setting.MultiThreadedMessageLoop = false; - Must set
setting.ExternalMessagePump= true; - When implementing
IBrowserProcessHandler.OnScheduleMessagePumpWorkyou should refer to theCEFreference implementation for ideas see https://bitbucket.org/chromiumembedded/cef/commits/1ff26aa02a656b3bc9f0712591c92849c5909e04?at=2785
Background:
The default is to use MultiThreadedMessageLoop, this provides excellent performance though has it's own problems in the message loop is run in a different thread to your main application. This makes it difficult for messages to be passed, like when you display the context menu, click on your app title bar, there menu will stay shown as the message loops aren't aware of the message triggered by clicking the title bar.
I've added a rough example, not production ready by any means.
https://github.com/cefsharp/CefSharp/search?l=C%23&q=OnScheduleMessagePumpWork
For WinForms Arrows and Tab keys don't work when integrating into a current message loop. I've applied a fix to master, you can either subclass ChromiumWebBrowser and override the same method or hook PreviewKeyDown and set e.IsInputKey=true; for those keys as described in http://stackoverflow.com/a/7815185
Fix in master is https://github.com/cefsharp/CefSharp/commit/e23ad38a1105c3932523511937f8308ec68939d8
Another example can be seen in JCEF, can be used as a reference implementation https://bitbucket.org/chromiumembedded/java-cef/src/5fc1533f7d42201578566697ce567ef6d55adf24/java/org/cef/CefApp.java?fileviewer=file-view-default#CefApp.java-505
Is integrating into a current message loop still an acceptable solution?
Also, I'm not thrilled with adding a timer that runs 30 times a second. It doesn't seem to adversely affect performance but it's always running ... even if a ChromiumWebBrowser is not being shown/used. Is there a way, maybe, to set up a WpfBrowserProcessHandler ... when using a ChromiumWebBrowser ... and then taking it down ... when I'm no longer using a ChromiumWebBrowser? That is, is there another way to hook this baby in ... instead of via Cef.Initialize?
Is integrating into a current message loop still an acceptable solution?
Yes, see https://github.com/cefsharp/CefSharp/wiki/General-Usage#multithreadedmessageloop
Is there a way, maybe, to set up a WpfBrowserProcessHandler ... when using a ChromiumWebBrowser ... and then taking it down ... when I'm no longer using a ChromiumWebBrowser? That is, is there another way to hook this baby in ... instead of via Cef.Initialize?
No, we're limited by the CEF API here.
http://magpcss.org/ceforum/apidocs3/projects/(default)/(_globals).html#CefInitialize(constCefMainArgs&,constCefSettings&,CefRefPtr%3CCefApp%3E,void*) http://magpcss.org/ceforum/apidocs3/projects/(default)/CefApp.html#GetBrowserProcessHandler() http://magpcss.org/ceforum/apidocs3/projects/(default)/CefBrowserProcessHandler.html#OnScheduleMessagePumpWork(int64)
There is nothing that says you have to run a Timer from within the IBrowserProcessHandler, you just need to call Cef.DoMessageLoopWork sufficiently frequently for your application to run smoothly (usually at least 30 times per second).
http://magpcss.org/ceforum/apidocs3/projects/(default)/(_globals).html#CefDoMessageLoopWork()
If you have further questions please ask them on https://gitter.im/cefsharp/CefSharp
Is there any chance of an example of how to integrate this? I am battling to find where to add what to get it to work. I would like to close my form with the browser control on from the JSObjects class when a certain event is fired from javascript and caught in VB . The event is working, but the cross thread error wont allow me to close the form. I do not know exactly how to implement the message pumping and what to add.
Maybe this quick hack helps someone. For the menu to close when clicking on the browser control I created a simple class file "ChromiumWebBrowser.cs" and added:
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace CefSharp.MinimalExample.WinForms
{
[Obsolete]
class ChromiumWebBrowser : CefSharp.WinForms.ChromiumWebBrowser
{
public ChromiumWebBrowser(string path) : base(path)
{
}
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
protected override void WndProc(ref Message m)
{
const int WM_PARENTNOTIFY = 0x0210;
const int WM_NCLBUTTONDOWN = 0x00A1;
//Console.WriteLine(m.Msg);
if (m.Msg == WM_PARENTNOTIFY)
{
PostMessage(Parent.Handle, WM_NCLBUTTONDOWN, IntPtr.Zero, IntPtr.Zero);
}
base.WndProc(ref m);
}
}
}