private async void CalculateAreaForSelectedPolygon()
        {
            if (Calculator == null)
            {
                throw new InvalidOperationException("No calculator available");
            }

            if (SelectedPolygon == null)
            {
                throw new InvalidOperationException("No polygon selected");
            }

            if (IsCalculatingArea)
            {
                throw new InvalidOperationException("Calculation is already running");
            }

            CancelSource      = new CancellationTokenSource();
            Area              = null;
            IsCalculatingArea = true;
            try
            {
                Area = await Calculator.CalculateAreaAsync(SelectedPolygon.Points, CancelSource.Token,
                                                           new Progress <double>(progress => Progress = progress));
            }
            catch (OperationCanceledException)
            {
                Area = null;
            }

            IsCalculatingArea = false;
            CancelSource      = null;
        }
Exemple #2
0
        private async void CalculateAreaForSelectedPolygon()
        {
            if (IsCalculatingArea)
            {
                throw new InvalidOperationException("Calculation already running");
            }

            if (AreaCalculator == null)
            {
                throw new InvalidOperationException("No Area Calculator defined");
            }

            if (SelectedPolygon == null)
            {
                throw new InvalidOperationException("No polygon selected");
            }

            IsCalculatingArea = true;

            using (CancelSource = new CancellationTokenSource())
            {
                try
                {
                    Area = await AreaCalculator.CalculateAreaAsync(
                        SelectedPolygon.Points,
                        CancelSource.Token,
                        new Progress <AreaCalculationProgress>(p =>
                    {
                        Progress = p.ProgressPercentage;
                        Area = p.AreaIntermediateResult;
                    }));
                }
                catch (OperationCanceledException)
                {
                    // Add logging later...
                }
            }

            IsCalculatingArea = false;
        }