Home > database >  How to optimize CEFSharp in WinForms to render heavy applications (such as THREEJS)?
How to optimize CEFSharp in WinForms to render heavy applications (such as THREEJS)?

Time:06-21

I developed a winforms that shows the user a web application that uses 3D rendering (with THREEJS). The problem is that not every client has a good enough CPU to use this application. The application runs smoothly in almost every computer when accessing it through the browser, but it doesn't when accessing it through the winforms application. This is the CPU usage of every program:enter image description here

the same content when ran on Google Chrome uses 2.3% of the CPU, whereas my winforms app running CEFSharp uses 92.7%.

This is the code for my winforms component:

namespace tissuewebs
{
    public partial class Form1 : Form
    {
        public ChromiumWebBrowser chromeBrowser;
           
        public Form1()
        {
            InitializeComponent();
            InitializeChromium();             
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        private void InitializeChromium()
        {
            CefSettings settings = new CefSettings();
            settings.CefCommandLineArgs.Add("disable-features", "BlockInsecurePrivateNetworkRequests");
            settings.CefCommandLineArgs.Add("disable-gpu", "1");
            settings.RemoteDebuggingPort = 8088;
            settings.CachePath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)   @"\CEF";
            settings.PersistSessionCookies = true;
            CefSharp.Cef.Initialize(settings);

            chromeBrowser = new ChromiumWebBrowser("example.com")
            {
                Dock = DockStyle.Fill
            };
            this.Controls.Add(chromeBrowser);
        }
    }
}

Is it possible to optimize my application, so it uses less CPU?

CodePudding user response:

  • Firstly you need to leave GPU acceleration enabled. CPU usage with GPU acceleration disabled will be higher.

  • Secondly use a 64bit version for better performance. You are comparing Chrome running as a 64bit process to your 32bit application.

  • Thirdly you need to make sure your application has an app.manifest, with compatibility entries. Performance will be adversely reacted otherwise.

<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
    <application>
      <!-- A list of all Windows versions that this application is designed to work with. 
            Windows will automatically select the most compatible environment.-->

      <!-- Windows 7 -->
      <supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}" />
      <!-- Windows 8 -->
      <supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}" />
      <!-- Windows 8.1 -->
      <supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}" />
      <!-- Windows 10 -->
      <supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
    </application>
  </compatibility>

See https://github.com/cefsharp/CefSharp.MinimalExample/blob/master/CefSharp.MinimalExample.WinForms/app.manifest for a reference example. I'd also suggest making sure your application is DPI aware.


When comparing your application with Chrome it's important to make sure you are comparing like for like versions. Chrome 102 may perform differently to CefSharp 100 for example.

  • Related