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
- QueryOperator.cs
- ListViewInsertEventArgs.cs
- MetadataArtifactLoaderXmlReaderWrapper.cs
- ThreadStaticAttribute.cs
- RenderCapability.cs
- ConfigurationSectionGroup.cs
- WebServiceClientProxyGenerator.cs
- DependencyPropertyValueSerializer.cs
- CallbackValidatorAttribute.cs
- JsonQNameDataContract.cs
- Stylesheet.cs
- PolygonHotSpot.cs
- CodePageEncoding.cs
- Array.cs
- StringArrayConverter.cs
- Line.cs
- WindowAutomationPeer.cs
- UIServiceHelper.cs
- TimelineClockCollection.cs
- SignedInfo.cs
- WebPartCollection.cs
- CollectionConverter.cs
- Debug.cs
- MessageSecurityVersionConverter.cs
- XmlSchemaExternal.cs
- AdPostCacheSubstitution.cs
- CookieParameter.cs
- SiteMapNodeItem.cs
- ColorKeyFrameCollection.cs
- SoapExtension.cs
- SpoolingTaskBase.cs
- DataKeyArray.cs
- TableItemProviderWrapper.cs
- RoutedEventValueSerializer.cs
- DiagnosticTraceSource.cs
- SiteOfOriginContainer.cs
- ipaddressinformationcollection.cs
- TextMetrics.cs
- ListViewGroupItemCollection.cs
- SmiSettersStream.cs
- Symbol.cs
- AspNetSynchronizationContext.cs
- ActivityWithResult.cs
- PartialTrustVisibleAssembliesSection.cs
- BaseParagraph.cs
- ValueSerializerAttribute.cs
- AddInPipelineAttributes.cs
- XamlToRtfWriter.cs
- ObjectQueryProvider.cs
- RouteItem.cs
- MultiPageTextView.cs
- MetadataPropertyCollection.cs
- IdnElement.cs
- MemberDomainMap.cs
- DescendantQuery.cs
- TypeElement.cs
- SHA1CryptoServiceProvider.cs
- FixedSOMTable.cs
- XmlChildEnumerator.cs
- MTConfigUtil.cs
- Block.cs
- CheckBoxBaseAdapter.cs
- HTMLTextWriter.cs
- DataExpression.cs
- LinqDataSourceSelectEventArgs.cs
- CommandEventArgs.cs
- WebPartTransformer.cs
- AnimationClockResource.cs
- StyleSelector.cs
- ResolveNameEventArgs.cs
- ZeroOpNode.cs
- XhtmlMobileTextWriter.cs
- AccessedThroughPropertyAttribute.cs
- FileLevelControlBuilderAttribute.cs
- CapabilitiesState.cs
- SecurityChannelFaultConverter.cs
- SR.cs
- XmlDataSourceNodeDescriptor.cs
- DrawingAttributes.cs
- NativeMethods.cs
- OdbcDataAdapter.cs
- StateMachineExecutionState.cs
- TagMapCollection.cs
- PersonalizablePropertyEntry.cs
- Simplifier.cs
- NullRuntimeConfig.cs
- PointCollectionConverter.cs
- Drawing.cs
- TableCellCollection.cs
- AttachedPropertiesService.cs
- BooleanStorage.cs
- ObjectDataSource.cs
- Size.cs
- MLangCodePageEncoding.cs
- PieceDirectory.cs
- PageParser.cs
- HtmlTableCell.cs
- DataGridViewAccessibleObject.cs
- UnmanagedHandle.cs
- DockAndAnchorLayout.cs