Code:
/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / Orcas / NetFXw7 / wpf / src / Framework / MS / Internal / Data / AsyncDataRequest.cs / 1 / AsyncDataRequest.cs
//----------------------------------------------------------------------------
//
//
// Copyright (C) Microsoft Corporation. All rights reserved.
//
//
// Description: Defines a request to the async data system.
//
// Specs: http://avalon/connecteddata/M5%20Specs/Asynchronous%20Data%20Model.mht
//
//---------------------------------------------------------------------------
using System;
namespace MS.Internal.Data
{
/// Type for the work and completion delegates of an AsyncDataRequest
internal delegate object AsyncRequestCallback(AsyncDataRequest request);
/// Status of an async data request.
internal enum AsyncRequestStatus
{
/// Request has not been started
Waiting,
/// Request is in progress
Working,
/// Request has been completed
Completed,
/// Request was cancelled
Cancelled,
/// Request failed
Failed
}
/// A request to the async data system.
internal class AsyncDataRequest
{
//-----------------------------------------------------
//
// Constructors
//
//-----------------------------------------------------
/// Constructor
internal AsyncDataRequest( object bindingState,
AsyncRequestCallback workCallback,
AsyncRequestCallback completedCallback,
params object[] args
)
{
_bindingState = bindingState;
_workCallback = workCallback;
_completedCallback = completedCallback;
_args = args;
}
//------------------------------------------------------
//
// Public Properties
//
//-----------------------------------------------------
/* unused by default scheduler. Restore for custom schedulers.
/// The "user data" from the binding that issued the request.
public object BindingState { get { return _bindingState; } }
*/
/// The result of the request (valid when request is completed).
public object Result { get { return _result; } }
/// The status of the request.
public AsyncRequestStatus Status { get { return _status; } }
/// The exception (for a failed request).
public Exception Exception { get { return _exception; } }
//------------------------------------------------------
//
// Public Methods
//
//------------------------------------------------------
/// Run the request's work delegate and return the result.
///
/// This method should be called synchronously on a worker thread, as it
/// calls the work delegate, which potentially takes a long time. The
/// method sets the status to "Working". It is normally followed by a
/// call to Complete.
///
/// If the request has already been run or has been abandoned, this method
/// returns null.
///
public object DoWork()
{
if (DoBeginWork() && _workCallback != null)
return _workCallback(this);
else
return null;
}
/// If the request is in the "Waiting" state, return true and
/// set its status to "Working". Otherwise return false.
///
///
/// This method is thread-safe and works atomically. Therefore only
/// one thread will be permitted to run the request.
///
public bool DoBeginWork()
{
return ChangeStatus(AsyncRequestStatus.Working);
}
/// Set the request's status to "Completed", save the result,
/// and call the completed delegate.
///
/// This method should be called on any thread, after
/// either calling DoWork or performing the work for a request in some
/// other way.
///
/// If the request has already been run or has been abandoned, this method
/// does nothing.
///
public void Complete(object result)
{
if (ChangeStatus(AsyncRequestStatus.Completed))
{
_result = result;
if (_completedCallback != null)
_completedCallback(this);
}
}
/// Cancel the request.
/// This method can be called from any thread.
/// Calling Cancel does not actually terminate the work being
/// done on behalf of the request, but merely causes the result
/// of that work to be ignored.
///
public void Cancel()
{
ChangeStatus(AsyncRequestStatus.Cancelled);
}
/// Fail the request because of an exception.
/// This method can be called from any thread.
public void Fail(Exception exception)
{
if (ChangeStatus(AsyncRequestStatus.Failed))
{
_exception = exception;
if (_completedCallback != null)
_completedCallback(this);
}
}
//-----------------------------------------------------
//
// Internal properties
//
//------------------------------------------------------
/// The caller-defined arguments.
internal object[] Args { get { return _args; } }
//-----------------------------------------------------
//
// Private methods
//
//-----------------------------------------------------
// Change the status to the new status. Return true if this is allowed.
// Do it all atomically.
bool ChangeStatus(AsyncRequestStatus newStatus)
{
bool allowChange = false;
lock(SyncRoot)
{
switch (newStatus)
{
case AsyncRequestStatus.Working:
allowChange = (_status == AsyncRequestStatus.Waiting);
break;
case AsyncRequestStatus.Completed:
allowChange = (_status == AsyncRequestStatus.Working);
break;
case AsyncRequestStatus.Cancelled:
allowChange = (_status == AsyncRequestStatus.Waiting) ||
(_status == AsyncRequestStatus.Working);
break;
case AsyncRequestStatus.Failed:
allowChange = (_status == AsyncRequestStatus.Working);
break;
}
if (allowChange)
_status = newStatus;;
}
return allowChange;
}
//-----------------------------------------------------
//
// Private data
//
//------------------------------------------------------
AsyncRequestStatus _status;
object _result;
object _bindingState;
object[] _args;
Exception _exception;
AsyncRequestCallback _workCallback;
AsyncRequestCallback _completedCallback;
object SyncRoot = new object(); // for synchronization
}
/// Async request to get the value of a property on an item.
internal class AsyncGetValueRequest : AsyncDataRequest
{
//-----------------------------------------------------
//
// Constructors
//
//------------------------------------------------------
/// Constructor.
internal AsyncGetValueRequest( object item,
string propertyName,
object bindingState,
AsyncRequestCallback workCallback,
AsyncRequestCallback completedCallback,
params object[] args
)
: base(bindingState, workCallback, completedCallback, args)
{
_item = item;
_propertyName = propertyName;
}
//------------------------------------------------------
//
// Public Properties
//
//-----------------------------------------------------
/// The item whose property is being requested
public object SourceItem { get { return _item; } }
/* unused by default scheduler. Restore for custom schedulers.
/// The name of the property being requested
public string PropertyName { get { return _propertyName; } }
*/
//------------------------------------------------------
//
// Private data
//
//-----------------------------------------------------
object _item;
string _propertyName;
}
/// Async request to set the value of a property on an item.
internal class AsyncSetValueRequest : AsyncDataRequest
{
//-----------------------------------------------------
//
// Constructors
//
//-----------------------------------------------------
/// Constructor.
internal AsyncSetValueRequest( object item,
string propertyName,
object value,
object bindingState,
AsyncRequestCallback workCallback,
AsyncRequestCallback completedCallback,
params object[] args
)
: base(bindingState, workCallback, completedCallback, args)
{
_item = item;
_propertyName = propertyName;
_value = value;
}
//------------------------------------------------------
//
// Public Properties
//
//-----------------------------------------------------
/// The item whose property is being set
public object TargetItem { get { return _item; } }
/* unused by default scheduler. Restore for custom schedulers.
/// The name of the property being set
public string PropertyName { get { return _propertyName; } }
*/
/// The new value for the property
public object Value { get { return _value; } }
//------------------------------------------------------
//
// Private data
//
//------------------------------------------------------
object _item;
string _propertyName;
object _value;
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
//----------------------------------------------------------------------------
//
//
// Copyright (C) Microsoft Corporation. All rights reserved.
//
//
// Description: Defines a request to the async data system.
//
// Specs: http://avalon/connecteddata/M5%20Specs/Asynchronous%20Data%20Model.mht
//
//---------------------------------------------------------------------------
using System;
namespace MS.Internal.Data
{
/// Type for the work and completion delegates of an AsyncDataRequest
internal delegate object AsyncRequestCallback(AsyncDataRequest request);
/// Status of an async data request.
internal enum AsyncRequestStatus
{
/// Request has not been started
Waiting,
/// Request is in progress
Working,
/// Request has been completed
Completed,
/// Request was cancelled
Cancelled,
/// Request failed
Failed
}
/// A request to the async data system.
internal class AsyncDataRequest
{
//-----------------------------------------------------
//
// Constructors
//
//-----------------------------------------------------
/// Constructor
internal AsyncDataRequest( object bindingState,
AsyncRequestCallback workCallback,
AsyncRequestCallback completedCallback,
params object[] args
)
{
_bindingState = bindingState;
_workCallback = workCallback;
_completedCallback = completedCallback;
_args = args;
}
//------------------------------------------------------
//
// Public Properties
//
//-----------------------------------------------------
/* unused by default scheduler. Restore for custom schedulers.
/// The "user data" from the binding that issued the request.
public object BindingState { get { return _bindingState; } }
*/
/// The result of the request (valid when request is completed).
public object Result { get { return _result; } }
/// The status of the request.
public AsyncRequestStatus Status { get { return _status; } }
/// The exception (for a failed request).
public Exception Exception { get { return _exception; } }
//------------------------------------------------------
//
// Public Methods
//
//------------------------------------------------------
/// Run the request's work delegate and return the result.
///
/// This method should be called synchronously on a worker thread, as it
/// calls the work delegate, which potentially takes a long time. The
/// method sets the status to "Working". It is normally followed by a
/// call to Complete.
///
/// If the request has already been run or has been abandoned, this method
/// returns null.
///
public object DoWork()
{
if (DoBeginWork() && _workCallback != null)
return _workCallback(this);
else
return null;
}
/// If the request is in the "Waiting" state, return true and
/// set its status to "Working". Otherwise return false.
///
///
/// This method is thread-safe and works atomically. Therefore only
/// one thread will be permitted to run the request.
///
public bool DoBeginWork()
{
return ChangeStatus(AsyncRequestStatus.Working);
}
/// Set the request's status to "Completed", save the result,
/// and call the completed delegate.
///
/// This method should be called on any thread, after
/// either calling DoWork or performing the work for a request in some
/// other way.
///
/// If the request has already been run or has been abandoned, this method
/// does nothing.
///
public void Complete(object result)
{
if (ChangeStatus(AsyncRequestStatus.Completed))
{
_result = result;
if (_completedCallback != null)
_completedCallback(this);
}
}
/// Cancel the request.
/// This method can be called from any thread.
/// Calling Cancel does not actually terminate the work being
/// done on behalf of the request, but merely causes the result
/// of that work to be ignored.
///
public void Cancel()
{
ChangeStatus(AsyncRequestStatus.Cancelled);
}
/// Fail the request because of an exception.
/// This method can be called from any thread.
public void Fail(Exception exception)
{
if (ChangeStatus(AsyncRequestStatus.Failed))
{
_exception = exception;
if (_completedCallback != null)
_completedCallback(this);
}
}
//-----------------------------------------------------
//
// Internal properties
//
//------------------------------------------------------
/// The caller-defined arguments.
internal object[] Args { get { return _args; } }
//-----------------------------------------------------
//
// Private methods
//
//-----------------------------------------------------
// Change the status to the new status. Return true if this is allowed.
// Do it all atomically.
bool ChangeStatus(AsyncRequestStatus newStatus)
{
bool allowChange = false;
lock(SyncRoot)
{
switch (newStatus)
{
case AsyncRequestStatus.Working:
allowChange = (_status == AsyncRequestStatus.Waiting);
break;
case AsyncRequestStatus.Completed:
allowChange = (_status == AsyncRequestStatus.Working);
break;
case AsyncRequestStatus.Cancelled:
allowChange = (_status == AsyncRequestStatus.Waiting) ||
(_status == AsyncRequestStatus.Working);
break;
case AsyncRequestStatus.Failed:
allowChange = (_status == AsyncRequestStatus.Working);
break;
}
if (allowChange)
_status = newStatus;;
}
return allowChange;
}
//-----------------------------------------------------
//
// Private data
//
//------------------------------------------------------
AsyncRequestStatus _status;
object _result;
object _bindingState;
object[] _args;
Exception _exception;
AsyncRequestCallback _workCallback;
AsyncRequestCallback _completedCallback;
object SyncRoot = new object(); // for synchronization
}
/// Async request to get the value of a property on an item.
internal class AsyncGetValueRequest : AsyncDataRequest
{
//-----------------------------------------------------
//
// Constructors
//
//------------------------------------------------------
/// Constructor.
internal AsyncGetValueRequest( object item,
string propertyName,
object bindingState,
AsyncRequestCallback workCallback,
AsyncRequestCallback completedCallback,
params object[] args
)
: base(bindingState, workCallback, completedCallback, args)
{
_item = item;
_propertyName = propertyName;
}
//------------------------------------------------------
//
// Public Properties
//
//-----------------------------------------------------
/// The item whose property is being requested
public object SourceItem { get { return _item; } }
/* unused by default scheduler. Restore for custom schedulers.
/// The name of the property being requested
public string PropertyName { get { return _propertyName; } }
*/
//------------------------------------------------------
//
// Private data
//
//-----------------------------------------------------
object _item;
string _propertyName;
}
/// Async request to set the value of a property on an item.
internal class AsyncSetValueRequest : AsyncDataRequest
{
//-----------------------------------------------------
//
// Constructors
//
//-----------------------------------------------------
/// Constructor.
internal AsyncSetValueRequest( object item,
string propertyName,
object value,
object bindingState,
AsyncRequestCallback workCallback,
AsyncRequestCallback completedCallback,
params object[] args
)
: base(bindingState, workCallback, completedCallback, args)
{
_item = item;
_propertyName = propertyName;
_value = value;
}
//------------------------------------------------------
//
// Public Properties
//
//-----------------------------------------------------
/// The item whose property is being set
public object TargetItem { get { return _item; } }
/* unused by default scheduler. Restore for custom schedulers.
/// The name of the property being set
public string PropertyName { get { return _propertyName; } }
*/
/// The new value for the property
public object Value { get { return _value; } }
//------------------------------------------------------
//
// Private data
//
//------------------------------------------------------
object _item;
string _propertyName;
object _value;
}
}
// 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
- FullTextState.cs
- InputMethod.cs
- Aggregates.cs
- XmlReturnWriter.cs
- EdmTypeAttribute.cs
- ConsumerConnectionPointCollection.cs
- AllMembershipCondition.cs
- CommunicationObjectManager.cs
- TableMethodGenerator.cs
- ExternalException.cs
- CounterCreationDataCollection.cs
- RoleServiceManager.cs
- HtmlMeta.cs
- CreateUserWizard.cs
- WeakReference.cs
- ValueOfAction.cs
- _HTTPDateParse.cs
- InkSerializer.cs
- MsmqIntegrationChannelFactory.cs
- HostProtectionPermission.cs
- DebugView.cs
- DataTableReaderListener.cs
- EventWaitHandleSecurity.cs
- JobInputBins.cs
- xmlfixedPageInfo.cs
- SizeAnimationClockResource.cs
- PhysicalAddress.cs
- AttachedProperty.cs
- JsonQNameDataContract.cs
- ControlCollection.cs
- Deserializer.cs
- IWorkflowDebuggerService.cs
- GenericTypeParameterBuilder.cs
- SessionStateContainer.cs
- EditorZone.cs
- DesignerActionVerbList.cs
- HttpValueCollection.cs
- Cursors.cs
- securestring.cs
- TableAutomationPeer.cs
- PropertyConverter.cs
- ColorInterpolationModeValidation.cs
- PlanCompiler.cs
- AmbientLight.cs
- XpsColorContext.cs
- TextContainerChangeEventArgs.cs
- WSAddressing10ProblemHeaderQNameFault.cs
- IgnoreFileBuildProvider.cs
- AmbiguousMatchException.cs
- ComboBox.cs
- EnvelopedPkcs7.cs
- ChangeDirector.cs
- ConnectionManagementSection.cs
- PeerEndPoint.cs
- ControlAdapter.cs
- CommonXSendMessage.cs
- XmlBoundElement.cs
- XmlNodeChangedEventArgs.cs
- TaiwanLunisolarCalendar.cs
- ProjectionRewriter.cs
- TextTreeInsertUndoUnit.cs
- MethodRental.cs
- DataSourceConverter.cs
- TimeoutValidationAttribute.cs
- KeyGestureConverter.cs
- ControlValuePropertyAttribute.cs
- HandlerFactoryCache.cs
- RegexFCD.cs
- IdleTimeoutMonitor.cs
- CharacterMetrics.cs
- DropDownList.cs
- SafeNativeMemoryHandle.cs
- DataServiceProviderMethods.cs
- XhtmlBasicPageAdapter.cs
- MaskDescriptors.cs
- ConsoleKeyInfo.cs
- XmlWellformedWriter.cs
- ConnectionStringsSection.cs
- JsonUriDataContract.cs
- IncrementalHitTester.cs
- UpdateProgress.cs
- XmlSchemaCollection.cs
- ProgressBar.cs
- TraceEventCache.cs
- InvokeSchedule.cs
- SBCSCodePageEncoding.cs
- ReadOnlyDataSourceView.cs
- ZipQueryOperator.cs
- SecUtil.cs
- followingquery.cs
- WebBodyFormatMessageProperty.cs
- NameSpaceEvent.cs
- RootCodeDomSerializer.cs
- ExpressionsCollectionEditor.cs
- RegexCompilationInfo.cs
- ImageIndexConverter.cs
- DesignerDataStoredProcedure.cs
- XmlAnyElementAttribute.cs
- UrlPath.cs
- ParseElement.cs