private async void btnStartWorker_Click(object sender, RoutedEventArgs e) { if (!isTaskRunning) { oldTime = DateTimeOffset.Now.ToUnixTimeSeconds(); isTaskRunning = true; pbRefresh.Value = 0; btnRefresh.Content = "Cancel"; tbStatus.Text = "Step 1/4: Downloading new item list..."; tbPbMessage.Visibility = Visibility.Hidden; pbRefresh.Visibility = Visibility.Visible; tbMaxAge.IsEnabled = false; ctSource = new CancellationTokenSource(); progress = new Progress <DownloadProgressReportModel>(); progress.ProgressChanged += ReportProgress; worker = new AsyncUpdateItemDatabase(); try { bool finishedWithErrors = await Task.Run(() => worker.DoWork(progress, ctSource.Token)); tbMaxAge.IsEnabled = true; pbRefresh.Value = 0; btnRefresh.Content = "Update items"; tbPbMessage.Visibility = Visibility.Visible; pbRefresh.Visibility = Visibility.Hidden; tbStatus.Text = "Total Items: " + SqliteDataAccess.CountItems().ToString(); if (finishedWithErrors) { tbPbMessage.Text = "Finished with errors! To correct errors, click on the button again!"; } else { tbPbMessage.Text = "If you would like to update item database, please click on the button."; } } catch (OperationCanceledException) { btnRefresh.IsEnabled = true; tbMaxAge.IsEnabled = true; pbRefresh.Value = 0; btnRefresh.Content = "Update items"; tbPbMessage.Visibility = Visibility.Visible; pbRefresh.Visibility = Visibility.Hidden; tbStatus.Text = "Total Items: " + SqliteDataAccess.CountItems().ToString(); tbPbMessage.Text = "Item update cancelled! To resume or retry, click the button."; } isTaskRunning = false; } else { isTaskRunning = false; btnRefresh.IsEnabled = false; ctSource.Cancel(); } }