Exemplo n.º 1
0
        private void Button_Click_old(object sender, RoutedEventArgs e)
        {
            progressBar.IsIndeterminate = true;
            statusText.Text             = "Calculating, please wait...";

            var task = Task.Factory.StartNew(() =>
            {
                var results = PrimesCalculator.GetAllPrimes(1000, 180000, CancellationToken.None, null);
                return(results);
            });

            task.ContinueWith(t =>
            {
                resultsList.ItemsSource = task.Result;

                progressBar.IsIndeterminate = false;
                statusText.Text             = "Calculation Completed";
            }, TaskScheduler.FromCurrentSynchronizationContext());
        }
Exemplo n.º 2
0
        private async void Button_Click(object sender, RoutedEventArgs e)
        {
            _cts = new CancellationTokenSource();

            try
            {
                var confirm = await GetConfirmationFromUser();

                if (!confirm)
                {
                    return;
                }

                progressBar.Value = 0;
                statusText.Text   = "Calculating, please wait...";

                var pr = new Progress <double>(val => progressBar.Value = val);

                resultsList.ItemsSource = await Task.Factory.StartNew(() => PrimesCalculator.GetAllPrimes(1000, 180000, _cts.Token, pr));

                statusText.Text = "Calculation Completed";
            }
            catch (OperationCanceledException)
            {
                statusText.Text = "Cancelled";
            }
            catch (Exception)
            {
                statusText.Text = "Calculation Failed";
            }
            finally {
                progressBar.Value = 0;

                _cts = null;
            }
        }