Code:
/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / NetFx40 / System.Activities / System / Activities / Statements / ParallelForEach.cs / 1305376 / ParallelForEach.cs
//------------------------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- namespace System.Activities.Statements { using System.Activities; using System.Activities.Validation; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Windows.Markup; [ContentProperty("Body")] public sealed class ParallelForEach: NativeActivity { Variable hasCompleted; CompletionCallback onConditionComplete; public ParallelForEach() : base() { } [DefaultValue(null)] public ActivityAction Body { get; set; } [DefaultValue(null)] public Activity CompletionCondition { get; set; } [RequiredArgument] [DefaultValue(null)] public InArgument > Values { get; set; } protected override void CacheMetadata(NativeActivityMetadata metadata) { RuntimeArgument valuesArgument = new RuntimeArgument("Values", typeof(IEnumerable ), ArgumentDirection.In, true); metadata.Bind(this.Values, valuesArgument); metadata.SetArgumentsCollection(new Collection { valuesArgument }); // declare the CompletionCondition as a child if (this.CompletionCondition != null) { metadata.SetChildrenCollection(new Collection { this.CompletionCondition }); } // declare the hasCompleted variable if (this.CompletionCondition != null) { if (this.hasCompleted == null) { this.hasCompleted = new Variable (); } metadata.AddImplementationVariable(this.hasCompleted); } metadata.AddDelegate(this.Body); } protected override void Execute(NativeActivityContext context) { IEnumerable values = this.Values.Get(context); if (values == null) { throw FxTrace.Exception.AsError(new InvalidOperationException(SR.ParallelForEachRequiresNonNullValues(this.DisplayName))); } IEnumerator valueEnumerator = values.GetEnumerator(); CompletionCallback onBodyComplete = new CompletionCallback(OnBodyComplete); while (valueEnumerator.MoveNext()) { if (this.Body != null) { context.ScheduleAction(this.Body, valueEnumerator.Current, onBodyComplete); } } valueEnumerator.Dispose(); } void OnBodyComplete(NativeActivityContext context, ActivityInstance completedInstance) { // for the completion condition, we handle cancelation ourselves if (this.CompletionCondition != null && !this.hasCompleted.Get(context)) { if (completedInstance.State != ActivityInstanceState.Closed && context.IsCancellationRequested) { // If we hadn't completed before getting canceled // or one of our iteration of body cancels then we'll consider // ourself canceled. context.MarkCanceled(); this.hasCompleted.Set(context, true); } else { if (this.onConditionComplete == null) { this.onConditionComplete = new CompletionCallback (OnConditionComplete); } context.ScheduleActivity(CompletionCondition, this.onConditionComplete); } } } void OnConditionComplete(NativeActivityContext context, ActivityInstance completedInstance, bool result) { if (result) { context.CancelChildren(); this.hasCompleted.Set(context, true); } } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- SelectingProviderEventArgs.cs
- SourceSwitch.cs
- Int32CollectionConverter.cs
- SqlDataRecord.cs
- PageThemeBuildProvider.cs
- HttpAsyncResult.cs
- COSERVERINFO.cs
- Compiler.cs
- ComplexBindingPropertiesAttribute.cs
- Knowncolors.cs
- UnmanagedMemoryStreamWrapper.cs
- ProtocolViolationException.cs
- DispatcherSynchronizationContext.cs
- GridViewCancelEditEventArgs.cs
- DecoderFallback.cs
- HitTestWithGeometryDrawingContextWalker.cs
- ConsoleCancelEventArgs.cs
- AuthorizationPolicyTypeElementCollection.cs
- ConvertersCollection.cs
- RelatedEnd.cs
- HandledEventArgs.cs
- GlyphingCache.cs
- AuthorizationSection.cs
- VectorCollection.cs
- FilterEventArgs.cs
- CommandExpr.cs
- CompModSwitches.cs
- PaintValueEventArgs.cs
- ColorDialog.cs
- ValidatedControlConverter.cs
- ServiceModelActivationSectionGroup.cs
- AssociationTypeEmitter.cs
- hwndwrapper.cs
- GeneralTransform3DTo2D.cs
- EntityViewGenerationConstants.cs
- HtmlInputHidden.cs
- AppearanceEditorPart.cs
- TimeSpanMinutesConverter.cs
- ParserContext.cs
- BaseProcessor.cs
- SkewTransform.cs
- Frame.cs
- MetadataWorkspace.cs
- mediaeventshelper.cs
- PaginationProgressEventArgs.cs
- SqlDataReader.cs
- BindToObject.cs
- SQLInt32Storage.cs
- ClrPerspective.cs
- TextEditorDragDrop.cs
- StylusPointPropertyId.cs
- _DomainName.cs
- _ConnectionGroup.cs
- ClockController.cs
- HtmlTableRowCollection.cs
- EditingScope.cs
- HttpConfigurationContext.cs
- propertytag.cs
- XmlFormatWriterGenerator.cs
- EncryptedReference.cs
- _DynamicWinsockMethods.cs
- SponsorHelper.cs
- ItemCheckEvent.cs
- NegationPusher.cs
- VisualTreeUtils.cs
- WebPartManagerInternals.cs
- XmlCodeExporter.cs
- BuildProviderCollection.cs
- FtpWebResponse.cs
- MsmqIntegrationElement.cs
- QilUnary.cs
- BlurBitmapEffect.cs
- ReturnType.cs
- PinnedBufferMemoryStream.cs
- EpmCustomContentSerializer.cs
- UniqueIdentifierService.cs
- SystemWebExtensionsSectionGroup.cs
- Animatable.cs
- WindowsButton.cs
- CompoundFileDeflateTransform.cs
- VisualTreeUtils.cs
- DataGridPagerStyle.cs
- Enum.cs
- ValidationSummary.cs
- Vars.cs
- HttpListenerPrefixCollection.cs
- ContourSegment.cs
- ListViewInsertionMark.cs
- RichTextBox.cs
- EvidenceTypeDescriptor.cs
- ThaiBuddhistCalendar.cs
- SiteMapNodeItem.cs
- DropShadowEffect.cs
- WorkflowMessageEventHandler.cs
- NetPeerTcpBindingElement.cs
- BinaryOperationBinder.cs
- DateTimeConstantAttribute.cs
- UInt32Converter.cs
- AssemblyBuilder.cs
- ItemCheckedEvent.cs