Skip to content

Commit 8b515ae

Browse files
BornToBeRootclaude
andcommitted
Refactor: Wait for the view to be loaded without polling
WaitForLoadedAsync (Utilities.WPF) completes on the Loaded event (or immediately if the view is already loaded) and replaces the "while (!_loaded) await Task.Delay(250)" loops of the remote tools. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent ce661b1 commit 8b515ae

11 files changed

Lines changed: 44 additions & 60 deletions
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System.Threading.Tasks;
2+
using System.Windows;
3+
4+
namespace NETworkManager.Utilities.WPF;
5+
6+
/// <summary>
7+
/// Extension methods for <see cref="FrameworkElement" />.
8+
/// </summary>
9+
public static class FrameworkElementExtensions
10+
{
11+
/// <summary>
12+
/// Waits until the element is loaded (e.g. before showing a dialog from a view that was just created, which
13+
/// MahApps doesn't allow before the view is loaded). Completes immediately if the element is already loaded.
14+
/// </summary>
15+
/// <param name="element">Element to wait for.</param>
16+
/// <returns>Task that completes when the element is loaded.</returns>
17+
public static Task WaitForLoadedAsync(this FrameworkElement element)
18+
{
19+
if (element.IsLoaded)
20+
return Task.CompletedTask;
21+
22+
var taskCompletionSource = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
23+
24+
void OnLoaded(object sender, RoutedEventArgs e)
25+
{
26+
element.Loaded -= OnLoaded;
27+
taskCompletionSource.TrySetResult();
28+
}
29+
30+
element.Loaded += OnLoaded;
31+
32+
return taskCompletionSource.Task;
33+
}
34+
}

‎Source/NETworkManager/Views/PowerShellHostView.xaml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
xmlns:wpfHelpers="clr-namespace:NETworkManager.Utilities.WPF;assembly=NETworkManager.Utilities.WPF"
1919
xmlns:networkManager="clr-namespace:NETworkManager"
2020
xmlns:hostControls="clr-namespace:NETworkManager.Views.Controls"
21-
Loaded="UserControl_Loaded"
2221
mc:Ignorable="d" d:DataContext="{d:DesignInstance viewModels:PowerShellHostViewModel}">
2322
<UserControl.Resources>
2423
<converters:BooleanToVisibilityCollapsedConverter x:Key="BooleanToVisibilityCollapsedConverter" />

‎Source/NETworkManager/Views/PowerShellHostView.xaml.cs‎

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,23 @@
1+
using NETworkManager.Utilities.WPF;
12
using NETworkManager.ViewModels;
2-
using System.Threading.Tasks;
3-
using System.Windows;
43

54
namespace NETworkManager.Views;
65

76
public partial class PowerShellHostView : IApplicationRequestHandler
87
{
98
private readonly PowerShellHostViewModel _viewModel = new();
109

11-
private bool _loaded;
12-
1310
public PowerShellHostView()
1411
{
1512
InitializeComponent();
1613
DataContext = _viewModel;
1714
}
1815

19-
private void UserControl_Loaded(object sender, RoutedEventArgs e)
20-
{
21-
_loaded = true;
22-
}
23-
2416
public async void Open(ApplicationRequest request)
2517
{
2618
// Wait for the interface to load, before displaying the dialog to connect a new profile...
2719
// MahApps will throw an exception...
28-
while (!_loaded)
29-
await Task.Delay(250);
20+
await this.WaitForLoadedAsync();
3021

3122
if (!_viewModel.IsExecutableConfigured)
3223
return;

‎Source/NETworkManager/Views/PuTTYHostView.xaml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
xmlns:wpfHelpers="clr-namespace:NETworkManager.Utilities.WPF;assembly=NETworkManager.Utilities.WPF"
1919
xmlns:networkManager="clr-namespace:NETworkManager"
2020
xmlns:hostControls="clr-namespace:NETworkManager.Views.Controls"
21-
Loaded="UserControl_Loaded"
2221
mc:Ignorable="d" d:DataContext="{d:DesignInstance viewModels:PuTTYHostViewModel}">
2322
<UserControl.Resources>
2423
<converters:BooleanToVisibilityCollapsedConverter x:Key="BooleanToVisibilityCollapsedConverter" />

‎Source/NETworkManager/Views/PuTTYHostView.xaml.cs‎

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,23 @@
1+
using NETworkManager.Utilities.WPF;
12
using NETworkManager.ViewModels;
2-
using System.Threading.Tasks;
3-
using System.Windows;
43

54
namespace NETworkManager.Views;
65

76
public partial class PuTTYHostView : IApplicationRequestHandler
87
{
98
private readonly PuTTYHostViewModel _viewModel = new();
109

11-
private bool _loaded;
12-
1310
public PuTTYHostView()
1411
{
1512
InitializeComponent();
1613
DataContext = _viewModel;
1714
}
1815

19-
private void UserControl_Loaded(object sender, RoutedEventArgs e)
20-
{
21-
_loaded = true;
22-
}
23-
2416
public async void Open(ApplicationRequest request)
2517
{
2618
// Wait for the interface to load, before displaying the dialog to connect a new profile...
2719
// MahApps will throw an exception...
28-
while (!_loaded)
29-
await Task.Delay(250);
20+
await this.WaitForLoadedAsync();
3021

3122
if (!_viewModel.IsExecutableConfigured)
3223
return;

‎Source/NETworkManager/Views/RemoteDesktopHostView.xaml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
xmlns:wpfHelpers="clr-namespace:NETworkManager.Utilities.WPF;assembly=NETworkManager.Utilities.WPF"
1818
xmlns:networkManager="clr-namespace:NETworkManager"
1919
xmlns:hostControls="clr-namespace:NETworkManager.Views.Controls"
20-
Loaded="UserControl_Loaded"
2120
mc:Ignorable="d" d:DataContext="{d:DesignInstance viewModels:RemoteDesktopHostViewModel}">
2221
<UserControl.Resources>
2322
<converters:BooleanReverseToVisibilityCollapsedConverter x:Key="BooleanReverseToVisibilityCollapsedConverter" />

‎Source/NETworkManager/Views/RemoteDesktopHostView.xaml.cs‎

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,23 @@
1+
using NETworkManager.Utilities.WPF;
12
using NETworkManager.ViewModels;
2-
using System.Threading.Tasks;
3-
using System.Windows;
43

54
namespace NETworkManager.Views;
65

76
public partial class RemoteDesktopHostView : IApplicationRequestHandler
87
{
98
private readonly RemoteDesktopHostViewModel _viewModel = new();
109

11-
private bool _loaded;
12-
1310
public RemoteDesktopHostView()
1411
{
1512
InitializeComponent();
1613
DataContext = _viewModel;
1714
}
1815

19-
private void UserControl_Loaded(object sender, RoutedEventArgs e)
20-
{
21-
_loaded = true;
22-
}
23-
2416
public async void Open(ApplicationRequest request)
2517
{
2618
// Wait for the interface to load, before displaying the dialog to connect a new Profile...
2719
// MahApps will throw an exception...
28-
while (!_loaded)
29-
await Task.Delay(250);
20+
await this.WaitForLoadedAsync();
3021

3122
if (request.Profile != null)
3223
_viewModel.ConnectProfile(request.Profile);

‎Source/NETworkManager/Views/TigerVNCHostView.xaml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
xmlns:wpfHelpers="clr-namespace:NETworkManager.Utilities.WPF;assembly=NETworkManager.Utilities.WPF"
1818
xmlns:networkManager="clr-namespace:NETworkManager"
1919
xmlns:hostControls="clr-namespace:NETworkManager.Views.Controls"
20-
Loaded="UserControl_Loaded"
2120
mc:Ignorable="d" d:DataContext="{d:DesignInstance viewModels:TigerVNCHostViewModel}">
2221
<UserControl.Resources>
2322
<converters:BooleanToVisibilityCollapsedConverter x:Key="BooleanToVisibilityCollapsedConverter" />

‎Source/NETworkManager/Views/TigerVNCHostView.xaml.cs‎

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,23 @@
1+
using NETworkManager.Utilities.WPF;
12
using NETworkManager.ViewModels;
2-
using System.Threading.Tasks;
3-
using System.Windows;
43

54
namespace NETworkManager.Views;
65

76
public partial class TigerVNCHostView : IApplicationRequestHandler
87
{
98
private readonly TigerVNCHostViewModel _viewModel = new();
109

11-
private bool _loaded;
12-
1310
public TigerVNCHostView()
1411
{
1512
InitializeComponent();
1613
DataContext = _viewModel;
1714
}
1815

19-
private void UserControl_Loaded(object sender, RoutedEventArgs e)
20-
{
21-
_loaded = true;
22-
}
23-
2416
public async void Open(ApplicationRequest request)
2517
{
2618
// Wait for the interface to load, before displaying the dialog to connect a new profile...
2719
// MahApps will throw an exception...
28-
while (!_loaded)
29-
await Task.Delay(250);
20+
await this.WaitForLoadedAsync();
3021

3122
if (!_viewModel.IsConfigured)
3223
return;

‎Source/NETworkManager/Views/WebConsoleHostView.xaml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
xmlns:wpfHelpers="clr-namespace:NETworkManager.Utilities.WPF;assembly=NETworkManager.Utilities.WPF"
1919
xmlns:networkManager="clr-namespace:NETworkManager"
2020
xmlns:hostControls="clr-namespace:NETworkManager.Views.Controls"
21-
Loaded="UserControl_Loaded"
2221
mc:Ignorable="d" d:DataContext="{d:DesignInstance viewModels:WebConsoleHostViewModel}">
2322
<UserControl.Resources>
2423
<converters:BooleanToVisibilityCollapsedConverter x:Key="BooleanToVisibilityCollapsedConverter" />

0 commit comments

Comments
 (0)