Skip to content

Commit fc4de8d

Browse files
committed
Fix: Network connection check
1 parent b2242d9 commit fc4de8d

File tree

2 files changed

+62
-33
lines changed

2 files changed

+62
-33
lines changed

Source/NETworkManager/ViewModels/NetworkConnectionWidgetViewModel.cs

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -472,52 +472,63 @@ public void Check()
472472

473473
private CancellationTokenSource _tokenSource;
474474
private CancellationToken _ct;
475+
private Task _checkTask = Task.CompletedTask;
475476

476477
private async Task CheckAsync()
477478
{
478-
// Already in queue
479-
if (_tokenSource is { IsCancellationRequested: true })
479+
// Return if cancellation is already requested
480+
if(_tokenSource is { IsCancellationRequested: true })
480481
return;
481-
482-
// Cancel if running
482+
483+
// Cancel previous checks if running
483484
if (_isChecking)
484-
{
485-
_tokenSource.Cancel();
485+
{
486+
await _tokenSource.CancelAsync();
486487

487-
while (_isChecking) await Task.Delay(250, _ct);
488+
try
489+
{
490+
await _checkTask;
491+
}
492+
catch(OperationCanceledException)
493+
{
494+
// Handle task cancellation
495+
}
496+
finally
497+
{
498+
_tokenSource.Dispose();
499+
}
488500
}
489-
501+
490502
// Start check
491503
_isChecking = true;
492-
493504
_tokenSource = new CancellationTokenSource();
494505
_ct = _tokenSource.Token;
495-
506+
507+
// Run tasks
496508
try
497509
{
498-
await Task.Run(async () =>
499-
{
500-
List<Task> tasks = new()
501-
{
502-
CheckConnectionComputerAsync(_ct),
503-
CheckConnectionRouterAsync(_ct),
504-
CheckConnectionInternetAsync(_ct)
505-
};
506-
507-
await Task.WhenAll(tasks);
508-
}, _tokenSource.Token);
510+
_checkTask = RunTasks(_ct);
511+
await _checkTask;
509512
}
510-
catch (OperationCanceledException)
513+
catch(OperationCanceledException)
511514
{
512-
515+
// Handle task cancellation
513516
}
514517
finally
515518
{
516-
_tokenSource.Dispose();
517519
_isChecking = false;
518520
}
519521
}
520-
522+
523+
private async Task RunTasks(CancellationToken ct)
524+
{
525+
await Task.WhenAll(
526+
CheckConnectionComputerAsync(ct),
527+
CheckConnectionRouterAsync(ct),
528+
CheckConnectionInternetAsync(ct)
529+
);
530+
}
531+
521532
private Task CheckConnectionComputerAsync(CancellationToken ct)
522533
{
523534
return Task.Run(async () =>
@@ -847,7 +858,6 @@ private Task CheckConnectionInternetAsync(CancellationToken ct)
847858
IsInternetDNSChecking = false;
848859
}, ct);
849860
}
850-
851861
#endregion
852862

853863
}

Source/NETworkManager/Views/DashboardView.xaml.cs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,45 @@ public partial class DashboardView
66
{
77
private readonly DashboardViewModel _viewModel = new();
88

9-
9+
private readonly NetworkConnectionWidgetView _networkConnectionWidgetView = new();
10+
private readonly IPApiIPGeolocationWidgetView _ipApiIPGeolocationWidgetView = new();
11+
private readonly IPApiDNSResolverWidgetView _ipApiDNSResolverWidgetView = new();
12+
1013

1114
public DashboardView()
1215
{
1316
InitializeComponent();
1417
DataContext = _viewModel;
1518

1619
// Load views
17-
ContentControlNetworkConnection.Content = new NetworkConnectionWidgetView();
18-
ContentControlIPApiIPGeolocation.Content = new IPApiIPGeolocationWidgetView();
19-
ContentControlIPApiDNSResolver.Content = new IPApiDNSResolverWidgetView();
20+
ContentControlNetworkConnection.Content = _networkConnectionWidgetView;
21+
ContentControlIPApiIPGeolocation.Content = _ipApiIPGeolocationWidgetView;
22+
ContentControlIPApiDNSResolver.Content = _ipApiDNSResolverWidgetView;
23+
24+
// Check all widgets
25+
Check();
26+
}
27+
28+
public void OnViewVisible()
29+
{
30+
_viewModel.OnViewVisible();
31+
32+
// Check all widgets
33+
Check();
2034
}
2135

2236
public void OnViewHide()
2337
{
2438
_viewModel.OnViewHide();
2539
}
26-
27-
public void OnViewVisible()
40+
41+
/// <summary>
42+
/// Check all widgets
43+
/// </summary>
44+
private void Check()
2845
{
29-
_viewModel.OnViewVisible();
46+
_networkConnectionWidgetView.Check();
47+
_ipApiIPGeolocationWidgetView.Check();
48+
_ipApiDNSResolverWidgetView.Check();
3049
}
3150
}

0 commit comments

Comments
 (0)