public void Dispose() {
if (this.task != null) {
try {
this.cancelTaskSource.Cancel();
this.task.Wait(this.Interval);
this.task.Dispose();
} catch (InvalidOperationException) {
// Disposing the login task before it is finished is not a problem
} catch (TaskCanceledException) {
// It is fine if the task is canceled
} catch (AggregateException) {
// It is also fine if the task is canceled
} finally {
this.cancelTaskSource.Dispose();
}
}
}