refactor(startup): expose legacy process lifecycle

This commit is contained in:
2026-07-29 13:58:20 +08:00
parent e50a29c37a
commit a82bc39edf
2 changed files with 234 additions and 59 deletions
+158 -48
View File
@@ -21,11 +21,22 @@ using System.Resources;
using System.Runtime.InteropServices;
using System.Resources;
using System.Diagnostics;
using Lskj.Main.Hosting;
namespace Lskj.Main
{
static class Program
{
private static CefMainArgs _mainArgs;
private static DemoCefApp _cefApp;
private static CefSettings _cefSettings;
private static Messager _messageFilter;
private static EventHandler _cefIdleHandler;
private static AboutDevCompanion _devCompanion;
private static bool _cefInitialized;
private static bool _mainProcessInitialized;
private static bool _mainProcessShutdown;
//[DllImport("user32.dll")]
//private static extern bool SetProcessDpiAwarenessContext(IntPtr dpiContext);
@@ -36,61 +47,160 @@ namespace Lskj.Main
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
static int Main(string[] args)
{
//SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
CefRuntime.Load();//cef浏览器模块设置加载
var mainArgs = new CefMainArgs(args);
var app = new DemoCefApp();
var settings = new CefSettings
LegacyProcessStartResult processResult;
try
{
processResult = LegacyApplicationHost.PrepareProcess(args);
}
catch (Exception exception)
{
LogHelper.Instance.WriteError(exception);
return -1;
}
if (!processResult.ShouldRunApplication)
return processResult.ExitCode;
try
{
LegacyApplicationHost.InitializeMainProcess(null);
Manager.StartForm();
return 0;
}
catch (Exception exception)
{
MessageUtil.Show(exception);
LogHelper.Instance.WriteError(exception);
return -1;
}
finally
{
LegacyApplicationHost.ShutdownMainProcess();
}
}
internal static int PrepareProcess(string[] args)
{
if (_mainArgs != null)
throw new InvalidOperationException("CEF 进程分流已经执行。");
CefRuntime.Load();
_mainArgs = new CefMainArgs(args ?? new string[0]);
_cefApp = new DemoCefApp();
_cefSettings = CreateCefSettings();
int code = CefRuntime.ExecuteProcess(
_mainArgs,
_cefApp,
IntPtr.Zero);
Console.WriteLine(
"CefRuntime.ExecuteProcess() returns {0}",
code);
return code;
}
internal static void InitializeMainProcess()
{
if (_mainProcessInitialized)
throw new InvalidOperationException("旧主进程环境已经初始化。");
if (_mainProcessShutdown)
throw new InvalidOperationException("旧主进程环境已经关闭。");
if (_mainArgs == null || _cefApp == null || _cefSettings == null)
throw new InvalidOperationException("尚未执行 CEF 进程分流。");
try
{
CefRuntime.Initialize(
_mainArgs,
_cefSettings,
_cefApp,
IntPtr.Zero);
_cefInitialized = true;
Thread.CurrentThread.CurrentUICulture =
new CultureInfo("zh-CN");
BonusSkins.Register();
SkinManager.EnableFormSkins();
Application.SetUnhandledExceptionMode(
UnhandledExceptionMode.CatchException);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
_messageFilter = new Messager();
Application.AddMessageFilter(_messageFilter);
Application.ThreadException += Application_ThreadException;
AppDomain.CurrentDomain.UnhandledException +=
CurrentDomain_UnhandledException;
if (!_cefSettings.MultiThreadedMessageLoop)
{
_cefIdleHandler = delegate
{
CefRuntime.DoMessageLoopWork();
};
Application.Idle += _cefIdleHandler;
}
_devCompanion = new AboutDevCompanion(1, false);
_devCompanion.Run();
Register();
_mainProcessInitialized = true;
}
catch
{
ShutdownMainProcess();
throw;
}
}
internal static void ShutdownMainProcess()
{
if (_mainProcessShutdown)
return;
_mainProcessShutdown = true;
try
{
if (_cefIdleHandler != null)
{
Application.Idle -= _cefIdleHandler;
_cefIdleHandler = null;
}
Application.ThreadException -= Application_ThreadException;
AppDomain.CurrentDomain.UnhandledException -=
CurrentDomain_UnhandledException;
if (_messageFilter != null)
{
Application.RemoveMessageFilter(_messageFilter);
_messageFilter = null;
}
if (_devCompanion != null)
{
_devCompanion.Stop();
_devCompanion = null;
}
}
finally
{
if (_cefInitialized)
{
CefRuntime.Shutdown();
_cefInitialized = false;
}
_mainProcessInitialized = false;
}
}
private static CefSettings CreateCefSettings()
{
return new CefSettings
{
MultiThreadedMessageLoop = true,
LogSeverity = CefLogSeverity.Disable,
LogFile = "CefGlue.log",
Locale = "zh-CN"
};
try
{
var Code = CefRuntime.ExecuteProcess(mainArgs, app, IntPtr.Zero);
Console.WriteLine("CefRuntime.ExecuteProcess() returns {0}", Code);
}
catch (Exception ex)
{
}
CefRuntime.Initialize(mainArgs, settings, app, IntPtr.Zero);
try
{
// 汉化DevExpress界面
Thread.CurrentThread.CurrentUICulture = new CultureInfo("zh-CN");
// 设置皮肤
BonusSkins.Register();
SkinManager.EnableFormSkins();
//处理未捕获的异常
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.AddMessageFilter(new Messager());
Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
if (!settings.MultiThreadedMessageLoop)//消息循环
{
Application.Idle += (sender, e) => { CefRuntime.DoMessageLoopWork(); };
}
AboutDevCompanion DC = new AboutDevCompanion(1, false);
DC.Run();
Register();
// 启动程序
Manager.StartForm();
CefRuntime.Shutdown();
DC.Stop();
}
catch (Exception ex)
{
MessageUtil.Show(ex);
LogHelper.Instance.WriteError(ex);
}
}
/// <summary>
/// 允许 .NET Framework 4.8 外部宿主复用原程序的 CEF、皮肤、异常、注册和登录初始化。