Refactor: Streamline code formatting, introduce cleaner structure, and remove unused Class1. Add .editorconfig for consistent coding style.

This commit is contained in:
2026-02-02 09:45:44 +01:00
parent 7182061a5f
commit c09fe5fd36
16 changed files with 623 additions and 173 deletions

View File

@@ -20,4 +20,4 @@ public partial class App : Application
base.OnFrameworkInitializationCompleted();
}
}
}

View File

@@ -118,10 +118,26 @@ public sealed class ChildSession : NotifyBase, IAsyncDisposable
}
catch { }
try { Pipe.Dispose(); } catch { }
try { Process.Dispose(); } catch { }
try { LifetimeCts.Dispose(); } catch { }
try { _writeLock.Dispose(); } catch { }
try
{
Pipe.Dispose();
}
catch { }
try
{
Process.Dispose();
}
catch { }
try
{
LifetimeCts.Dispose();
}
catch { }
try
{
_writeLock.Dispose();
}
catch { }
await Task.CompletedTask;
}

View File

@@ -45,7 +45,8 @@ public sealed class MainViewModel : NotifyBase, IAsyncDisposable
string corr = Guid.NewGuid().ToString("N");
await SelectedChild.SendAsync(
new IpcFrame(IpcKinds.Ping, CorrelationId: corr, Payload: IpcProtocol.ToJsonElement(new { from = "parent" })),
SelectedChild.LifetimeCts.Token);
SelectedChild.LifetimeCts.Token
);
SelectedChild.AddLog($"[parent] -> ping ({corr})");
}
@@ -62,11 +63,9 @@ public sealed class MainViewModel : NotifyBase, IAsyncDisposable
SelectedChild.ProgressPercent = 0;
await SelectedChild.SendAsync(
new IpcFrame(
IpcKinds.StartWork,
CorrelationId: corr,
Payload: IpcProtocol.ToJsonElement(new { steps, delayMs })),
SelectedChild.LifetimeCts.Token);
new IpcFrame(IpcKinds.StartWork, CorrelationId: corr, Payload: IpcProtocol.ToJsonElement(new { steps, delayMs })),
SelectedChild.LifetimeCts.Token
);
SelectedChild.AddLog($"[parent] -> startWork (corr={corr}, steps={steps}, delayMs={delayMs})");
}
@@ -79,9 +78,7 @@ public sealed class MainViewModel : NotifyBase, IAsyncDisposable
}
string corr = SelectedChild.CurrentWorkId ?? Guid.NewGuid().ToString("N");
await SelectedChild.SendAsync(
new IpcFrame(IpcKinds.CancelWork, CorrelationId: corr),
SelectedChild.LifetimeCts.Token);
await SelectedChild.SendAsync(new IpcFrame(IpcKinds.CancelWork, CorrelationId: corr), SelectedChild.LifetimeCts.Token);
SelectedChild.AddLog($"[parent] -> cancelWork (corr={corr})");
}
@@ -94,7 +91,8 @@ public sealed class MainViewModel : NotifyBase, IAsyncDisposable
direction: PipeDirection.InOut,
maxNumberOfServerInstances: 1,
transmissionMode: PipeTransmissionMode.Byte,
options: PipeOptions.Asynchronous);
options: PipeOptions.Asynchronous
);
Task waitForConnection = server.WaitForConnectionAsync(cancellationToken);
@@ -124,11 +122,10 @@ public sealed class MainViewModel : NotifyBase, IAsyncDisposable
string debugDll = Path.Combine(solutionRoot, "ChildWorker", "bin", "Debug", "net10.0", "ChildWorker.dll");
string releaseDll = Path.Combine(solutionRoot, "ChildWorker", "bin", "Release", "net10.0", "ChildWorker.dll");
string childDll = File.Exists(debugDll)
? debugDll
: File.Exists(releaseDll)
? releaseDll
: string.Empty;
string childDll =
File.Exists(debugDll) ? debugDll
: File.Exists(releaseDll) ? releaseDll
: string.Empty;
string fileName;
string arguments;
@@ -150,7 +147,7 @@ public sealed class MainViewModel : NotifyBase, IAsyncDisposable
FileName = fileName,
Arguments = arguments,
UseShellExecute = false,
CreateNoWindow = true
CreateNoWindow = true,
};
var p = new Process { StartInfo = psi, EnableRaisingEvents = true };
@@ -216,46 +213,46 @@ public sealed class MainViewModel : NotifyBase, IAsyncDisposable
switch (frame.Kind)
{
case IpcKinds.Hello:
{
var hello = IpcProtocol.FromJsonElement<HelloPayload>(frame.Payload);
session.AddLog($"[child] hello: id={hello?.ChildId}, pid={hello?.Pid}");
break;
}
{
var hello = IpcProtocol.FromJsonElement<HelloPayload>(frame.Payload);
session.AddLog($"[child] hello: id={hello?.ChildId}, pid={hello?.Pid}");
break;
}
case IpcKinds.Pong:
session.AddLog($"[child] pong ({frame.CorrelationId})");
break;
case IpcKinds.Log:
{
var log = IpcProtocol.FromJsonElement<LogPayload>(frame.Payload);
session.AddLog($"[child:{log?.Level ?? "info"}] {log?.Message}");
break;
}
{
var log = IpcProtocol.FromJsonElement<LogPayload>(frame.Payload);
session.AddLog($"[child:{log?.Level ?? "info"}] {log?.Message}");
break;
}
case IpcKinds.Progress:
{
var prog = IpcProtocol.FromJsonElement<ProgressPayload>(frame.Payload);
if (prog is not null)
{
var prog = IpcProtocol.FromJsonElement<ProgressPayload>(frame.Payload);
if (prog is not null)
{
session.ProgressPercent = prog.Percent;
session.Status = $"Working ({prog.Step}/{prog.Total})";
}
break;
session.ProgressPercent = prog.Percent;
session.Status = $"Working ({prog.Step}/{prog.Total})";
}
break;
}
case IpcKinds.Result:
{
var res = IpcProtocol.FromJsonElement<ResultPayload>(frame.Payload);
session.ProgressPercent = 100;
session.Status = "Idle";
session.AddLog($"[child] result: {res?.Message}");
break;
}
{
var res = IpcProtocol.FromJsonElement<ResultPayload>(frame.Payload);
session.ProgressPercent = 100;
session.Status = "Idle";
session.AddLog($"[child] result: {res?.Message}");
break;
}
case IpcKinds.Error:
{
var err = IpcProtocol.FromJsonElement<ErrorPayload>(frame.Payload);
session.Status = "Error";
session.AddLog($"[child] error: {err?.Message}");
break;
}
{
var err = IpcProtocol.FromJsonElement<ErrorPayload>(frame.Payload);
session.Status = "Error";
session.AddLog($"[child] error: {err?.Message}");
break;
}
default:
session.AddLog($"[child] {frame.Kind} ({frame.CorrelationId})");
break;
@@ -268,15 +265,23 @@ public sealed class MainViewModel : NotifyBase, IAsyncDisposable
foreach (var child in Children.ToArray())
{
try { await child.DisposeAsync(); } catch { }
try
{
await child.DisposeAsync();
}
catch { }
}
_shutdownCts.Dispose();
}
private sealed record HelloPayload(int ChildId, int Pid);
private sealed record LogPayload(string Level, string Message);
private sealed record ProgressPayload(int Step, int Total, double Percent);
private sealed record ResultPayload(string Message);
private sealed record ErrorPayload(string Message);
}

View File

@@ -15,32 +15,60 @@ public partial class MainWindow : Window
Closed += async (_, __) =>
{
try { await _vm.DisposeAsync(); } catch { }
try
{
await _vm.DisposeAsync();
}
catch { }
};
}
private async void StartChildren_Click(object? sender, RoutedEventArgs e)
{
try { await _vm.StartChildrenAsync(count: 3); }
catch (Exception ex) { await MessageBoxAsync(ex.Message); }
try
{
await _vm.StartChildrenAsync(count: 3);
}
catch (Exception ex)
{
await MessageBoxAsync(ex.Message);
}
}
private async void PingSelected_Click(object? sender, RoutedEventArgs e)
{
try { await _vm.PingSelectedAsync(); }
catch (Exception ex) { await MessageBoxAsync(ex.Message); }
try
{
await _vm.PingSelectedAsync();
}
catch (Exception ex)
{
await MessageBoxAsync(ex.Message);
}
}
private async void StartWork_Click(object? sender, RoutedEventArgs e)
{
try { await _vm.StartWorkSelectedAsync(); }
catch (Exception ex) { await MessageBoxAsync(ex.Message); }
try
{
await _vm.StartWorkSelectedAsync();
}
catch (Exception ex)
{
await MessageBoxAsync(ex.Message);
}
}
private async void CancelWork_Click(object? sender, RoutedEventArgs e)
{
try { await _vm.CancelWorkSelectedAsync(); }
catch (Exception ex) { await MessageBoxAsync(ex.Message); }
try
{
await _vm.CancelWorkSelectedAsync();
}
catch (Exception ex)
{
await MessageBoxAsync(ex.Message);
}
}
private async Task MessageBoxAsync(string message)
@@ -61,10 +89,10 @@ public partial class MainWindow : Window
{
HorizontalAlignment = Avalonia.Layout.HorizontalAlignment.Right,
Content = "OK",
IsDefault = true
}
}
}
IsDefault = true,
},
},
},
};
if (dlg.Content is StackPanel sp && sp.Children.LastOrDefault() is Button ok)
@@ -74,4 +102,4 @@ public partial class MainWindow : Window
await dlg.ShowDialog(this);
}
}
}

View File

@@ -7,8 +7,8 @@ public abstract class NotifyBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler? PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string? propertyName = null)
=> PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
protected void OnPropertyChanged([CallerMemberName] string? propertyName = null) =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
protected bool SetField<T>(ref T field, T value, [CallerMemberName] string? propertyName = null)
{

View File

@@ -19,6 +19,10 @@
<IncludeAssets Condition="'$(Configuration)' != 'Debug'">None</IncludeAssets>
<PrivateAssets Condition="'$(Configuration)' != 'Debug'">All</PrivateAssets>
</PackageReference>
<PackageReference Include="CSharpier.MsBuild" Version="1.2.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>

View File

@@ -1,5 +1,5 @@
using Avalonia;
using System;
using System;
using Avalonia;
namespace ParentAvalonia;
@@ -9,13 +9,8 @@ class Program
// SynchronizationContext-reliant code before AppMain is called: things aren't initialized
// yet and stuff might break.
[STAThread]
public static void Main(string[] args) => BuildAvaloniaApp()
.StartWithClassicDesktopLifetime(args);
public static void Main(string[] args) => BuildAvaloniaApp().StartWithClassicDesktopLifetime(args);
// Avalonia configuration, don't remove; also used by visual designer.
public static AppBuilder BuildAvaloniaApp()
=> AppBuilder.Configure<App>()
.UsePlatformDetect()
.WithInterFont()
.LogToTrace();
public static AppBuilder BuildAvaloniaApp() => AppBuilder.Configure<App>().UsePlatformDetect().WithInterFont().LogToTrace();
}