Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork3k
SelfHost BrowserSubProcess
CefSharp uses the samemultiprocess model asChromium. When you launch an application that embedsCefSharp then by default you will see multiple instances ofCefSharp.BrowserSubProcess.exe inTask Manager.
It is possible and in some casesrequired that youSelf Host theBrowserSubProcess.Self Host means that multiple instances ofyour application exe will be launched instead ofCefSharp.BrowserSubProcess.exe.
- YouMUST set performDependencyCheck tofalse.
- Add anApplication Manifest to your exe if you don't already have one, it's required for Windows 10 compatibility, GPU detection, HighDPI support and tooltips. You can review the
CefSharp.BrowserSubProcess.exeapp.manifest for a working example. - For
.Net 6+when Publishing a Single file youmustSelf Host. - Better user experience when it comes to firewall permission prompts.
- DPI Awareness will match that of your application.
32bit executablesshould be madeLargeAddressAware. See alsohttps://github.com/cefsharp/CefSharp/wiki/General-Usage#win32-out-of-memory- For
64bitexecutables increasing theStack Size to 8mb to matchChromiumis recommended. - If running on
Terminal Serverthen make your applicationTSAware
Self Hosting usingWinForms is very easy, forx64/x86 build yourProgram.cs would look something like:
publicstaticclassProgram{[STAThread]publicstaticintMain(string[]args){varexitCode=CefSharp.BrowserSubprocess.SelfHost.Main(args);if(exitCode>=0){returnexitCode;}varsettings=newCefSettings(){//By default CefSharp will use an in-memory cache, you need to specify a Cache Folder to persist dataCachePath=Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),"CefSharp\\Cache"),BrowserSubprocessPath=System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName};//Perform dependency check to make sure all relevant resources are in our output directory.//IMPORTANT: MUST set performDependencyCheck falseCef.Initialize(settings,performDependencyCheck:false,browserProcessHandler:null);varbrowser=newBrowserForm();Application.Run(browser);return0;}}
ForWPF there are a few steps required as the compiler generates theMain method for you. You need to:
- Create your own custom entry point (typically adding a
Program.csfile with astatic Mainmethod) that callsApplication.Run - Change the
<StartupObject/>to use your own main method, (Right click Properties on your project, Startup Object is a dropdown or edit your proj file manually).
Yourproj file should look something like:
<PropertyGroup> <OutputType>WinExe</OutputType> <TargetFramework>net6.0-windows</TargetFramework> <UseWPF>true</UseWPF> <StartupObject>MyApp.Program</StartupObject></PropertyGroup>
Forx64/x64 build yourProgram.cs would look something like:
namespaceMyApp{publicstaticclassProgram{[STAThread]publicstaticintMain(string[]args){varexitCode=CefSharp.BrowserSubprocess.SelfHost.Main(args);if(exitCode>=0){returnexitCode;}varsettings=newCefSettings(){//By default CefSharp will use an in-memory cache, you need to specify a Cache Folder to persist dataCachePath=Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),"CefSharp\\Cache"),BrowserSubprocessPath=System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName};//IMPORTANT: MUST set performDependencyCheck falseCef.Initialize(settings,performDependencyCheck:false,browserProcessHandler:null);varapp=newApp();app.InitializeComponent();returnapp.Run();}}}