Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / Core / System / Linq / Parallel / Scheduling / SpoolingTaskBase.cs / 1305376 / SpoolingTaskBase.cs
// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
// =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
//
// SpoolingTaskBase.cs
//
// [....]
//
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
using System.Collections.Generic;
using System.Threading;
using System.Diagnostics.Contracts;
namespace System.Linq.Parallel
{
///
/// A spooling task handles marshaling data from a producer to a consumer. It simply
/// takes data from a producer and hands it off to a consumer. This class is the base
/// class from which other concrete spooling tasks derive, encapsulating some common
/// logic (such as capturing exceptions).
///
internal abstract class SpoolingTaskBase : QueryTask
{
//------------------------------------------------------------------------------------
// Constructs a new spooling task.
//
// Arguments:
// taskIndex - the unique index of this task
//
protected SpoolingTaskBase(int taskIndex, QueryTaskGroupState groupState) :
base(taskIndex, groupState)
{
}
//-----------------------------------------------------------------------------------
// The implementation of the Work API just enumerates the producer's data, and
// enqueues it into the consumer channel. Well, really, it just defers to extension
// points (below) implemented by subclasses to do these things.
//
protected override void Work()
{
try
{
// Defer to the base class for the actual work.
SpoolingWork();
}
catch (Exception ex)
{
OperationCanceledException oce = ex as OperationCanceledException;
if (oce != null &&
oce.CancellationToken == m_groupState.CancellationState.MergedCancellationToken
&& m_groupState.CancellationState.MergedCancellationToken.IsCancellationRequested)
{
//an expected internal cancellation has occurred. suppress this exception.
}
else
{
// TPL will catch and store the exception on the task object. We'll then later
// turn around and wait on it, having the effect of propagating it. In the meantime,
// we want to cooperative cancel all workers.
m_groupState.CancellationState.InternalCancellationTokenSource.Cancel();
// And then repropagate to let TPL catch it.
throw;
}
}
finally
{
SpoolingFinally(); //dispose resources etc.
}
}
//-----------------------------------------------------------------------------------
// This method is responsible for enumerating results and enqueueing them to
// the output channel(s) as appropriate. Each base class implements its own.
//
protected abstract void SpoolingWork();
//-----------------------------------------------------------------------------------
// If the subclass needs to do something in the finally block of the main work loop,
// it should override this and do it. Purely optional.
//
protected virtual void SpoolingFinally()
{
// (Intentionally left blank.)
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
// =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
//
// SpoolingTaskBase.cs
//
// [....]
//
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
using System.Collections.Generic;
using System.Threading;
using System.Diagnostics.Contracts;
namespace System.Linq.Parallel
{
///
/// A spooling task handles marshaling data from a producer to a consumer. It simply
/// takes data from a producer and hands it off to a consumer. This class is the base
/// class from which other concrete spooling tasks derive, encapsulating some common
/// logic (such as capturing exceptions).
///
internal abstract class SpoolingTaskBase : QueryTask
{
//------------------------------------------------------------------------------------
// Constructs a new spooling task.
//
// Arguments:
// taskIndex - the unique index of this task
//
protected SpoolingTaskBase(int taskIndex, QueryTaskGroupState groupState) :
base(taskIndex, groupState)
{
}
//-----------------------------------------------------------------------------------
// The implementation of the Work API just enumerates the producer's data, and
// enqueues it into the consumer channel. Well, really, it just defers to extension
// points (below) implemented by subclasses to do these things.
//
protected override void Work()
{
try
{
// Defer to the base class for the actual work.
SpoolingWork();
}
catch (Exception ex)
{
OperationCanceledException oce = ex as OperationCanceledException;
if (oce != null &&
oce.CancellationToken == m_groupState.CancellationState.MergedCancellationToken
&& m_groupState.CancellationState.MergedCancellationToken.IsCancellationRequested)
{
//an expected internal cancellation has occurred. suppress this exception.
}
else
{
// TPL will catch and store the exception on the task object. We'll then later
// turn around and wait on it, having the effect of propagating it. In the meantime,
// we want to cooperative cancel all workers.
m_groupState.CancellationState.InternalCancellationTokenSource.Cancel();
// And then repropagate to let TPL catch it.
throw;
}
}
finally
{
SpoolingFinally(); //dispose resources etc.
}
}
//-----------------------------------------------------------------------------------
// This method is responsible for enumerating results and enqueueing them to
// the output channel(s) as appropriate. Each base class implements its own.
//
protected abstract void SpoolingWork();
//-----------------------------------------------------------------------------------
// If the subclass needs to do something in the finally block of the main work loop,
// it should override this and do it. Purely optional.
//
protected virtual void SpoolingFinally()
{
// (Intentionally left blank.)
}
}
}
// 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
- UnsafeNativeMethods.cs
- ResolveCriteria11.cs
- WebServiceParameterData.cs
- SecurityCapabilities.cs
- WebPartHelpVerb.cs
- DirectoryObjectSecurity.cs
- PreProcessor.cs
- DrawTreeNodeEventArgs.cs
- DataBindingExpressionBuilder.cs
- BitmapCache.cs
- AnimationTimeline.cs
- ApplicationFileParser.cs
- SecurityPolicySection.cs
- TextEndOfSegment.cs
- DataGridViewToolTip.cs
- SchemaTableOptionalColumn.cs
- FixedPageProcessor.cs
- SpeechSynthesizer.cs
- SqlDataSourceFilteringEventArgs.cs
- SetStoryboardSpeedRatio.cs
- webbrowsersite.cs
- ComPlusInstanceProvider.cs
- CoTaskMemHandle.cs
- OleDbMetaDataFactory.cs
- __Error.cs
- AxisAngleRotation3D.cs
- ExpandSegment.cs
- SemanticValue.cs
- SiteMembershipCondition.cs
- Color.cs
- GridPatternIdentifiers.cs
- ThreadPool.cs
- ManagementObjectCollection.cs
- Int16KeyFrameCollection.cs
- URLEditor.cs
- StoreUtilities.cs
- Soap12ProtocolImporter.cs
- KnownColorTable.cs
- BehaviorEditorPart.cs
- LiteralControl.cs
- PasswordTextContainer.cs
- DesignerResources.cs
- PropertyGrid.cs
- HtmlElementEventArgs.cs
- ToolStripScrollButton.cs
- RangeBase.cs
- ConvertEvent.cs
- CompositionDesigner.cs
- XmlBindingWorker.cs
- PasswordRecoveryDesigner.cs
- NewExpression.cs
- PermissionToken.cs
- SignatureDescription.cs
- Win32MouseDevice.cs
- XmlAttributeAttribute.cs
- SqlErrorCollection.cs
- SourceLocationProvider.cs
- FlatButtonAppearance.cs
- DetailsViewUpdateEventArgs.cs
- IERequestCache.cs
- _SslState.cs
- DescendantOverDescendantQuery.cs
- DocumentViewerHelper.cs
- SettingsPropertyValue.cs
- GridViewHeaderRowPresenter.cs
- SimpleType.cs
- DataContractFormatAttribute.cs
- ProfileProvider.cs
- C14NUtil.cs
- IPHostEntry.cs
- ReadOnlyNameValueCollection.cs
- IPAddressCollection.cs
- XmlDocumentFragment.cs
- FormViewDeletedEventArgs.cs
- FlowDocumentView.cs
- TraceUtils.cs
- ReferencedAssembly.cs
- LocationFactory.cs
- Logging.cs
- Block.cs
- IdentityReference.cs
- SqlWriter.cs
- PeerCredentialElement.cs
- StrokeFIndices.cs
- FilterFactory.cs
- Selector.cs
- EdmPropertyAttribute.cs
- NameService.cs
- Lease.cs
- PropertyDescriptorCollection.cs
- GlobalAllocSafeHandle.cs
- SafePEFileHandle.cs
- DataSourceHelper.cs
- RadialGradientBrush.cs
- InputDevice.cs
- ContextStaticAttribute.cs
- InternalCache.cs
- PrintingPermission.cs
- ToolBarButton.cs
- BinaryCommonClasses.cs