Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / WF / Activities / EventHandlingScope.cs / 1305376 / EventHandlingScope.cs
namespace System.Workflow.Activities { #region Imports using System; using System.Text; using System.Reflection; using System.Collections; using System.CodeDom; using System.ComponentModel; using System.ComponentModel.Design; using System.ComponentModel.Design.Serialization; using System.Drawing; using System.Drawing.Drawing2D; using System.Workflow.ComponentModel; using System.Workflow.ComponentModel.Design; using System.Workflow.ComponentModel.Compiler; using System.Workflow.Activities.Common; #endregion [SRDescription(SR.EventHandlingScopeActivityDescription)] [ToolboxItem(typeof(ActivityToolboxItem))] [ToolboxBitmap(typeof(EventHandlingScopeActivity), "Resources.Sequence.png")] [ActivityValidator(typeof(EventHandlingScopeValidator))] [Designer(typeof(EventHandlingScopeDesigner), typeof(IDesigner))] public sealed class EventHandlingScopeActivity : CompositeActivity, IActivityEventListener{ public EventHandlingScopeActivity() { } public EventHandlingScopeActivity(string name) : base(name) { } protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext) { if (executionContext == null) throw new ArgumentNullException("executionContext"); Activity bodyActivity = this.BodyActivity; if (bodyActivity == null) return ActivityExecutionStatus.Closed; // run EventHandlers EventHandlersActivity eventHandlers = this.EventHandlersActivity; if (eventHandlers != null) { eventHandlers.RegisterForStatusChange(Activity.ClosedEvent, this); executionContext.ExecuteActivity(eventHandlers); } // run body bodyActivity.RegisterForStatusChange(Activity.ClosedEvent, this); executionContext.ExecuteActivity(bodyActivity); // return the status return this.ExecutionStatus; } protected override ActivityExecutionStatus Cancel(ActivityExecutionContext executionContext) { if (executionContext == null) throw new ArgumentNullException("executionContext"); Activity bodyActivity = this.BodyActivity; EventHandlersActivity eventHandlers = this.EventHandlersActivity; if(bodyActivity == null && eventHandlers == null) return ActivityExecutionStatus.Closed; bool cancelScheduled = false; // check the status of body if (bodyActivity != null && bodyActivity.ExecutionStatus == ActivityExecutionStatus.Executing) { executionContext.CancelActivity(bodyActivity); cancelScheduled = true; } //Check the status of EventHandlers if (eventHandlers != null && eventHandlers.ExecutionStatus == ActivityExecutionStatus.Executing) { executionContext.CancelActivity(eventHandlers); cancelScheduled = true; } if (cancelScheduled || (bodyActivity != null && (bodyActivity.ExecutionStatus == ActivityExecutionStatus.Faulting || bodyActivity.ExecutionStatus == ActivityExecutionStatus.Canceling)) || (eventHandlers != null && (eventHandlers.ExecutionStatus == ActivityExecutionStatus.Faulting || eventHandlers.ExecutionStatus == ActivityExecutionStatus.Canceling)) ) { return this.ExecutionStatus; } return ActivityExecutionStatus.Closed; } #region IActivityEventListener Members void IActivityEventListener .OnEvent(object sender, ActivityExecutionStatusChangedEventArgs e) { ActivityExecutionContext context = sender as ActivityExecutionContext; if (context == null) throw new ArgumentException(); e.Activity.UnregisterForStatusChange(Activity.ClosedEvent, this); if (e.Activity is EventHandlersActivity) { if (this.BodyActivity.ExecutionStatus == ActivityExecutionStatus.Closed) { context.CloseActivity(); } //else Eventhandlers faulted, let exception propagate up. } else { EventHandlersActivity eventHandlers = this.EventHandlersActivity; if (eventHandlers == null || eventHandlers.ExecutionStatus == ActivityExecutionStatus.Closed) { context.CloseActivity(); } else { eventHandlers.UnsubscribeAndClose(); } } } #endregion internal EventHandlersActivity EventHandlersActivity { get { EventHandlersActivity eventHandlers = null; foreach (Activity child in this.EnabledActivities) { if (child is EventHandlersActivity) eventHandlers = child as EventHandlersActivity; } return eventHandlers; } } internal Activity BodyActivity { get { Activity body = null; foreach (Activity child in this.EnabledActivities) { if (!(child is EventHandlersActivity)) body = child; } return body; } } #region Dynamic Update Methods [NonSerialized] private bool eventHandlersRemovedInDynamicUpdate = false; [NonSerialized] private bool bodyActivityRemovedInDynamicUpdate = false; protected override void OnActivityChangeRemove(ActivityExecutionContext executionContext, Activity removedActivity) { base.OnActivityChangeRemove(executionContext, removedActivity); if (removedActivity is EventHandlersActivity) eventHandlersRemovedInDynamicUpdate = true; else bodyActivityRemovedInDynamicUpdate = true; } protected override void OnWorkflowChangesCompleted(ActivityExecutionContext executionContext) { base.OnWorkflowChangesCompleted(executionContext); switch (this.ExecutionStatus) { case ActivityExecutionStatus.Executing: if (bodyActivityRemovedInDynamicUpdate) { if (EventHandlersActivity == null || EventHandlersActivity.ExecutionStatus == ActivityExecutionStatus.Closed) executionContext.CloseActivity(); else if(EventHandlersActivity.ExecutionStatus == ActivityExecutionStatus.Executing) EventHandlersActivity.UnsubscribeAndClose(); } if (eventHandlersRemovedInDynamicUpdate) { if (BodyActivity == null || BodyActivity.ExecutionStatus == ActivityExecutionStatus.Closed) executionContext.CloseActivity(); } break; default: break; } eventHandlersRemovedInDynamicUpdate = false; bodyActivityRemovedInDynamicUpdate = false; } #endregion } #region Class EventHandlingScopeValidator internal sealed class EventHandlingScopeValidator : CompositeActivityValidator { public override ValidationErrorCollection Validate(ValidationManager manager, object obj) { ValidationErrorCollection validationErrors = base.Validate(manager, obj); EventHandlingScopeActivity compositeActivity = obj as EventHandlingScopeActivity; if (compositeActivity == null) throw new ArgumentException(SR.GetString(SR.Error_UnexpectedArgumentType, typeof(EventHandlingScopeActivity).FullName), "obj"); //we only allow one activity to be inserted int childCount = 0; int eventHandlersCount = 0; foreach (Activity child in compositeActivity.EnabledActivities) { if (child is EventHandlersActivity) eventHandlersCount++; else childCount++; } // check if more than two activities inside the collection if (childCount > 1) validationErrors.Add(new ValidationError(SR.GetString(SR.Error_MoreThanTwoActivitiesInEventHandlingScope, compositeActivity.QualifiedName), ErrorNumbers.Error_MoreThanTwoActivitiesInEventHandlingScope)); // check if more than one EventHandlers if (eventHandlersCount > 1) validationErrors.Add(new ValidationError(SR.GetString(SR.Error_MoreThanOneEventHandlersDecl, compositeActivity.GetType().Name), ErrorNumbers.Error_ScopeMoreThanOneEventHandlersDecl)); return validationErrors; } public override ValidationError ValidateActivityChange(Activity activity, ActivityChangeAction action) { if (activity == null) throw new ArgumentNullException("activity"); if (action == null) throw new ArgumentNullException("action"); if (activity.ExecutionStatus != ActivityExecutionStatus.Initialized && activity.ExecutionStatus != ActivityExecutionStatus.Executing && activity.ExecutionStatus != ActivityExecutionStatus.Closed) { return new ValidationError(SR.GetString(SR.Error_DynamicActivity2, activity.QualifiedName, Enum.GetName(typeof(ActivityExecutionStatus), activity.ExecutionStatus), activity.GetType().FullName), ErrorNumbers.Error_DynamicActivity2); } if (activity.ExecutionStatus == ActivityExecutionStatus.Executing && action is AddedActivityAction) { return new ValidationError(SR.GetString(SR.Error_DynamicActivity3, activity.QualifiedName, Enum.GetName(typeof(ActivityExecutionStatus), activity.ExecutionStatus), activity.GetType().FullName), ErrorNumbers.Error_DynamicActivity2); } return null; } } #endregion #region Class EventHandlingScopeDesigner [ActivityDesignerTheme(typeof(EventHandlingScopeActivityDesignerTheme))] internal sealed class EventHandlingScopeDesigner : SequentialActivityDesigner { #region Properties and Methods public override bool CanExpandCollapse { get { return false; } } #endregion public override bool CanInsertActivities(HitTestInfo insertLocation, System.Collections.ObjectModel.ReadOnlyCollection activitiesToInsert) { //we only allow one activity to be inserted int childCount = 0; foreach (Activity child in ((EventHandlingScopeActivity)this.Activity).Activities) { if (!Helpers.IsFrameworkActivity(child) && !(child is EventHandlersActivity)) childCount++; } if (childCount > 0) return false; return base.CanInsertActivities(insertLocation, activitiesToInsert); } } #endregion #region EventHandlingScopeActivityDesignerTheme internal sealed class EventHandlingScopeActivityDesignerTheme : CompositeDesignerTheme { public EventHandlingScopeActivityDesignerTheme(WorkflowTheme theme) : base(theme) { this.ShowDropShadow = false; this.ConnectorStartCap = LineAnchor.None; this.ConnectorEndCap = LineAnchor.ArrowAnchor; this.ForeColor = Color.FromArgb(0xFF, 0x00, 0x00, 0x00); this.BorderColor = Color.FromArgb(0xFF, 0xE0, 0xE0, 0xE0); this.BorderStyle = DashStyle.Dash; this.BackColorStart = Color.FromArgb(0x00, 0x00, 0x00, 0x00); this.BackColorEnd = Color.FromArgb(0x00, 0x00, 0x00, 0x00); } } #endregion } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved. namespace System.Workflow.Activities { #region Imports using System; using System.Text; using System.Reflection; using System.Collections; using System.CodeDom; using System.ComponentModel; using System.ComponentModel.Design; using System.ComponentModel.Design.Serialization; using System.Drawing; using System.Drawing.Drawing2D; using System.Workflow.ComponentModel; using System.Workflow.ComponentModel.Design; using System.Workflow.ComponentModel.Compiler; using System.Workflow.Activities.Common; #endregion [SRDescription(SR.EventHandlingScopeActivityDescription)] [ToolboxItem(typeof(ActivityToolboxItem))] [ToolboxBitmap(typeof(EventHandlingScopeActivity), "Resources.Sequence.png")] [ActivityValidator(typeof(EventHandlingScopeValidator))] [Designer(typeof(EventHandlingScopeDesigner), typeof(IDesigner))] public sealed class EventHandlingScopeActivity : CompositeActivity, IActivityEventListener { public EventHandlingScopeActivity() { } public EventHandlingScopeActivity(string name) : base(name) { } protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext) { if (executionContext == null) throw new ArgumentNullException("executionContext"); Activity bodyActivity = this.BodyActivity; if (bodyActivity == null) return ActivityExecutionStatus.Closed; // run EventHandlers EventHandlersActivity eventHandlers = this.EventHandlersActivity; if (eventHandlers != null) { eventHandlers.RegisterForStatusChange(Activity.ClosedEvent, this); executionContext.ExecuteActivity(eventHandlers); } // run body bodyActivity.RegisterForStatusChange(Activity.ClosedEvent, this); executionContext.ExecuteActivity(bodyActivity); // return the status return this.ExecutionStatus; } protected override ActivityExecutionStatus Cancel(ActivityExecutionContext executionContext) { if (executionContext == null) throw new ArgumentNullException("executionContext"); Activity bodyActivity = this.BodyActivity; EventHandlersActivity eventHandlers = this.EventHandlersActivity; if(bodyActivity == null && eventHandlers == null) return ActivityExecutionStatus.Closed; bool cancelScheduled = false; // check the status of body if (bodyActivity != null && bodyActivity.ExecutionStatus == ActivityExecutionStatus.Executing) { executionContext.CancelActivity(bodyActivity); cancelScheduled = true; } //Check the status of EventHandlers if (eventHandlers != null && eventHandlers.ExecutionStatus == ActivityExecutionStatus.Executing) { executionContext.CancelActivity(eventHandlers); cancelScheduled = true; } if (cancelScheduled || (bodyActivity != null && (bodyActivity.ExecutionStatus == ActivityExecutionStatus.Faulting || bodyActivity.ExecutionStatus == ActivityExecutionStatus.Canceling)) || (eventHandlers != null && (eventHandlers.ExecutionStatus == ActivityExecutionStatus.Faulting || eventHandlers.ExecutionStatus == ActivityExecutionStatus.Canceling)) ) { return this.ExecutionStatus; } return ActivityExecutionStatus.Closed; } #region IActivityEventListener Members void IActivityEventListener .OnEvent(object sender, ActivityExecutionStatusChangedEventArgs e) { ActivityExecutionContext context = sender as ActivityExecutionContext; if (context == null) throw new ArgumentException(); e.Activity.UnregisterForStatusChange(Activity.ClosedEvent, this); if (e.Activity is EventHandlersActivity) { if (this.BodyActivity.ExecutionStatus == ActivityExecutionStatus.Closed) { context.CloseActivity(); } //else Eventhandlers faulted, let exception propagate up. } else { EventHandlersActivity eventHandlers = this.EventHandlersActivity; if (eventHandlers == null || eventHandlers.ExecutionStatus == ActivityExecutionStatus.Closed) { context.CloseActivity(); } else { eventHandlers.UnsubscribeAndClose(); } } } #endregion internal EventHandlersActivity EventHandlersActivity { get { EventHandlersActivity eventHandlers = null; foreach (Activity child in this.EnabledActivities) { if (child is EventHandlersActivity) eventHandlers = child as EventHandlersActivity; } return eventHandlers; } } internal Activity BodyActivity { get { Activity body = null; foreach (Activity child in this.EnabledActivities) { if (!(child is EventHandlersActivity)) body = child; } return body; } } #region Dynamic Update Methods [NonSerialized] private bool eventHandlersRemovedInDynamicUpdate = false; [NonSerialized] private bool bodyActivityRemovedInDynamicUpdate = false; protected override void OnActivityChangeRemove(ActivityExecutionContext executionContext, Activity removedActivity) { base.OnActivityChangeRemove(executionContext, removedActivity); if (removedActivity is EventHandlersActivity) eventHandlersRemovedInDynamicUpdate = true; else bodyActivityRemovedInDynamicUpdate = true; } protected override void OnWorkflowChangesCompleted(ActivityExecutionContext executionContext) { base.OnWorkflowChangesCompleted(executionContext); switch (this.ExecutionStatus) { case ActivityExecutionStatus.Executing: if (bodyActivityRemovedInDynamicUpdate) { if (EventHandlersActivity == null || EventHandlersActivity.ExecutionStatus == ActivityExecutionStatus.Closed) executionContext.CloseActivity(); else if(EventHandlersActivity.ExecutionStatus == ActivityExecutionStatus.Executing) EventHandlersActivity.UnsubscribeAndClose(); } if (eventHandlersRemovedInDynamicUpdate) { if (BodyActivity == null || BodyActivity.ExecutionStatus == ActivityExecutionStatus.Closed) executionContext.CloseActivity(); } break; default: break; } eventHandlersRemovedInDynamicUpdate = false; bodyActivityRemovedInDynamicUpdate = false; } #endregion } #region Class EventHandlingScopeValidator internal sealed class EventHandlingScopeValidator : CompositeActivityValidator { public override ValidationErrorCollection Validate(ValidationManager manager, object obj) { ValidationErrorCollection validationErrors = base.Validate(manager, obj); EventHandlingScopeActivity compositeActivity = obj as EventHandlingScopeActivity; if (compositeActivity == null) throw new ArgumentException(SR.GetString(SR.Error_UnexpectedArgumentType, typeof(EventHandlingScopeActivity).FullName), "obj"); //we only allow one activity to be inserted int childCount = 0; int eventHandlersCount = 0; foreach (Activity child in compositeActivity.EnabledActivities) { if (child is EventHandlersActivity) eventHandlersCount++; else childCount++; } // check if more than two activities inside the collection if (childCount > 1) validationErrors.Add(new ValidationError(SR.GetString(SR.Error_MoreThanTwoActivitiesInEventHandlingScope, compositeActivity.QualifiedName), ErrorNumbers.Error_MoreThanTwoActivitiesInEventHandlingScope)); // check if more than one EventHandlers if (eventHandlersCount > 1) validationErrors.Add(new ValidationError(SR.GetString(SR.Error_MoreThanOneEventHandlersDecl, compositeActivity.GetType().Name), ErrorNumbers.Error_ScopeMoreThanOneEventHandlersDecl)); return validationErrors; } public override ValidationError ValidateActivityChange(Activity activity, ActivityChangeAction action) { if (activity == null) throw new ArgumentNullException("activity"); if (action == null) throw new ArgumentNullException("action"); if (activity.ExecutionStatus != ActivityExecutionStatus.Initialized && activity.ExecutionStatus != ActivityExecutionStatus.Executing && activity.ExecutionStatus != ActivityExecutionStatus.Closed) { return new ValidationError(SR.GetString(SR.Error_DynamicActivity2, activity.QualifiedName, Enum.GetName(typeof(ActivityExecutionStatus), activity.ExecutionStatus), activity.GetType().FullName), ErrorNumbers.Error_DynamicActivity2); } if (activity.ExecutionStatus == ActivityExecutionStatus.Executing && action is AddedActivityAction) { return new ValidationError(SR.GetString(SR.Error_DynamicActivity3, activity.QualifiedName, Enum.GetName(typeof(ActivityExecutionStatus), activity.ExecutionStatus), activity.GetType().FullName), ErrorNumbers.Error_DynamicActivity2); } return null; } } #endregion #region Class EventHandlingScopeDesigner [ActivityDesignerTheme(typeof(EventHandlingScopeActivityDesignerTheme))] internal sealed class EventHandlingScopeDesigner : SequentialActivityDesigner { #region Properties and Methods public override bool CanExpandCollapse { get { return false; } } #endregion public override bool CanInsertActivities(HitTestInfo insertLocation, System.Collections.ObjectModel.ReadOnlyCollection activitiesToInsert) { //we only allow one activity to be inserted int childCount = 0; foreach (Activity child in ((EventHandlingScopeActivity)this.Activity).Activities) { if (!Helpers.IsFrameworkActivity(child) && !(child is EventHandlersActivity)) childCount++; } if (childCount > 0) return false; return base.CanInsertActivities(insertLocation, activitiesToInsert); } } #endregion #region EventHandlingScopeActivityDesignerTheme internal sealed class EventHandlingScopeActivityDesignerTheme : CompositeDesignerTheme { public EventHandlingScopeActivityDesignerTheme(WorkflowTheme theme) : base(theme) { this.ShowDropShadow = false; this.ConnectorStartCap = LineAnchor.None; this.ConnectorEndCap = LineAnchor.ArrowAnchor; this.ForeColor = Color.FromArgb(0xFF, 0x00, 0x00, 0x00); this.BorderColor = Color.FromArgb(0xFF, 0xE0, 0xE0, 0xE0); this.BorderStyle = DashStyle.Dash; this.BackColorStart = Color.FromArgb(0x00, 0x00, 0x00, 0x00); this.BackColorEnd = Color.FromArgb(0x00, 0x00, 0x00, 0x00); } } #endregion } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- Root.cs
- PermissionListSet.cs
- TakeOrSkipWhileQueryOperator.cs
- StopStoryboard.cs
- RowSpanVector.cs
- BuildManager.cs
- OleDbParameterCollection.cs
- Errors.cs
- BitmapEffectInput.cs
- SystemIPv4InterfaceProperties.cs
- NullableDecimalMinMaxAggregationOperator.cs
- EdmProviderManifest.cs
- RenderTargetBitmap.cs
- MulticastDelegate.cs
- GreenMethods.cs
- HashHelpers.cs
- MonthCalendar.cs
- RevocationPoint.cs
- RegexInterpreter.cs
- DelegatingStream.cs
- ParseElementCollection.cs
- SqlClientWrapperSmiStream.cs
- ProfileService.cs
- Vector.cs
- EdmFunction.cs
- HeaderFilter.cs
- GeneralTransformGroup.cs
- TemplateBindingExpressionConverter.cs
- WindowProviderWrapper.cs
- ColumnCollection.cs
- CodeIdentifiers.cs
- ColumnHeaderConverter.cs
- ApplicationInfo.cs
- AdapterDictionary.cs
- EmptyImpersonationContext.cs
- WebPartConnectionsConnectVerb.cs
- FastEncoder.cs
- SqlTypeConverter.cs
- XamlBuildProvider.cs
- SmtpException.cs
- ManagedWndProcTracker.cs
- PrePrepareMethodAttribute.cs
- NGCSerializer.cs
- InputBindingCollection.cs
- EntityDataSource.cs
- JavaScriptSerializer.cs
- MetadataItemEmitter.cs
- Shape.cs
- Console.cs
- BrushValueSerializer.cs
- CodeTypeConstructor.cs
- NullableBoolConverter.cs
- StringFormat.cs
- SessionViewState.cs
- AppModelKnownContentFactory.cs
- SoapEnumAttribute.cs
- DefaultWorkflowLoaderService.cs
- HwndHostAutomationPeer.cs
- MachineKeyConverter.cs
- PriorityChain.cs
- ObjectViewListener.cs
- EdmEntityTypeAttribute.cs
- XmlBinaryReader.cs
- Viewport2DVisual3D.cs
- SiteMapSection.cs
- TdsEnums.cs
- IIS7UserPrincipal.cs
- DataControlFieldCollection.cs
- DataGridViewRowsAddedEventArgs.cs
- HierarchicalDataBoundControlAdapter.cs
- ProfileService.cs
- OleDbError.cs
- HelpProvider.cs
- EventListener.cs
- SchemaTypeEmitter.cs
- SafePEFileHandle.cs
- SqlParameterCollection.cs
- PerspectiveCamera.cs
- TraceProvider.cs
- ObjectPersistData.cs
- TextPointerBase.cs
- Journal.cs
- ObjectDataSourceMethodEventArgs.cs
- Soap.cs
- TickBar.cs
- ToolBarButtonClickEvent.cs
- EditorPartCollection.cs
- Quaternion.cs
- PropertyReferenceSerializer.cs
- CodeSnippetCompileUnit.cs
- FormViewPagerRow.cs
- ObjectSet.cs
- PenContext.cs
- SapiRecoContext.cs
- BitmapEncoder.cs
- GuidConverter.cs
- MaterialCollection.cs
- WindowsTokenRoleProvider.cs
- cookie.cs
- PenLineCapValidation.cs