CancellationScope.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / NetFx40 / System.Activities / System / Activities / Statements / CancellationScope.cs / 1305376 / CancellationScope.cs

                            //------------------------------------------------------------------------------ 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------------------------

namespace System.Activities.Statements 
{
    using System; 
    using System.Activities; 
    using System.Collections.Generic;
    using System.Collections.ObjectModel; 
    using System.ComponentModel;
    using System.Runtime.Collections;
    using System.Windows.Markup;
 
    [ContentProperty("Body")]
    public sealed class CancellationScope : NativeActivity 
    { 
        Collection variables;
        Variable suppressCancel; 

        public CancellationScope()
            : base()
        { 
            this.suppressCancel = new Variable();
        } 
 
        public Collection Variables
        { 
            get
            {
                if (this.variables == null)
                { 
                    this.variables = new ValidatingCollection
                    { 
                        // disallow null values 
                        OnAddValidationCallback = item =>
                        { 
                            if (item == null)
                            {
                                throw FxTrace.Exception.ArgumentNull("item");
                            } 
                        }
                    }; 
                } 
                return this.variables;
            } 
        }

        [DefaultValue(null)]
        [DependsOn("Variables")] 
        public Activity Body
        { 
            get; 
            set;
        } 

        [DefaultValue(null)]
        [DependsOn("Body")]
        public Activity CancellationHandler 
        {
            get; 
            set; 
        }
 
        protected override void CacheMetadata(NativeActivityMetadata metadata)
        {
            metadata.AddChild(this.Body);
            metadata.AddChild(this.CancellationHandler); 
            metadata.SetVariablesCollection(this.Variables);
            metadata.AddImplementationVariable(this.suppressCancel); 
        } 

        protected override void Execute(NativeActivityContext context) 
        {
            if (this.Body != null)
            {
                context.ScheduleActivity(this.Body, new CompletionCallback(OnBodyComplete)); 
            }
        } 
 
        void OnBodyComplete(NativeActivityContext context, ActivityInstance completedInstance)
        { 
            // Determine whether to run the Cancel based on whether the body
            // canceled rather than whether cancel had been requested.
            if (completedInstance.State == ActivityInstanceState.Canceled ||
                (context.IsCancellationRequested && completedInstance.State == ActivityInstanceState.Faulted)) 

            { 
                // We don't cancel the cancel handler 
                this.suppressCancel.Set(context, true);
 
                context.MarkCanceled();

                if (this.CancellationHandler != null)
                { 
                    context.ScheduleActivity(this.CancellationHandler, onFaulted : new FaultCallback(OnExceptionFromCancelHandler));
                } 
            } 
        }
 
        protected override void Cancel(NativeActivityContext context)
        {
            bool suppressCancel = this.suppressCancel.Get(context);
            if (!suppressCancel) 
            {
                context.CancelChildren(); 
            } 
        }
 
        void OnExceptionFromCancelHandler(NativeActivityFaultContext context, Exception propagatedException, ActivityInstance propagatedFrom)
        {
            this.suppressCancel.Set(context, false);
        } 
    }
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK