Ubuntu 24.04下CefGlue示例程序无法运行求助
解决Ubuntu下CefGlue运行循环报错问题
问题根源
你的错误是线程资源耗尽(pthread_create: Resource temporarily unavailable)和CoreCLR初始化失败,主要由以下原因导致:
- Windows风格路径在Linux下无效,Cef无法正常初始化缓存目录,引发重复初始化尝试
- 多线程消息循环配置与Linux下.NET运行时不兼容
- 不必要的sudo权限干扰了CoreCLR的正常启动
修复步骤
1. 修正Linux兼容的路径格式
把代码中的Windows路径替换为Linux路径,建议使用当前用户目录下的缓存文件夹(避免权限问题):
var userDir = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); var settings = new CefSettings { MultiThreadedMessageLoop = true, LogSeverity = CefLogSeverity.Default, LogFile = "./reddit_cef.log", // 当前目录生成日志文件 RootCachePath = $"{userDir}/.cef_root_cache", CachePath = $"{userDir}/.cef_cache", // 如果编译后输出目录有locales文件夹,需指定: // LocalesDirPath = "./locales" };
注意:确保这些目录存在,或者让Cef自动创建(当前用户需有写入权限)
2. 调整多线程消息循环配置
Linux下MultiThreadedMessageLoop = true易引发线程冲突,改为false并手动处理消息循环:
var settings = new CefSettings { MultiThreadedMessageLoop = false, // 修改为false // 其他配置保持不变 }; // 初始化前先执行进程检查 var exitCode = CefRuntime.ExecuteProcess(mainArgs, app, IntPtr.Zero); if (exitCode >= 0) { Environment.Exit(exitCode); } // 初始化Cef if (!CefRuntime.Initialize(mainArgs, settings, app, IntPtr.Zero)) { Console.WriteLine("Cef初始化失败"); return; } // 创建浏览器的代码保持不变... // 替换Console.ReadKey()为手动消息循环 Console.WriteLine("按任意键退出..."); while (true) { if (Console.KeyAvailable) { Console.ReadKey(true); break; } CefRuntime.DoMessageLoopWork(); Thread.Sleep(10); } CefRuntime.Shutdown();
3. 不要用sudo运行
直接用普通用户权限执行:
dotnet run
sudo会改变.NET运行时的环境变量和文件权限,导致CoreCLR初始化失败,普通用户权限足够运行CefGlue示例。
4. 确保Cef资源文件存在
检查项目输出目录下是否有locales文件夹和libcef.so等库文件,若缺失需从CefGlue编译产物中复制到输出目录,或在项目属性中设置自动复制这些文件到输出目录。
完整修正后的代码示例
using System; using System.Threading; using Xilium.CefGlue; namespace ConsoleApp1 { class Program { static void Main(string[] args) { var mainArgs = new CefMainArgs(args); var app = new cefApp(); CefRuntime.Load(); Console.WriteLine("Log for reddit.com"); var userDir = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); var settings = new CefSettings { MultiThreadedMessageLoop = false, LogSeverity = CefLogSeverity.Default, LogFile = "./reddit_cef.log", RootCachePath = $"{userDir}/.cef_root_cache", CachePath = $"{userDir}/.cef_cache", LocalesDirPath = "./locales" // 根据实际资源目录调整 }; // 进程预检查 var exitCode = CefRuntime.ExecuteProcess(mainArgs, app, IntPtr.Zero); if (exitCode >= 0) { Environment.Exit(exitCode); } if (!CefRuntime.Initialize(mainArgs, settings, app, IntPtr.Zero)) { Console.WriteLine("Cef初始化失败"); return; } var windowInfo = CefWindowInfo.Create(); windowInfo.SetAsPopup(IntPtr.Zero, "CefGlue Example"); var browserSettings = new CefBrowserSettings(); var client = new cefClient(); CefBrowserHost.CreateBrowser(windowInfo, client, browserSettings, "https://www.reddit.com"); Console.WriteLine("按任意键退出..."); while (true) { if (Console.KeyAvailable) { Console.ReadKey(true); break; } CefRuntime.DoMessageLoopWork(); Thread.Sleep(10); } CefRuntime.Shutdown(); } } class cefApp : CefApp { // 可选:添加Linux特定命令行参数,解决显示问题 protected override void OnBeforeCommandLineProcessing(string processType, CefCommandLine commandLine) { base.OnBeforeCommandLineProcessing(processType, commandLine); commandLine.AppendSwitch("disable-gpu"); commandLine.AppendSwitch("disable-gpu-compositing"); } } class cefClient : CefClient { } }
内容的提问来源于stack exchange,提问作者idk
相关产品推荐
相关产品推荐

