public ActivityExecutionContext CreateExecutionContext(Activity activity)
        {
            if (this.ownerContext == null)
            {
                throw new ObjectDisposedException("ActivityExecutionContextManager");
            }

            if (activity == null)
            {
                throw new ArgumentNullException("activity");
            }

            if (!this.ownerContext.IsValidChild(activity, true))
            {
                throw new ArgumentException(SR.GetString(SR.AEC_InvalidActivity), "activity");
            }

            Activity copiedActivity = activity.Clone();

            ((IDependencyObjectAccessor)copiedActivity).InitializeInstanceForRuntime(this.ownerContext.Activity.WorkflowCoreRuntime);

            //Reset the cloned tree for execution.
            Queue <Activity> activityQueue = new Queue <Activity>();

            activityQueue.Enqueue(copiedActivity);

            while (activityQueue.Count != 0)
            {
                Activity clonedActivity = activityQueue.Dequeue();
                if (clonedActivity.ExecutionStatus != ActivityExecutionStatus.Initialized)
                {
                    clonedActivity.ResetAllKnownDependencyProperties();
                    CompositeActivity compositeActivity = clonedActivity as CompositeActivity;

                    if (compositeActivity != null)
                    {
                        for (int i = 0; i < compositeActivity.EnabledActivities.Count; ++i)
                        {
                            activityQueue.Enqueue(compositeActivity.EnabledActivities[i]);
                        }

                        ISupportAlternateFlow alternateFlow = compositeActivity as ISupportAlternateFlow;

                        if (alternateFlow != null)
                        {
                            for (int i = 0; i < alternateFlow.AlternateFlowActivities.Count; ++i)
                            {
                                activityQueue.Enqueue(alternateFlow.AlternateFlowActivities[i]);
                            }
                        }
                    }
                }
            }

            // get active context activities and add it to this one
            IList <Activity> activeContexts = (IList <Activity>) this.ownerContext.Activity.ContextActivity.GetValue(Activity.ActiveExecutionContextsProperty);

            if (activeContexts == null)
            {
                activeContexts = new List <Activity>();
                this.ownerContext.Activity.ContextActivity.SetValue(Activity.ActiveExecutionContextsProperty, activeContexts);
            }
            activeContexts.Add(copiedActivity);

            // prepare the copied activity as a context activity
            ActivityExecutionContextInfo contextInfo = new ActivityExecutionContextInfo(activity.QualifiedName, this.ownerContext.WorkflowCoreRuntime.GetNewContextActivityId(), Guid.NewGuid(), this.ownerContext.ContextId);

            copiedActivity.SetValue(Activity.ActivityExecutionContextInfoProperty, contextInfo);
            copiedActivity.SetValue(Activity.ActivityContextGuidProperty, contextInfo.ContextGuid);
            ActivityExecutionContext newExecutionContext = null;

            try
            {
                // inform workflow runtime
                this.ownerContext.Activity.WorkflowCoreRuntime.RegisterContextActivity(copiedActivity);

                // return the new context
                newExecutionContext = new ActivityExecutionContext(copiedActivity);
                this.executionContexts.Add(newExecutionContext);
                newExecutionContext.InitializeActivity(newExecutionContext.Activity);
                return(newExecutionContext);
            }
            catch (Exception)
            {
                if (newExecutionContext != null)
                {
                    this.CompleteExecutionContext(newExecutionContext);
                }
                else
                {
                    activeContexts.Remove(copiedActivity);
                }
                throw;
            }
        }
        public ActivityExecutionContext CreateExecutionContext(Activity activity)
        {
            ActivityExecutionContext context2;

            if (this.ownerContext == null)
            {
                throw new ObjectDisposedException("ActivityExecutionContextManager");
            }
            if (activity == null)
            {
                throw new ArgumentNullException("activity");
            }
            if (!this.ownerContext.IsValidChild(activity, true))
            {
                throw new ArgumentException(SR.GetString("AEC_InvalidActivity"), "activity");
            }
            Activity item = activity.Clone();

            ((IDependencyObjectAccessor)item).InitializeInstanceForRuntime(this.ownerContext.Activity.WorkflowCoreRuntime);
            Queue <Activity> queue = new Queue <Activity>();

            queue.Enqueue(item);
            while (queue.Count != 0)
            {
                Activity activity3 = queue.Dequeue();
                if (activity3.ExecutionStatus != ActivityExecutionStatus.Initialized)
                {
                    activity3.ResetAllKnownDependencyProperties();
                    CompositeActivity activity4 = activity3 as CompositeActivity;
                    if (activity4 != null)
                    {
                        for (int i = 0; i < activity4.EnabledActivities.Count; i++)
                        {
                            queue.Enqueue(activity4.EnabledActivities[i]);
                        }
                        ISupportAlternateFlow flow = activity4;
                        if (flow != null)
                        {
                            for (int j = 0; j < flow.AlternateFlowActivities.Count; j++)
                            {
                                queue.Enqueue(flow.AlternateFlowActivities[j]);
                            }
                        }
                    }
                }
            }
            IList <Activity> list = (IList <Activity>) this.ownerContext.Activity.ContextActivity.GetValue(Activity.ActiveExecutionContextsProperty);

            if (list == null)
            {
                list = new List <Activity>();
                this.ownerContext.Activity.ContextActivity.SetValue(Activity.ActiveExecutionContextsProperty, list);
            }
            list.Add(item);
            ActivityExecutionContextInfo info = new ActivityExecutionContextInfo(activity.QualifiedName, this.ownerContext.WorkflowCoreRuntime.GetNewContextActivityId(), Guid.NewGuid(), this.ownerContext.ContextId);

            item.SetValue(Activity.ActivityExecutionContextInfoProperty, info);
            item.SetValue(Activity.ActivityContextGuidProperty, info.ContextGuid);
            ActivityExecutionContext context = null;

            try
            {
                this.ownerContext.Activity.WorkflowCoreRuntime.RegisterContextActivity(item);
                context = new ActivityExecutionContext(item);
                this.executionContexts.Add(context);
                context.InitializeActivity(context.Activity);
                context2 = context;
            }
            catch (Exception)
            {
                if (context != null)
                {
                    this.CompleteExecutionContext(context);
                }
                else
                {
                    list.Remove(item);
                }
                throw;
            }
            return(context2);
        }