ForEach.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 / ForEach.cs / 1305376 / ForEach.cs

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

namespace System.Activities.Statements 
{
    using System.Collections; 
    using System.Collections.Generic; 
    using System.ComponentModel;
    using System.Runtime; 
    using System.Runtime.Serialization;
    using System.Windows.Markup;
    using System.Activities;
    using System.Activities.Validation; 

    [ContentProperty("Body")] 
    public sealed class ForEach : NativeActivity 
    {
        Variable> valueEnumerator; 
        CompletionCallback onChildComplete;

        public ForEach()
            : base() 
        {
            this.valueEnumerator = new Variable>(); 
        } 

        [DefaultValue(null)] 
        public ActivityAction Body
        {
            get;
            set; 
        }
 
        [RequiredArgument] 
        [DefaultValue(null)]
        public InArgument> Values 
        {
            get;
            set;
        } 

        CompletionCallback OnChildComplete 
        { 
            get
            { 
                if (this.onChildComplete == null)
                {
                    this.onChildComplete = new CompletionCallback(GetStateAndExecute);
                } 

                return this.onChildComplete; 
            } 
        }
 
        protected override void CacheMetadata(NativeActivityMetadata metadata)
        {
            RuntimeArgument valuesArgument = new RuntimeArgument("Values", typeof(IEnumerable), ArgumentDirection.In, true);
            metadata.Bind(this.Values, valuesArgument); 

            metadata.AddArgument(valuesArgument); 
            metadata.AddDelegate(this.Body); 
            metadata.AddImplementationVariable(this.valueEnumerator);
        } 

        protected override void Execute(NativeActivityContext context)
        {
            IEnumerable values = this.Values.Get(context); 
            if (values == null)
            { 
                throw FxTrace.Exception.AsError(new InvalidOperationException(SR.ForEachRequiresNonNullValues(this.DisplayName))); 
            }
 
            IEnumerator valueEnumerator = values.GetEnumerator();
            this.valueEnumerator.Set(context, valueEnumerator);

            if (this.Body == null || this.Body.Handler == null) 
            {
                while (valueEnumerator.MoveNext()) 
                { 
                    // do nothing
                }; 
                valueEnumerator.Dispose();
                return;
            }
            InternalExecute(context, null, valueEnumerator); 
        }
 
        void GetStateAndExecute(NativeActivityContext context, ActivityInstance completedInstance) 
        {
            IEnumerator valueEnumerator = this.valueEnumerator.Get(context); 
            Fx.Assert(valueEnumerator != null, "GetStateAndExecute");
            InternalExecute(context, completedInstance, valueEnumerator);
        }
 
        void InternalExecute(NativeActivityContext context, ActivityInstance completedInstance, IEnumerator valueEnumerator)
        { 
            Fx.Assert(this.Body != null && this.Body.Handler != null, "Body and Body.Handler should not be null"); 

            if (!valueEnumerator.MoveNext()) 
            {
                if (completedInstance != null)
                {
                    if (completedInstance.State == ActivityInstanceState.Canceled || 
                        (context.IsCancellationRequested && completedInstance.State == ActivityInstanceState.Faulted))
                    { 
                        context.MarkCanceled(); 
                    }
                } 
                valueEnumerator.Dispose();
                return;
            }
 
            // After making sure there is another value, let's check for cancelation
            if (context.IsCancellationRequested) 
            { 
                context.MarkCanceled();
                valueEnumerator.Dispose(); 
                return;
            }

            context.ScheduleAction(this.Body, valueEnumerator.Current, this.OnChildComplete); 
        }
    } 
} 

// 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