控制台刷新优化

This commit is contained in:
zhaotielin 2026-07-13 03:51:13 +08:00
parent 96bdd42528
commit 07b536895a
3 changed files with 26 additions and 7 deletions

View File

@ -180,7 +180,10 @@ public partial class App : Application
services.AddTransient<HomeViewModel>(); services.AddTransient<HomeViewModel>();
services.AddTransient<ProjectInfoViewModel>(); services.AddTransient<ProjectInfoViewModel>();
services.AddTransient<TestHostViewModel>(); services.AddTransient<TestHostViewModel>();
services.AddTransient<ExperimentConsoleViewModel>(); // The experiment console controls application-wide hardware and is not
// owned by a project. Keep one instance so switching project pages does
// not replace its state or interrupt its refresh loop.
services.AddSingleton<ExperimentConsoleViewModel>();
services.AddTransient<BasicTestViewModel>(); services.AddTransient<BasicTestViewModel>();
services.AddTransient<CurveTestViewModel>(); services.AddTransient<CurveTestViewModel>();
services.AddTransient<PowerTestViewModel>(); services.AddTransient<PowerTestViewModel>();

View File

@ -17,6 +17,7 @@ public partial class ExperimentConsoleViewModel : ObservableObject
private readonly UserSettingsStore _settingsStore; private readonly UserSettingsStore _settingsStore;
private readonly UiLogSink _log; private readonly UiLogSink _log;
private readonly object _pollLock = new(); private readonly object _pollLock = new();
private readonly HashSet<object> _activeHosts = new(ReferenceEqualityComparer.Instance);
private CancellationTokenSource? _pollCts; private CancellationTokenSource? _pollCts;
private Task? _pollTask; private Task? _pollTask;
@ -113,13 +114,25 @@ public partial class ExperimentConsoleViewModel : ObservableObject
LoadCurrentSetpoint = value.Current; LoadCurrentSetpoint = value.Current;
} }
public void SetActive(bool active) public void SetActive(object host, bool active)
{
ArgumentNullException.ThrowIfNull(host);
lock (_pollLock)
{ {
if (active) if (active)
{
_activeHosts.Add(host);
StartPolling(); StartPolling();
}
else else
{
_activeHosts.Remove(host);
if (_activeHosts.Count == 0)
StopPolling(); StopPolling();
} }
}
}
[RelayCommand] [RelayCommand]
private async Task ApplyPower() private async Task ApplyPower()

View File

@ -49,6 +49,9 @@ public partial class TestHostViewModel : ObservableObject
private void UpdateConsoleActive() private void UpdateConsoleActive()
{ {
Console.SetActive(_isViewActive && SelectedTabIndex == 0); // Identify this host when acquiring/releasing console polling. During a
// project switch WPF can load the new host before unloading the old one;
// the old host must not stop the shared console on behalf of the new host.
Console.SetActive(this, _isViewActive && SelectedTabIndex == 0);
} }
} }