Clicking on 'Test Connection' again cancels previous connection attempt

This commit is contained in:
Liza 2023-02-13 22:42:24 +01:00
parent 019a204862
commit c586ced5bd
2 changed files with 20 additions and 3 deletions

View File

@ -43,6 +43,8 @@ namespace Pal.Client.Net
await _channel.ConnectAsync(cancellationToken); await _channel.ConnectAsync(cancellationToken);
} }
cancellationToken.ThrowIfCancellationRequested();
var accountClient = new AccountService.AccountServiceClient(_channel); var accountClient = new AccountService.AccountServiceClient(_channel);
if (AccountId == null) if (AccountId == null)
{ {
@ -70,6 +72,8 @@ namespace Pal.Client.Net
} }
} }
cancellationToken.ThrowIfCancellationRequested();
if (AccountId == null) if (AccountId == null)
{ {
PluginLog.Warning("TryConnect: No account id to login with"); PluginLog.Warning("TryConnect: No account id to login with");
@ -123,6 +127,7 @@ namespace Pal.Client.Net
return (false, Localization.ConnectionError_LoginReturnedNoToken); return (false, Localization.ConnectionError_LoginReturnedNoToken);
} }
cancellationToken.ThrowIfCancellationRequested();
return (true, string.Empty); return (true, string.Empty);
} }

View File

@ -46,6 +46,8 @@ namespace Pal.Client.Windows
private readonly FileDialogManager _importDialog; private readonly FileDialogManager _importDialog;
private readonly FileDialogManager _exportDialog; private readonly FileDialogManager _exportDialog;
private CancellationTokenSource? _testConnectionCts;
public ConfigWindow() : base(WindowId) public ConfigWindow() : base(WindowId)
{ {
LanguageChanged(); LanguageChanged();
@ -86,6 +88,8 @@ namespace Pal.Client.Windows
{ {
_importDialog.Reset(); _importDialog.Reset();
_exportDialog.Reset(); _exportDialog.Reset();
_testConnectionCts?.Cancel();
_testConnectionCts = null;
} }
public override void Draw() public override void Draw()
@ -377,8 +381,11 @@ namespace Pal.Client.Windows
_connectionText = Localization.Config_TestConnection_Connecting; _connectionText = Localization.Config_TestConnection_Connecting;
_switchToCommunityTab = true; _switchToCommunityTab = true;
CancellationTokenSource cts = new CancellationTokenSource(); _testConnectionCts?.Cancel();
CancellationTokenSource cts = new();
cts.CancelAfter(TimeSpan.FromSeconds(60)); cts.CancelAfter(TimeSpan.FromSeconds(60));
_testConnectionCts = cts;
try try
{ {
@ -386,8 +393,13 @@ namespace Pal.Client.Windows
} }
catch (Exception e) catch (Exception e)
{ {
PluginLog.Error(e, "Could not establish remote connection"); if (cts == _testConnectionCts)
_connectionText = e.ToString(); {
PluginLog.Error(e, "Could not establish remote connection");
_connectionText = e.ToString();
}
else
PluginLog.Warning(e, "Could not establish a remote connection, but user also clicked 'test connection' again so not updating UI");
} }
}); });
} }