Skip to content

Commit 3a72a73

Browse files
committed
fix: Fix app crash when writing file who is being used by another by retry. fix #382
1 parent adf63b9 commit 3a72a73

12 files changed

+63
-12
lines changed

Ui/LOGO_D.ico

14.7 KB
Binary file not shown.

Ui/Service/ConfigurationService.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using _1RM.Model.DAO;
1313
using _1RM.Service.DataSource;
1414
using _1RM.Service.DataSource.Model;
15+
using _1RM.Utils;
1516
using Shawn.Utils;
1617
using Shawn.Utils.Wpf;
1718
using Shawn.Utils.Wpf.FileSystem;
@@ -253,7 +254,11 @@ public void Save()
253254
var fi = new FileInfo(AppPathHelper.Instance.ProfileJsonPath);
254255
if (fi?.Directory?.Exists == false)
255256
fi.Directory.Create();
256-
File.WriteAllText(AppPathHelper.Instance.ProfileJsonPath, JsonConvert.SerializeObject(this._cfg, Formatting.Indented), Encoding.UTF8);
257+
258+
RetryHelper.Try(() =>
259+
{
260+
File.WriteAllText(AppPathHelper.Instance.ProfileJsonPath, JsonConvert.SerializeObject(this._cfg, Formatting.Indented), Encoding.UTF8);
261+
}, actionOnError: exception => MsAppCenterHelper.Error(exception));
257262
}
258263

259264

Ui/Service/ConnectionTimeRecorder.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
using System.Threading.Tasks;
88
using _1RM.Model.DAO.Dapper;
99
using _1RM.Model.Protocol.Base;
10+
using _1RM.Utils;
1011
using _1RM.View;
1112
using Newtonsoft.Json;
13+
using Shawn.Utils;
1214

1315
namespace _1RM.Service
1416
{
@@ -33,7 +35,10 @@ private static void Load()
3335

3436
public static void Save()
3537
{
36-
File.WriteAllText(Path, JsonConvert.SerializeObject(ConnectTimeData));
38+
RetryHelper.Try(() =>
39+
{
40+
File.WriteAllText(Path, JsonConvert.SerializeObject(ConnectTimeData));
41+
}, actionOnError: exception => MsAppCenterHelper.Error(exception));
3742
}
3843

3944
/// <summary>

Ui/Service/DataSource/DataSourceService.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using _1RM.Model.DAO;
1111
using _1RM.Model.Protocol.Base;
1212
using _1RM.Service.DataSource.Model;
13+
using _1RM.Utils;
1314
using _1RM.View;
1415
using Microsoft.WindowsAPICodePack.Shell.PropertySystem;
1516
using Newtonsoft.Json;
@@ -163,7 +164,10 @@ public static void AdditionalSourcesSaveToProfile(string path, List<DataSourceBa
163164
if (fi?.Directory?.Exists == false)
164165
fi.Directory.Create();
165166
if (IoPermissionHelper.HasWritePermissionOnFile(path))
166-
File.WriteAllText(path, JsonConvert.SerializeObject(sources, Formatting.Indented), Encoding.UTF8);
167+
RetryHelper.Try(() =>
168+
{
169+
File.WriteAllText(path, JsonConvert.SerializeObject(sources, Formatting.Indented), Encoding.UTF8);
170+
}, actionOnError: exception => MsAppCenterHelper.Error(exception));
167171
}
168172
}
169173
finally

Ui/Service/LocalityService.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using _1RM.View.Launcher;
1313
using Shawn.Utils;
1414
using _1RM.View;
15+
using _1RM.Utils;
1516

1617
namespace _1RM.Service
1718
{
@@ -255,7 +256,11 @@ private void Save()
255256
var fi = new FileInfo(AppPathHelper.Instance.LocalityJsonPath);
256257
if (fi?.Directory?.Exists == false)
257258
fi.Directory.Create();
258-
File.WriteAllText(AppPathHelper.Instance.LocalityJsonPath, JsonConvert.SerializeObject(this._localitySettings, Formatting.Indented), Encoding.UTF8);
259+
260+
RetryHelper.Try(() =>
261+
{
262+
File.WriteAllText(AppPathHelper.Instance.LocalityJsonPath, JsonConvert.SerializeObject(this._localitySettings, Formatting.Indented), Encoding.UTF8);
263+
}, actionOnError: exception => MsAppCenterHelper.Error(exception));
259264
CanSave = true;
260265
}
261266
}

Ui/Service/ProtocolConfigurationService.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using _1RM.Model.ProtocolRunner;
1414
using _1RM.Model.ProtocolRunner.Default;
1515
using Shawn.Utils;
16+
using _1RM.Utils;
1617

1718
namespace _1RM.Service
1819
{
@@ -194,7 +195,10 @@ public void Save()
194195
}
195196
}
196197
var file = Path.Combine(AppPathHelper.Instance.ProtocolRunnerDirPath, $"{protocolName}.json");
197-
File.WriteAllText(file, JsonConvert.SerializeObject(config, Formatting.Indented), Encoding.UTF8);
198+
RetryHelper.Try(() =>
199+
{
200+
File.WriteAllText(file, JsonConvert.SerializeObject(config, Formatting.Indented), Encoding.UTF8);
201+
}, actionOnError: exception => MsAppCenterHelper.Error(exception));
198202
}
199203
}
200204
}

Ui/Service/SessionControlService_OpenConnection.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Diagnostics;
33
using System.IO;
44
using System.Linq;
5+
using System.Text;
56
using System.Threading;
67
using System.Threading.Tasks;
78
using System.Windows;
@@ -12,6 +13,7 @@
1213
using _1RM.Model.ProtocolRunner.Default;
1314
using _1RM.Utils;
1415
using _1RM.View.Host;
16+
using Newtonsoft.Json;
1517
using Shawn.Utils;
1618
using Shawn.Utils.Wpf;
1719
using Stylet;
@@ -29,10 +31,14 @@ private void ConnectRdpByMstsc(in RDP rdp)
2931
new string(Path.GetInvalidPathChars());
3032
rdpFileName = invalid.Aggregate(rdpFileName, (current, c) => current.Replace(c.ToString(), ""));
3133
var rdpFile = Path.Combine(tmp, rdpFileName + ".rdp");
34+
var text = rdp.ToRdpConfig().ToString();
3235

3336
// write a .rdp file for mstsc.exe
37+
if (RetryHelper.Try(() =>
38+
{
39+
File.WriteAllText(rdpFile, text);
40+
}, actionOnError: exception => MsAppCenterHelper.Error(exception)))
3441
{
35-
File.WriteAllText(rdpFile, rdp.ToRdpConfig().ToString());
3642
var p = new Process
3743
{
3844
StartInfo =
@@ -76,9 +82,14 @@ private void ConnectRemoteApp(in RdpApp remoteApp)
7682
rdpFileName = invalid.Aggregate(rdpFileName, (current, c) => current.Replace(c.ToString(), ""));
7783
var rdpFile = Path.Combine(tmp, rdpFileName + ".rdp");
7884

85+
// write a .rdp file for mstsc.exet.Replace(c.ToString(), ""));
86+
var text = remoteApp.ToRdpConfig().ToString();
7987
// write a .rdp file for mstsc.exe
88+
if (RetryHelper.Try(() =>
89+
{
90+
File.WriteAllText(rdpFile, text);
91+
}, actionOnError: exception => MsAppCenterHelper.Error(exception)))
8092
{
81-
File.WriteAllText(rdpFile, remoteApp.ToRdpConfig().ToString());
8293
var p = new Process
8394
{
8495
StartInfo =

Ui/Service/TaskTrayService.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ private void ProtocolBaseOnPropertyChanged(object? sender, PropertyChangedEventA
4242
public void TaskTrayInit()
4343
{
4444
Debug.Assert(Application.GetResourceStream(ResourceUriHelper.GetUriFromCurrentAssembly("LOGO.ico"))?.Stream != null);
45+
#if DEBUG
46+
var icon = new System.Drawing.Icon(Application.GetResourceStream(ResourceUriHelper.GetUriFromCurrentAssembly("LOGO_D.ico")).Stream);
47+
#else
4548
var icon = new System.Drawing.Icon(Application.GetResourceStream(ResourceUriHelper.GetUriFromCurrentAssembly("LOGO.ico")).Stream);
49+
#endif
4650
Debug.Assert(icon != null);
4751
Executor.OnUIThread(() =>
4852
{

Ui/Ui.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
<UseWindowsForms>True</UseWindowsForms>
1010
<RootNamespace>_1RM</RootNamespace>
1111
<AssemblyName>1Remote</AssemblyName>
12-
<ApplicationIcon>LOGO.ico</ApplicationIcon>
12+
<ApplicationIcon Condition="'$(Configuration)'=='Debug'">LOGO_D.ico</ApplicationIcon>
13+
<ApplicationIcon Condition="'$(Configuration)'!='Debug'">LOGO.ico</ApplicationIcon>
1314
<Configurations>Debug;Release;StoreRelease;ReleaseNet48</Configurations>
1415
<TargetFramework Condition="'$(Configuration)'=='Debug'">net6.0-windows</TargetFramework>
1516
<TargetFramework Condition="'$(Configuration)'=='Release'">net6.0-windows</TargetFramework>
@@ -70,6 +71,7 @@
7071
</PropertyGroup>
7172

7273
<ItemGroup>
74+
<None Remove="LOGO_D.ico" />
7375
<None Remove="Resources\Icons\000_OS\000_Win10_1.png" />
7476
<None Remove="Resources\Icons\000_OS\000_Win10_2.png" />
7577
<None Remove="Resources\Icons\000_OS\000_Win11_0.png" />
@@ -150,6 +152,7 @@
150152

151153
<ItemGroup>
152154
<Resource Include="LOGO.ico" />
155+
<Resource Include="LOGO_D.ico" />
153156
<Resource Include="Resources\Icons\000_OS\000_Win10_1.png" />
154157
<Resource Include="Resources\Icons\000_OS\000_Win11_0.png" />
155158
<Resource Include="Resources\Icons\000_OS\000_Win11_1.png" />

Ui/Utils/KiTTY/IKittyConnectable.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using _1RM.Service.DataSource.Model;
1515
using _1RM.Utils.KiTTY.Model;
1616
using _1RM.Model.ProtocolRunner.Default;
17+
using System.Text;
1718

1819
namespace _1RM.Utils.KiTTY
1920
{
@@ -138,8 +139,11 @@ public static void WriteKiTTYDefaultConfig(string kittyFullName)
138139
var fi = new FileInfo(kittyFullName);
139140
if (fi?.Directory?.Exists == false)
140141
fi.Directory.Create();
141-
File.WriteAllText(Path.Combine(fi!.Directory!.FullName, "kitty.ini"),
142-
@"
142+
143+
RetryHelper.Try(() =>
144+
{
145+
File.WriteAllText(Path.Combine(fi!.Directory!.FullName, "kitty.ini"),
146+
@"
143147
[Agent]
144148
[ConfigBox]
145149
dblclick=open
@@ -181,6 +185,7 @@ public static void WriteKiTTYDefaultConfig(string kittyFullName)
181185
[Launcher]
182186
reload=yes
183187
");
188+
}, actionOnError: exception => MsAppCenterHelper.Error(exception));
184189
}
185190

186191
public static string GetInternalKittyExeFullName()

0 commit comments

Comments
 (0)