Code:
/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / infocard / Service / managed / Microsoft / InfoCards / RpcAsyncResult.cs / 1 / RpcAsyncResult.cs
//------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
namespace Microsoft.InfoCards
{
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Globalization;
using System.Threading;
using Microsoft.Win32.SafeHandles;
using IDT=Microsoft.InfoCards.Diagnostics.InfoCardTrace;
//
// Summary:
// This class is responsible for keeping track of the state of an
// asynchronous ui agent rpc.
//
// Remarks:
// An RpcAysncResult instance is created for each incoming ui agent
// asynchronous RPC request. When the request either completes or is
// cancelled the AsyncWaitHandle property is set. If a native SafeWaitHandle
// corresponding to an Event is passed in, then that is also set.
//
// The thread started to service the asynchronous request should periodically
// check the IsCanceled property on the RpcAsyncResult and immediately clean
// up, complete the request and return if it is true.
//
internal class RpcAsyncResult : IAsyncResult, IDisposable
{
//
// public delegate type exposed for cancelation callbacks.
//
public delegate void AsyncCancelCallback();
class RpcAsyncHandleDictionary : HandleDictionary< RpcAsyncResult > {}
static RpcAsyncHandleDictionary s_handles = new RpcAsyncHandleDictionary();
static object s_syncRoot = new Object();
ClientRequest m_request; // The request that this async op is associated with
ManualResetEvent m_externEvent; // A handle to native event to signal when the operation completes.
bool m_isCompleted;
bool m_isCanceled;
bool m_isResultPickedUp;
bool m_isDisposed;
object m_result;
string m_opType;
ManualResetEvent m_event;
int m_handle; // A unique id that clients use to reference this operation.
object m_syncRoot;
Exception m_exception; // Any exceptions that occur on the worker thread are stored here.
AsyncCancelCallback m_cancelCallback;
//
// Summary:
// Constructs a new RpcAsyncResult.
//
// Parameters:
// request - The request this operation is associated with.
// opType - The kind of operation that this is.
// exdternEvent - A native event handle so be signalled when the operation is complete.
//
public RpcAsyncResult( ClientRequest request, string opType, SafeWaitHandle externEvent )
{
IDT.ThrowInvalidArgumentConditional( null == request, "request" );
m_syncRoot = new Object();
m_request = request;
m_opType = opType;
if( null != externEvent )
{
m_externEvent = new ManualResetEvent( false );
m_externEvent.SafeWaitHandle = externEvent;
}
m_event = new ManualResetEvent( false );
//
// Pick a handle value that is not in use yet.
//
lock( s_syncRoot )
{
int handle = 0;
try
{
handle = s_handles.GetNewHandle();
}
catch( IndexOutOfRangeException e )
{
throw IDT.ThrowHelperError( new ServiceBusyException( SR.GetString( SR.ServiceTooManyAsyncOperations ,s_handles.MaxSize ), e ) );
}
m_handle = handle;
s_handles[ m_handle ] = this;
}
m_request.AddAsyncOp( this );
}
public AsyncCancelCallback CancelCallback
{
get{ return m_cancelCallback; }
set{ m_cancelCallback = value; }
}
public WaitHandle AsyncWaitHandle
{
get { return m_event; }
}
public bool CompletedSynchronously
{
get { return false; }
}
public object AsyncState
{
get { return null; }
}
//
// Summary:
// Holds the result of the async operation.
//
// Remarks:
// This should be an operation specific object type corresponding to the
// operation that this RpcAsyncResult is for.
//
public object Result
{
get
{
IDT.DebugAssert( m_isCompleted || m_isCanceled, "result is neither completed nor cancelled" );
return m_result;
}
set
{
m_result = value;
}
}
public bool IsCompleted
{
get { return m_isCompleted; }
}
public bool IsCanceled
{
get { return m_isCanceled; }
}
public int Handle
{
get { return m_handle; }
}
public Exception Exception
{
get { return m_exception; }
set { m_exception = value; }
}
//
// Summary:
// Should be called when the thread working on the async op has completed
// processing. Should be called even if the thread completes due to
// cancellation. Causes disposal if the async client has already indicated that it is through with the
// results.
//
public void Complete()
{
lock( m_syncRoot )
{
IDT.DebugAssert( !m_isCompleted, "result is already completed" );
if( !m_isCompleted )
{
m_isCompleted = true;
if( !m_isCanceled )
{
AlertWaiters();
}
MaybeDispose();
}
}
return;
}
public void Cancel()
{
lock( m_syncRoot )
{
InternalCancel();
}
}
private void InternalCancel()
{
if( m_isCanceled || m_isCompleted )
{
return;
}
if( null != CancelCallback )
{
CancelCallback();
}
m_isCanceled = true;
AlertWaiters();
}
private void AlertWaiters()
{
m_event.Set();
if( null != m_externEvent )
{
m_externEvent.Set();
}
}
//
// This must be called with m_syncRoot locked.
//
private void MaybeDispose()
{
if( m_isCompleted && m_isResultPickedUp )
{
DisposeInternal();
}
}
//
// Summary:
// Indicates that the async client is done with the request. Causes disposal if the async op has been
// completed already. Otherwise this object will be disposed when the async op has completed.
public void Dispose()
{
if ( m_isResultPickedUp )
{
return;
}
lock( m_syncRoot )
{
InternalCancel();
m_isResultPickedUp = true;
MaybeDispose();
}
}
private void DisposeInternal()
{
//
// At this point the m_syncRoot lock is held
// by the caller of MaybeDispose
//
if( !m_isDisposed )
{
m_isDisposed = true;
m_request.RemoveAsyncOp( this.Handle );
m_request = null;
if( null != m_externEvent )
{
m_externEvent.Close();
}
m_event.Close();
lock( s_syncRoot )
{
s_handles.Remove( this.Handle );
}
}
}
}
}
// 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
- SystemIPInterfaceStatistics.cs
- ObjectViewQueryResultData.cs
- WebPartRestoreVerb.cs
- CodeCommentStatementCollection.cs
- ProcessInfo.cs
- WebPartAuthorizationEventArgs.cs
- EntityDataSourceContextDisposingEventArgs.cs
- AttributeUsageAttribute.cs
- SizeConverter.cs
- BlurEffect.cs
- OleDbError.cs
- ClientWindowsAuthenticationMembershipProvider.cs
- xmlsaver.cs
- KeyNotFoundException.cs
- DesignerAutoFormatCollection.cs
- ColumnResizeAdorner.cs
- SmiMetaDataProperty.cs
- CodeMethodInvokeExpression.cs
- FileDialog_Vista.cs
- BindingExpressionUncommonField.cs
- ThemeableAttribute.cs
- StateManagedCollection.cs
- BehaviorEditorPart.cs
- HyperLink.cs
- HttpBufferlessInputStream.cs
- ModelItemKeyValuePair.cs
- SoapSchemaMember.cs
- EntityAdapter.cs
- PackageDigitalSignature.cs
- Switch.cs
- ToolStripDropDown.cs
- NativeMethodsCLR.cs
- BigInt.cs
- EmptyStringExpandableObjectConverter.cs
- IOThreadScheduler.cs
- Line.cs
- RowToFieldTransformer.cs
- SqlProfileProvider.cs
- QueryAccessibilityHelpEvent.cs
- CompiledQueryCacheKey.cs
- EditCommandColumn.cs
- AcceptorSessionSymmetricTransportSecurityProtocol.cs
- ListViewDeleteEventArgs.cs
- RuntimeComponentFilter.cs
- DocumentPageHost.cs
- WorkflowApplicationAbortedException.cs
- RoleManagerEventArgs.cs
- Model3D.cs
- CustomExpression.cs
- ZeroOpNode.cs
- Ray3DHitTestResult.cs
- HttpListenerTimeoutManager.cs
- FixedLineResult.cs
- EntityDataSourceReferenceGroup.cs
- DispatchChannelSink.cs
- SendingRequestEventArgs.cs
- ProtectedProviderSettings.cs
- CurrentTimeZone.cs
- DataGridViewAutoSizeColumnModeEventArgs.cs
- _BufferOffsetSize.cs
- DragEventArgs.cs
- ConnectionModeReader.cs
- MailAddress.cs
- DataStreams.cs
- InProcStateClientManager.cs
- MediaContextNotificationWindow.cs
- WebPartMinimizeVerb.cs
- WebPart.cs
- SetStoryboardSpeedRatio.cs
- MiniMapControl.xaml.cs
- UIElement3D.cs
- EntityContainerEntitySet.cs
- FileDialog_Vista.cs
- Wildcard.cs
- SoapProcessingBehavior.cs
- PaintEvent.cs
- EncodingNLS.cs
- IgnoreSectionHandler.cs
- Point3DCollectionConverter.cs
- ObjectHandle.cs
- ScriptReferenceEventArgs.cs
- DataTableCollection.cs
- AsyncOperation.cs
- XmlSerializerAssemblyAttribute.cs
- ExportOptions.cs
- ChannelEndpointElement.cs
- DataContractSerializer.cs
- SettingsPropertyCollection.cs
- AsymmetricSignatureDeformatter.cs
- GeneralTransform3DTo2DTo3D.cs
- DefaultSettingsSection.cs
- VisualStyleTypesAndProperties.cs
- DataBinding.cs
- NamedPipeTransportBindingElement.cs
- TextBox.cs
- XmlAnyElementAttribute.cs
- ApplicationId.cs
- Guid.cs
- ItemsPanelTemplate.cs
- GetLastErrorDetailsRequest.cs