diff --git a/csharp/ExcelAddIn/providers/SessionProvider.cs b/csharp/ExcelAddIn/providers/SessionProvider.cs index d7cbc0c1175..f2471281b90 100644 --- a/csharp/ExcelAddIn/providers/SessionProvider.cs +++ b/csharp/ExcelAddIn/providers/SessionProvider.cs @@ -102,19 +102,16 @@ void CreateSessionBaseInSeparateThread(CredentialsBase credentials) { result = StatusOr.OfStatus(ex.Message); } - // By the time we get here, some time has passed. Decide whether to keep it. + // By the time we get here, some time has passed. Have our results become moot? if (!ReferenceEquals(localLatestCookie, _sharedSetCredentialsCookie.Value)) { - // No, it's stale. Dispose the SessionBase if we have it. - _ = result.AcceptVisitor(sb => { - sb.Dispose(); - return Unit.Instance; - }, - _ => Unit.Instance - ); + // Our results are moot. Dispose of them. + if (result.GetValueOrStatus(out var sb, out _)) { + sb.Dispose(); + } return; } - // Keep it and tell everyone about it (on the worker thread). + // Our results are valid. Keep them and tell everyone about it (on the worker thread). workerThread.Invoke(() => _sessionObservers.SetAndSend(ref _session, result)); }