refactor(startup): expose legacy process lifecycle
This commit is contained in:
@@ -15,26 +15,91 @@ using Lskj.Util;
|
||||
|
||||
namespace Lskj.Main.Hosting
|
||||
{
|
||||
public sealed class LegacyProcessStartResult
|
||||
{
|
||||
internal LegacyProcessStartResult(
|
||||
bool shouldRunApplication,
|
||||
int exitCode)
|
||||
{
|
||||
ShouldRunApplication = shouldRunApplication;
|
||||
ExitCode = exitCode;
|
||||
}
|
||||
|
||||
public bool ShouldRunApplication { get; private set; }
|
||||
|
||||
public int ExitCode { get; private set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 为独立的新启动程序公开原 Ls_ERP 初始化和登录流程。
|
||||
/// </summary>
|
||||
public static class LegacyApplicationHost
|
||||
{
|
||||
public static void Run(string[] args, IExternalMainShell externalMainShell)
|
||||
{
|
||||
if (externalMainShell == null)
|
||||
throw new ArgumentNullException("externalMainShell");
|
||||
if (Manager.ExternalMainShell != null)
|
||||
throw new InvalidOperationException("旧程序已经由另一个外部主界面托管。");
|
||||
private static readonly object ProcessSync = new object();
|
||||
private static bool _mainProcessInitializationStarted;
|
||||
private static bool _mainProcessInitialized;
|
||||
private static bool _mainProcessShutdown;
|
||||
|
||||
Manager.ExternalMainShell = externalMainShell;
|
||||
try
|
||||
public static LegacyProcessStartResult PrepareProcess(string[] args)
|
||||
{
|
||||
int code = Program.PrepareProcess(args ?? new string[0]);
|
||||
return new LegacyProcessStartResult(code == -1, code);
|
||||
}
|
||||
|
||||
public static void InitializeMainProcess(IExternalMainShell externalMainShell)
|
||||
{
|
||||
lock (ProcessSync)
|
||||
{
|
||||
Program.RunFromExternalHost(args);
|
||||
if (_mainProcessInitialized)
|
||||
throw new InvalidOperationException(
|
||||
"旧主进程运行时已经初始化。");
|
||||
if (_mainProcessShutdown)
|
||||
throw new InvalidOperationException(
|
||||
"旧主进程运行时已经关闭。");
|
||||
|
||||
_mainProcessInitializationStarted = true;
|
||||
Manager.ExternalMainShell = externalMainShell;
|
||||
try
|
||||
{
|
||||
Program.InitializeMainProcess();
|
||||
_mainProcessInitialized = true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
try
|
||||
{
|
||||
Program.ShutdownMainProcess();
|
||||
}
|
||||
finally
|
||||
{
|
||||
_mainProcessShutdown = true;
|
||||
Manager.ExternalMainShell = null;
|
||||
}
|
||||
throw;
|
||||
}
|
||||
}
|
||||
finally
|
||||
}
|
||||
|
||||
public static void ShutdownMainProcess()
|
||||
{
|
||||
lock (ProcessSync)
|
||||
{
|
||||
Manager.ExternalMainShell = null;
|
||||
if (!_mainProcessInitializationStarted ||
|
||||
_mainProcessShutdown)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
Program.ShutdownMainProcess();
|
||||
}
|
||||
finally
|
||||
{
|
||||
_mainProcessInitialized = false;
|
||||
_mainProcessShutdown = true;
|
||||
Manager.ExternalMainShell = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user