FBro初始化
概述
浏览器在使用之前必须先初始化,初始化相关代码建议写在程序加载的入口,且只能调用一次。
对于C#应用程序,建议将初始化代码写在应用程序的主入口点Program类的Main函数中。
初始化步骤
1. 配置初始化参数
创建FBroSharpInitSet对象并配置相关参数:
csharp
static void Main()
{
FBroSharpInitSet set = new FBroSharpInitSet
{
// 设置缓存目录
cache_path = Directory.GetCurrentDirectory() + "\\Cache\\CachePath\\",
// 设置用户目录
user_data_path = Directory.GetCurrentDirectory() + "\\Cache\\UserPath\\",
// 设置根缓存目录,必须是上面缓存目录的父目录,否则采用独立缓存可能会失效
root_cache_path = Directory.GetCurrentDirectory() + "\\Cache\\",
// 启用事件消息循环
multi_threaded_message_loop = true,
// 设置执行子进程,不设置则会默认以当前进程作为子进程
browser_subprocess_path = Directory.GetCurrentDirectory() + "\\FBroSubprocess.exe",
// 本地化语言
locale = "zh-CN",
// 日志模式,只有出现错误的时候才记录日志
log_severity = FBroSharpLogSeverity.ERROR
};
// 创建初始化事件
InitEvent init_event = new InitEvent();
// 执行初始化
if (!FBroSharpInitControl.InitPro(set, init_event))
return;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
// 关闭框架释放资源
FBroSharpInitControl.Shutdown(false);
}2. 初始化参数说明
| 参数 | 类型 | 说明 |
|---|---|---|
cache_path | string | 缓存目录路径 |
user_data_path | string | 用户数据目录路径 |
root_cache_path | string | 根缓存目录,必须是缓存目录的父目录 |
multi_threaded_message_loop | bool | 是否启用多线程消息循环 |
browser_subprocess_path | string | 子进程可执行文件路径 |
locale | string | 本地化语言设置 |
log_severity | FBroSharpLogSeverity | 日志级别 |
注意事项
- 唯一性:初始化方法只能调用一次
- 路径关系:
root_cache_path必须是cache_path的父目录 - 资源释放:程序退出前必须调用
Shutdown方法释放资源
InitEvent.cs代码
using FBroSharp;
using FBroSharp.Const;
using FBroSharp.Event;
using FBroSharp.Lib;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
namespace BaseTest
{
public class InitEvent : FBroInitEvent
{
//即将处理命令行
public override void OnBeforeCommandLineProcessing(string process_type, IFBroSharpCommandLine cmd)
{
//if (Debugger.IsAttached)
//{
// cmd.AppendSwitch("--single-process");
//}
////关闭GPU加速
//cmd.AppendSwitchWithValue("--disable-gpu", "1");
//cmd.AppendSwitchWithValue("--disable-gpu-compositing", "1");
cmd.DisableGpuBlockList();
cmd.DisableGpuCache();
Console.WriteLine(MethodBase.GetCurrentMethod().Name + ":" + process_type + cmd.IsReadOnly() + cmd.GetProgram());
}
//初始化完毕
public override void OnContextInitialized()
{
Console.WriteLine(MethodBase.GetCurrentMethod().Name);
}
public override void OnBeforeChildProcessLaunch(IFBroSharpCommandLine command_line)
{
Console.WriteLine(MethodBase.GetCurrentMethod().Name + ":" + command_line.IsReadOnly() + command_line.GetProgram());
}
public override bool OnProcessMessageReceived(IFBroSharpBrowser browser, IFBroSharpFrame frame, FBroSharpProcessId source_process, IFBroSharpProcessMessage message)
{
var list = message.GetArgumentList();
//因为这里跨进程了,所有用信息框显示,而且要用当前exe作为执行进程才会有用
MessageBox.Show(message.GetName() + ":" + list.GetString(0), MethodBase.GetCurrentMethod().Name);
//返回真才会释放当前数据,代表数据处理完了
return true;
}
}
}