Process启动进程研究,准备工作完成

This commit is contained in:
大石头 2024-05-16 16:36:57 +08:00
parent 0ff2a9fb63
commit ec7bf886b6
8 changed files with 218 additions and 0 deletions

49
Doc/ProcessStart.md Normal file
View File

@ -0,0 +1,49 @@
Process启动进程研究
## 研究目标
A应用通过Process类启动B应用研究不同参数设置下的测试结果。
## 测试准备
.Net版本net8.0
星尘代理:/root/agent
A目录/root/testA
B目录/root/testB
跟随退出随着A应用退出B应用跟随退出
## 分类测试
根据不同类型的B应用分类测试。
#### NewLife应用测试
要求B应用必须引入NewLife.Core它能收到环境变量BasePath并自动调整当前目录。
| 启动目录 | ShellExecute | WorkingDirectory | Environment | 合并输出 | 跟随退出 | 结果 |
| ----------- | :----------: | ---------------- | ----------- | -------- | -------- | ---- |
| /root/testA | false | | | | | |
| /root/testA | false | | | | | |
| /root/testA | true | | | | | |
| /root/testA | true | | | | | |
#### Net8应用测试
要求B应用是普通net8应用禁止引入NewLife.Core。
#### 非托管应用测试
要求B应用必须是非托管应用。

46
Samples/TestA/Program.cs Normal file
View File

@ -0,0 +1,46 @@
using System.Diagnostics;
using NewLife;
using NewLife.Log;
XTrace.UseConsole();
XTrace.WriteLine("TestA启动PID={0}", Process.GetCurrentProcess().Id);
XTrace.WriteLine("测试参数:{0}", args.Join(" "));
var target = "TestB";
if (args.Contains("-c")) target = "TestC";
var old = Process.GetProcesses().FirstOrDefault(e => e.ProcessName == target);
if (old != null)
{
XTrace.WriteLine("关闭进程 {0} {1}", old.Id, old.ProcessName);
old.Kill();
}
var si = new ProcessStartInfo
{
FileName = Runtime.Windows ? $"../{target}/{target}.exe" : $"../{target}/{target}",
Arguments = "-name NewLife",
//WorkingDirectory = "",
//UseShellExecute = false,
};
if (args.Contains("-s")) si.UseShellExecute = true;
if (args.Contains("-w")) si.WorkingDirectory = ".";
if (args.Contains("-e")) si.Environment["star"] = "dust";
Environment.SetEnvironmentVariable("BasePath", si.WorkingDirectory.GetFullPath());
var p = Process.Start(si);
if (p == null || p.WaitForExit(3_000) && p.ExitCode != 0)
{
XTrace.WriteLine("启动失败ExitCode={0}", p?.ExitCode);
}
else
{
XTrace.WriteLine("启动成功PID={0}", p.Id);
}
Console.WriteLine("TestA OK!");
//Console.ReadKey();
Thread.Sleep(5_000);

View File

@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<Company>新生命开发团队</Company>
<Copyright>©2002-2024 NewLife</Copyright>
<VersionPrefix>1.0</VersionPrefix>
<VersionSuffix>$([System.DateTime]::Now.ToString(`yyyy.MMdd`))</VersionSuffix>
<Version>$(VersionPrefix).$(VersionSuffix)</Version>
<FileVersion>$(Version)</FileVersion>
<AssemblyVersion>$(VersionPrefix).*</AssemblyVersion>
<Deterministic>false</Deterministic>
<OutputPath>..\..\Bin\Samples\TestA</OutputPath>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NewLife.Core" Version="10.10.2024.507" />
</ItemGroup>
</Project>

25
Samples/TestB/Program.cs Normal file
View File

@ -0,0 +1,25 @@
using System.Diagnostics;
using NewLife;
using NewLife.Log;
XTrace.UseConsole();
XTrace.WriteLine("TestB启动PID={0}", Process.GetCurrentProcess().Id);
XTrace.WriteLine("测试参数:{0}", args.Join(" "));
XTrace.WriteLine("CurrentDirectory\t{0}", Environment.CurrentDirectory);
XTrace.WriteLine("BaseDirectory\t{0}", AppDomain.CurrentDomain.BaseDirectory);
XTrace.WriteLine("BasePath\t{0}", PathHelper.BasePath);
XTrace.WriteLine("BaseDirectory\t{0}", PathHelper.BaseDirectory);
var envs = new[] { "BasePath", "star" };
XTrace.WriteLine("环境变量:");
var dic = Runtime.GetEnvironmentVariables().OrderBy(e => e.Key).ToDictionary(e => e.Key, e => e.Value);
foreach (var item in dic)
{
if (item.Key.EqualIgnoreCase(envs))
XTrace.WriteLine("{0}:\t{1}", item.Key, item.Value);
}
Console.WriteLine("TestB OK!");
Console.ReadKey();

View File

@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<Company>新生命开发团队</Company>
<Copyright>©2002-2024 NewLife</Copyright>
<VersionPrefix>1.0</VersionPrefix>
<VersionSuffix>$([System.DateTime]::Now.ToString(`yyyy.MMdd`))</VersionSuffix>
<Version>$(VersionPrefix).$(VersionSuffix)</Version>
<FileVersion>$(Version)</FileVersion>
<AssemblyVersion>$(VersionPrefix).*</AssemblyVersion>
<Deterministic>false</Deterministic>
<OutputPath>..\..\Bin\Samples\TestB</OutputPath>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NewLife.Core" Version="10.10.2024.507" />
</ItemGroup>
</Project>

7
Samples/TestC/Program.cs Normal file
View File

@ -0,0 +1,7 @@
using System.Diagnostics;
Console.WriteLine("TestC启动PID={0}", Process.GetCurrentProcess().Id);
Console.WriteLine("测试参数:{0}", String.Join(',', args));
Console.WriteLine("TestC OK!");
Console.ReadKey();

View File

@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<Company>新生命开发团队</Company>
<Copyright>©2002-2024 NewLife</Copyright>
<VersionPrefix>1.0</VersionPrefix>
<VersionSuffix>$([System.DateTime]::Now.ToString(`yyyy.MMdd`))</VersionSuffix>
<Version>$(VersionPrefix).$(VersionSuffix)</Version>
<FileVersion>$(Version)</FileVersion>
<AssemblyVersion>$(VersionPrefix).*</AssemblyVersion>
<Deterministic>false</Deterministic>
<OutputPath>..\..\Bin\Samples\TestC</OutputPath>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@ -42,6 +42,14 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DeployAgent", "DeployAgent\
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MySqlAgent", "Plugins\MySqlAgent\MySqlAgent.csproj", "{3E3CB5E2-529C-4BCE-A4B5-B1108359B2D7}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Samples", "Samples", "{E636ED18-3DBE-41AA-904A-1294EF9DA8DD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestA", "Samples\TestA\TestA.csproj", "{7908FC48-EE80-41B4-BBB9-BAA8E9AF3C67}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestB", "Samples\TestB\TestB.csproj", "{4CF398BC-76FC-49DB-AD2A-8947062D6C34}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestC", "Samples\TestC\TestC.csproj", "{1827525F-2624-4DC5-8947-B8A5A069C08F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -100,6 +108,18 @@ Global
{3E3CB5E2-529C-4BCE-A4B5-B1108359B2D7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3E3CB5E2-529C-4BCE-A4B5-B1108359B2D7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3E3CB5E2-529C-4BCE-A4B5-B1108359B2D7}.Release|Any CPU.Build.0 = Release|Any CPU
{7908FC48-EE80-41B4-BBB9-BAA8E9AF3C67}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7908FC48-EE80-41B4-BBB9-BAA8E9AF3C67}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7908FC48-EE80-41B4-BBB9-BAA8E9AF3C67}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7908FC48-EE80-41B4-BBB9-BAA8E9AF3C67}.Release|Any CPU.Build.0 = Release|Any CPU
{4CF398BC-76FC-49DB-AD2A-8947062D6C34}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4CF398BC-76FC-49DB-AD2A-8947062D6C34}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4CF398BC-76FC-49DB-AD2A-8947062D6C34}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4CF398BC-76FC-49DB-AD2A-8947062D6C34}.Release|Any CPU.Build.0 = Release|Any CPU
{1827525F-2624-4DC5-8947-B8A5A069C08F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1827525F-2624-4DC5-8947-B8A5A069C08F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1827525F-2624-4DC5-8947-B8A5A069C08F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1827525F-2624-4DC5-8947-B8A5A069C08F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -109,6 +129,9 @@ Global
{677C6502-2C9C-4907-AD82-670BE77B0AE6} = {48B9E6E7-289F-42F2-8ACA-E7DF8E7C9059}
{7178875E-F5DD-4136-A5E6-EEBF151396CB} = {11AD7F18-1320-41C5-B4A9-F208DBCAA5A9}
{3E3CB5E2-529C-4BCE-A4B5-B1108359B2D7} = {11AD7F18-1320-41C5-B4A9-F208DBCAA5A9}
{7908FC48-EE80-41B4-BBB9-BAA8E9AF3C67} = {E636ED18-3DBE-41AA-904A-1294EF9DA8DD}
{4CF398BC-76FC-49DB-AD2A-8947062D6C34} = {E636ED18-3DBE-41AA-904A-1294EF9DA8DD}
{1827525F-2624-4DC5-8947-B8A5A069C08F} = {E636ED18-3DBE-41AA-904A-1294EF9DA8DD}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {9337283C-C795-479F-A2F1-C892EBE2490C}