private void AddPermToTest_Click(object sender, EventArgs e) { progressBar1.Visible = true; progressBar1.Value = 0; progressBar1.Maximum = files.Count; label2.Text = ((double)progressBar1.Value / progressBar1.Maximum).ToString("P"); var syncContextScheduler = TaskScheduler.FromCurrentSynchronizationContext(); tabControl1.SelectTab(1); var transferTask = new Task(() => OldOwnerGoogleService.RejectRights(files, NewOwnerGoogleService, file => { var task = new Task(() => { label2.Text = ((double)progressBar1.Value / progressBar1.Maximum).ToString("P"); progressBar1.Increment(1); }); task.Start(syncContextScheduler); })); var stopWatch = new Stopwatch(); transferTask.ContinueWith(t => { stopWatch.Stop(); UpdateFileList(); progressBar1.Visible = false; var execSec = (double)stopWatch.ElapsedMilliseconds / 1000; MessageBox.Show(null, $"Перенос завершен. Время работы программы {execSec.ToString("F2")} секунд", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); }, CancellationToken.None, TaskContinuationOptions.OnlyOnRanToCompletion, syncContextScheduler); transferTask.ContinueWith(t => { var message = Helpers.GetFullMessage(t.Exception); MessageBox.Show(null, message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); UpdateFileList(); progressBar1.Visible = false; }, CancellationToken.None, TaskContinuationOptions.OnlyOnFaulted, syncContextScheduler); transferTask.Start(); stopWatch.Start(); }