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,18 +381,26 @@ 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
{ {
_connectionText = await Service.RemoteApi.VerifyConnection(cts.Token); _connectionText = await Service.RemoteApi.VerifyConnection(cts.Token);
} }
catch (Exception e) catch (Exception e)
{
if (cts == _testConnectionCts)
{ {
PluginLog.Error(e, "Could not establish remote connection"); PluginLog.Error(e, "Could not establish remote connection");
_connectionText = e.ToString(); _connectionText = e.ToString();
} }
else
PluginLog.Warning(e, "Could not establish a remote connection, but user also clicked 'test connection' again so not updating UI");
}
}); });
} }