CefSharp icon indicating copy to clipboard operation
CefSharp copied to clipboard

Feature Request - Add IBrowserProcessHandler.OnScheduleMessagePumpWork

Open amaitland opened this issue 10 years ago • 8 comments

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.OnScheduleMessagePumpWork you should refer to the CEF reference 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.

amaitland avatar Jul 20 '16 03:07 amaitland

I've added a rough example, not production ready by any means.

https://github.com/cefsharp/CefSharp/search?l=C%23&q=OnScheduleMessagePumpWork

amaitland avatar Sep 08 '16 06:09 amaitland

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

amaitland avatar Oct 10 '16 04:10 amaitland

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

amaitland avatar Dec 19 '16 21:12 amaitland

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?

cplotts avatar Jul 25 '18 17:07 cplotts

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)

amaitland avatar Jul 25 '18 21:07 amaitland

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

amaitland avatar Jul 25 '18 21:07 amaitland

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.

JacquesOkes avatar Oct 29 '19 13:10 JacquesOkes

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

ghost avatar Aug 31 '20 08:08 ghost