Dispose() public method

public Dispose ( ) : void
return void
        private Action<Task<CalculatorResult>> ErrorAction(AutoRenewLease autoRenew)
        {
            return (task) => {
                task.Exception.Handle((inner) => {
                    if (inner is OperationCanceledException) {
                        autoRenew.Dispose();
                        Trace.WriteLine("Calculation Canceled");
                    }
                    else {
                        Trace.WriteLine("Unhandled exception when calculating the fibonacci sequence...", UnwindException(inner));
                    }

                    isCalculating = false;
                    return true;
                });
            };
        }
        public void Calculate()
        {
            CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) => {
                configSetter(RoleEnvironment.GetConfigurationSettingValue(configName));
            });

            var acct = CloudStorageAccount.FromConfigurationSetting("StorageConnectionString");
            var client = acct.CreateCloudBlobClient();
            var taskBlob = client.GetBlobReference("dev-task-blobs/fib-task-blob");
            var autoRenew = new AutoRenewLease(taskBlob);

            if (autoRenew.HasLease) {

                var cancellationSource = new CancellationTokenSource();
                var cancellationToken = cancellationSource.Token;
                var calculatorState = new CalculatorState { MaxNumbersInSequence = 48};

                var calculatorTask = Task.Factory.StartNew<CalculatorResult>((state) => {

                    isCalculating = true;
                    cancellationToken.ThrowIfCancellationRequested();

                    var cs = (CalculatorState)state;
                    var calculatorResult = new CalculatorResult();
                    Func<int, int> fib = null;
                    fib = number => number > 1 ? fib(number - 1) + fib(number - 2) : number;

                    while (autoRenew.HasLease && isCalculating && !cancellationToken.IsCancellationRequested) {
                        for (int i = 0; i < cs.MaxNumbersInSequence; i++) {
                            if (!autoRenew.HasLease) {
                                isCalculating = false;
                                Trace.WriteLine("autoRenew.HasLease is false.  Breaking out of calculation routine.");
                                cancellationSource.Cancel();
                                break;
                            }
                            else {
                                var nextNumber = fib(i);
                                calculatorResult.Numbers.Add(nextNumber);
                                Trace.WriteLine(String.Format("ix:{0} - {1}", (i + 1), nextNumber));
                            }
                        }

                        isCalculating = false;
                    }

                    autoRenew.Dispose();
                    cancellationToken.ThrowIfCancellationRequested();
                    return calculatorResult;

                }, calculatorState, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Default);

                calculatorTask.ContinueWith(CompletedAction(autoRenew), cancellationToken, TaskContinuationOptions.OnlyOnRanToCompletion, TaskScheduler.Default);

                calculatorTask.ContinueWith(ErrorAction(autoRenew), TaskContinuationOptions.OnlyOnFaulted);
            }
        }
        private Action<Task<CalculatorResult>> CompletedAction(AutoRenewLease autoRenew)
        {
            return (task) => {
                autoRenew.Dispose();
                Trace.WriteLine("Calculation is complete...");
                Trace.WriteLine("Last number in the sequence is: " + task.Result.Numbers.Last());

                isCalculating = false;
            };
        }