Skip to content

Commit 8cb5876

Browse files
committed
fix: The issue of slowness when using Environment.SetEnvironmentVariable.
1 parent aaa516b commit 8cb5876

File tree

13 files changed

+48
-57
lines changed

13 files changed

+48
-57
lines changed

src/c#/GeneralUpdate.Bowl/Bowl.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Runtime.InteropServices;
44
using System.Text.Json;
55
using GeneralUpdate.Bowl.Strategys;
6+
using GeneralUpdate.Common.Internal.Bootstrap;
67
using GeneralUpdate.Common.Internal.JsonContext;
78
using GeneralUpdate.Common.Shared.Object;
89

@@ -36,7 +37,7 @@ public static void Launch(MonitorParameter? monitorParameter = null)
3637

3738
private static MonitorParameter CreateParameter()
3839
{
39-
var json = Environment.GetEnvironmentVariable("ProcessInfo", EnvironmentVariableTarget.User);
40+
var json = Environments.GetEnvironmentVariable("ProcessInfo");
4041
if(string.IsNullOrWhiteSpace(json))
4142
throw new ArgumentNullException("ProcessInfo environment variable not set !");
4243

src/c#/GeneralUpdate.Bowl/GeneralUpdate.Bowl.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
<Compile Include="..\GeneralUpdate.Common\Internal\Bootstrap\AbstractBootstrap.cs" Link="Common\AbstractBootstrap.cs" />
5454
<Compile Include="..\GeneralUpdate.Common\Internal\Bootstrap\UpdateOption.cs" Link="Common\UpdateOption.cs" />
5555
<Compile Include="..\GeneralUpdate.Common\Internal\Bootstrap\UpdateOptionValue.cs" Link="Common\UpdateOptionValue.cs" />
56+
<Compile Include="..\GeneralUpdate.Common\Internal\Bootstrap\Environments.cs" Link="Common\Environments.cs" />
5657
<Compile Include="..\GeneralUpdate.Common\Internal\Event\EventManager.cs" Link="Common\EventManager.cs" />
5758
<Compile Include="..\GeneralUpdate.Common\Internal\Event\IEventManager.cs" Link="Common\IEventManager.cs" />
5859
<Compile Include="..\GeneralUpdate.Common\Internal\Exception\ExceptionEventArgs.cs" Link="Common\ExceptionEventArgs.cs" />

src/c#/GeneralUpdate.Bowl/Strategys/WindowStrategy.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Runtime.InteropServices;
66
using GeneralUpdate.Bowl.Internal;
77
using GeneralUpdate.Common.FileBasic;
8+
using GeneralUpdate.Common.Internal.Bootstrap;
89

910
namespace GeneralUpdate.Bowl.Strategys;
1011

@@ -101,6 +102,6 @@ private void SetEnvironment()
101102
* If the latest version number obtained via an HTTP request is less than or equal to the exception version number, the update is skipped.
102103
* Once this version number is set, it will not be removed, and updates will not proceed until a version greater than the exception version number is obtained through the HTTP request.
103104
*/
104-
Environment.SetEnvironmentVariable("UpgradeFail", _parameter.ExtendedField, EnvironmentVariableTarget.User);
105+
Environments.SetEnvironmentVariable("UpgradeFail", _parameter.ExtendedField);
105106
}
106107
}

src/c#/GeneralUpdate.Client/GeneralUpdate.Client.csproj

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,9 @@
55
<TargetFramework>net8.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
8-
<PublishAot>true</PublishAot>
98
<LangVersion>default</LangVersion>
109
</PropertyGroup>
1110

12-
<ItemGroup>
13-
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="9.0.0" />
14-
</ItemGroup>
15-
1611
<ItemGroup>
1712
<ProjectReference Include="..\GeneralUpdate.ClientCore\GeneralUpdate.ClientCore.csproj" />
1813
</ItemGroup>

src/c#/GeneralUpdate.ClientCore/GeneralClientBootstrap.cs

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ public override async Task<GeneralClientBootstrap> LaunchAsync()
4646
{
4747
CallSmallBowlHome(_configInfo.Bowl);
4848
ExecuteCustomOptions();
49-
ClearEnvironmentVariable();
5049
await ExecuteWorkflowAsync();
5150
}
5251
catch (Exception exception)
@@ -294,7 +293,7 @@ private bool CheckFail(string version)
294293
Read the version number of the last failed upgrade from the system environment variables, then compare it with the version number of the current request.
295294
If it is less than or equal to the failed version number, do not perform the update.
296295
*/
297-
var fail = Environment.GetEnvironmentVariable("UpgradeFail", EnvironmentVariableTarget.User);
296+
var fail = Environments.GetEnvironmentVariable("UpgradeFail");
298297
if (string.IsNullOrEmpty(fail) || string.IsNullOrEmpty(version))
299298
return false;
300299

@@ -404,33 +403,6 @@ private void ExecuteCustomOptions()
404403
}
405404
}
406405

407-
/// <summary>
408-
/// Clear the environment variable information needed to start the upgrade assistant process.
409-
/// </summary>
410-
private void ClearEnvironmentVariable()
411-
{
412-
try
413-
{
414-
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
415-
{
416-
Environment.SetEnvironmentVariable("ProcessInfo", null, EnvironmentVariableTarget.User);
417-
}
418-
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
419-
{
420-
if (File.Exists("ProcessInfo.json"))
421-
{
422-
File.SetAttributes("ProcessInfo.json", FileAttributes.Normal);
423-
File.Delete("ProcessInfo.json");
424-
}
425-
}
426-
}
427-
catch (Exception ex)
428-
{
429-
Debug.WriteLine(ex);
430-
EventManager.Instance.Dispatch(this, new ExceptionEventArgs(ex, "Error: An unknown error occurred while deleting the environment variable."));
431-
}
432-
}
433-
434406
protected override void ExecuteStrategy() => throw new NotImplementedException();
435407

436408
protected override Task ExecuteStrategyAsync() => throw new NotImplementedException();

src/c#/GeneralUpdate.ClientCore/GeneralClientOSS.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Threading;
99
using System.Threading.Tasks;
1010
using GeneralUpdate.Common.FileBasic;
11+
using GeneralUpdate.Common.Internal.Bootstrap;
1112
using GeneralUpdate.Common.Internal.JsonContext;
1213
using GeneralUpdate.Common.Shared.Object;
1314

@@ -45,7 +46,7 @@ await Task.Run(() =>
4546
throw new Exception($"The application does not exist {upgradeAppName} !");
4647

4748
var json = JsonSerializer.Serialize(configGlobalConfigInfo, GlobalConfigInfoOSSJsonContext.Default.GlobalConfigInfoOSS);
48-
Environment.SetEnvironmentVariable("GlobalConfigInfoOSS", json, EnvironmentVariableTarget.User);
49+
Environments.SetEnvironmentVariable("GlobalConfigInfoOSS", json);
4950
Process.Start(appPath);
5051
}
5152
catch (Exception ex)

src/c#/GeneralUpdate.ClientCore/GeneralUpdate.ClientCore.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
<Compile Include="..\GeneralUpdate.Common\Internal\Bootstrap\AbstractBootstrap.cs" Link="Common\AbstractBootstrap.cs" />
4444
<Compile Include="..\GeneralUpdate.Common\Internal\Bootstrap\UpdateOption.cs" Link="Common\UpdateOption.cs" />
4545
<Compile Include="..\GeneralUpdate.Common\Internal\Bootstrap\UpdateOptionValue.cs" Link="Common\UpdateOptionValue.cs" />
46+
<Compile Include="..\GeneralUpdate.Common\Internal\Bootstrap\Environments.cs" Link="Common\Environments.cs" />
4647
<Compile Include="..\GeneralUpdate.Common\Internal\Event\EventManager.cs" Link="Common\EventManager.cs" />
4748
<Compile Include="..\GeneralUpdate.Common\Internal\Event\IEventManager.cs" Link="Common\IEventManager.cs" />
4849
<Compile Include="..\GeneralUpdate.Common\Internal\Exception\ExceptionEventArgs.cs" Link="Common\ExceptionEventArgs.cs" />

src/c#/GeneralUpdate.ClientCore/Strategys/WindowsStrategy.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using GeneralUpdate.ClientCore.Pipeline;
66
using GeneralUpdate.Common.FileBasic;
77
using GeneralUpdate.Common.Internal;
8+
using GeneralUpdate.Common.Internal.Bootstrap;
89
using GeneralUpdate.Common.Internal.Event;
910
using GeneralUpdate.Common.Internal.Pipeline;
1011
using GeneralUpdate.Common.Internal.Strategy;
@@ -89,11 +90,11 @@ public override void StartApp()
8990
{
9091
try
9192
{
92-
Environment.SetEnvironmentVariable("ProcessInfo", _configinfo.ProcessInfo, EnvironmentVariableTarget.User);
93+
Environments.SetEnvironmentVariable("ProcessInfo", _configinfo.ProcessInfo);
9394
var appPath = Path.Combine(_configinfo.InstallPath, _configinfo.AppName);
9495
if (File.Exists(appPath))
9596
{
96-
Environment.SetEnvironmentVariable("ProcessInfo", _configinfo.ProcessInfo, EnvironmentVariableTarget.User);
97+
Environments.SetEnvironmentVariable("ProcessInfo", _configinfo.ProcessInfo);
9798
Process.Start(new ProcessStartInfo
9899
{
99100
UseShellExecute = true,

src/c#/GeneralUpdate.Common/FileBasic/BlackListManager.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,14 @@ public class BlackListManager
2020
".pdb"
2121
];
2222

23-
private static readonly List<string> _blackFiles = ["Newtonsoft.Json.dll"];
23+
private static readonly List<string> _blackFiles =
24+
[
25+
"Microsoft.Bcl.AsyncInterfaces.dll",
26+
"System.Collections.Immutable.dll",
27+
"System.IO.Pipelines.dll",
28+
"System.Text.Encodings.Web.dll",
29+
"System.Text.Json.dll"
30+
];
2431

2532
private static readonly List<string> _skipDirectorys = ["app-", "fail"];
2633

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System.IO;
2+
3+
namespace GeneralUpdate.Common.Internal.Bootstrap;
4+
5+
public static class Environments
6+
{
7+
public static void SetEnvironmentVariable(string key, string value)
8+
{
9+
var filePath = Path.Combine(Path.GetTempPath(), $"{key}.txt");
10+
File.WriteAllText(filePath, value);
11+
}
12+
13+
public static string GetEnvironmentVariable(string key)
14+
{
15+
var filePath = Path.Combine(Path.GetTempPath(), $"{key}.txt");
16+
if (!File.Exists(filePath))
17+
return string.Empty;
18+
19+
var content = File.ReadAllText(filePath);
20+
File.SetAttributes(filePath, FileAttributes.Normal);
21+
File.Delete(filePath);
22+
return content;
23+
}
24+
}

src/c#/GeneralUpdate.Core/GeneralUpdate.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
<Compile Include="..\GeneralUpdate.Common\Internal\Bootstrap\AbstractBootstrap.cs" Link="Common\AbstractBootstrap.cs" />
4747
<Compile Include="..\GeneralUpdate.Common\Internal\Bootstrap\UpdateOption.cs" Link="Common\UpdateOption.cs" />
4848
<Compile Include="..\GeneralUpdate.Common\Internal\Bootstrap\UpdateOptionValue.cs" Link="Common\UpdateOptionValue.cs" />
49+
<Compile Include="..\GeneralUpdate.Common\Internal\Bootstrap\Environments.cs" Link="Common\Environments.cs" />
4950
<Compile Include="..\GeneralUpdate.Common\Internal\Event\EventManager.cs" Link="Common\EventManager.cs" />
5051
<Compile Include="..\GeneralUpdate.Common\Internal\Event\IEventManager.cs" Link="Common\IEventManager.cs" />
5152
<Compile Include="..\GeneralUpdate.Common\Internal\Exception\ExceptionEventArgs.cs" Link="Common\ExceptionEventArgs.cs" />

src/c#/GeneralUpdate.Core/GeneralUpdateBootstrap.cs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class GeneralUpdateBootstrap : AbstractBootstrap<GeneralUpdateBootstrap,
2525

2626
public GeneralUpdateBootstrap()
2727
{
28-
var json = GetProcessInfoJsonContext();
28+
var json = Environments.GetEnvironmentVariable("ProcessInfo");
2929
if (string.IsNullOrWhiteSpace(json))
3030
throw new ArgumentException("json environment variable is not defined");
3131

@@ -112,21 +112,6 @@ public GeneralUpdateBootstrap AddListenerException(Action<object, ExceptionEvent
112112

113113
#endregion
114114

115-
private static string? GetProcessInfoJsonContext()
116-
{
117-
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
118-
return Environment.GetEnvironmentVariable("ProcessInfo", EnvironmentVariableTarget.User);
119-
120-
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
121-
{
122-
var jsonFileName = "ProcessInfo.json";
123-
if (File.Exists(jsonFileName))
124-
return File.ReadAllText(jsonFileName);
125-
}
126-
127-
return null;
128-
}
129-
130115
protected override Task ExecuteStrategyAsync()=> throw new NotImplementedException();
131116

132117
protected override void ExecuteStrategy()

src/c#/GeneralUpdate.Core/GeneralUpdateOSS.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Diagnostics;
33
using System.Text.Json;
44
using System.Threading.Tasks;
5+
using GeneralUpdate.Common.Internal.Bootstrap;
56
using GeneralUpdate.Common.Internal.JsonContext;
67
using GeneralUpdate.Common.Shared.Object;
78
using GeneralUpdate.Core.Strategys;
@@ -31,7 +32,7 @@ private static async Task BaseStart()
3132
{
3233
try
3334
{
34-
var json = Environment.GetEnvironmentVariable("GlobalConfigInfoOSS", EnvironmentVariableTarget.User);
35+
var json = Environments.GetEnvironmentVariable("GlobalConfigInfoOSS");
3536
if (string.IsNullOrWhiteSpace(json))
3637
return;
3738

0 commit comments

Comments
 (0)