private async void button1_Click(object sender, EventArgs e)
        {
            progressBarSample = new ProgressBarSample();
            progressBarSample.ProgressChanged += OnProgressChanged;
            cancellationTokenSource            = new System.Threading.CancellationTokenSource();
            var token = cancellationTokenSource.Token;

            string message = "";
            bool   isError = false;

            try
            {
                var task = await Task <bool> .Factory.StartNew(() => progressBarSample.Work(token), token);

                //await task;
            }
            catch (OperationCanceledException)
            {
            }
            catch (Exception ex)
            {
                isError = true;
                message = $"Произошла ошибка {ex.Message}";
            }

            if (!isError)
            {
                message = true ? "Процесс отменет" : "Процесс завершен";
            }

            MessageBox.Show(message);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            _progressBarSample = new ProgressBarSample();
            _progressBarSample.ProgressChanged += OnProgressChanged;
            _progressBarSample.WorkCompleted   += OnWorkCompleted;

            var thread = new Thread(() => _progressBarSample.Work(synchronizationContext));

            thread.Start();
            button1.Enabled = false;
        }
예제 #3
0
        private void button1_Click(object sender, EventArgs e)
        {
            progressBarSample = new ProgressBarSample();
            progressBarSample.ProgressChanged += OnProgressChanged;

            button1.Enabled = false;
            // Dowork method executed in new thread, but work completed will start in main thread (ui-thread)
            System.Threading.Tasks.Task.Factory
            .StartNew(() => progressBarSample.DoWork())
            .ContinueWith((t, o) => WorkCompleted(t.Result), null, taskScheduler);
        }
예제 #4
0
        private async void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled   = false;
            progressBarSample = new ProgressBarSample();
            progressBarSample.ProgressChanged += OnProgressChanged;

            //progressBarSample.WorkCompleted += OnWorkCompleted;
            var cancelled = await Task <bool> .Factory.StartNew(() => progressBarSample.DoWork());

            string message = cancelled ? "Work was cancelled" : "Work was done";

            MessageBox.Show(message);
            button1.Enabled = true;
        }