Code:
/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / infocard / Service / managed / Microsoft / InfoCards / UIAgentAsyncBeginRequest.cs / 1 / UIAgentAsyncBeginRequest.cs
//------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
//
// Presharp uses the c# pragma mechanism to supress its warnings.
// These are not recognised by the base compiler so we need to explictly
// disable the following warnings. See http://winweb/cse/Tools/PREsharp/userguide/default.asp
// for details.
//
#pragma warning disable 1634, 1691 // unknown message, unknown pragma
namespace Microsoft.InfoCards
{
using Microsoft.Win32.SafeHandles;
using System;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.IO;
using Microsoft.InfoCards.Diagnostics;
using IDT = Microsoft.InfoCards.Diagnostics.InfoCardTrace;
//
// Summary:
// Base class for begining any async operations from the UI Agent.
//
internal abstract class UIAgentAsyncBeginRequest : UIAgentRequest
{
SafeWaitHandle m_agentEventHandle;
int m_asyncHandle;
//
// Summary:
// Creates an new UIAgentAsyncBeginRequest
//
// Arguments:
// rpcHandle: The RPC Context handle.
// inArgs: The stream to hold the input arguments
// outArgs: The stream to hold the output arguments
// parent: the parent UI Request
//
public UIAgentAsyncBeginRequest(
IntPtr rpcHandle,
Stream inArgs,
Stream outArgs,
ClientUIRequest parent )
: base( rpcHandle, inArgs, outArgs, parent )
{
}
//
// Summary:
// Reads incoming params.
//
// Remarks:
// Reads the wait handle, and clones it for this process.
//
protected sealed override void OnMarshalInArgs()
{
IntPtr nativeEvent = IntPtr.Zero;
BinaryReader breader = new InfoCardBinaryReader( InArgs, Encoding.Unicode );
nativeEvent = Utility.ReadHandle( breader );
//
// Then translate the remote event handle into a locally valid handle.
//
using( SafeWaitHandle waitHandle = new SafeWaitHandle( nativeEvent, false ) )
{
m_agentEventHandle = Utility.GetLocalHandleFromRemoteHandle(
waitHandle,
(int) ParentRequest.UIAgentPid );
}
OnMarshalAsyncInArgs( breader );
}
//
// Summary:
// Called after the standard async args are already marsheled to allow derived classes to marshal op
// specific arguments.
//
protected virtual void OnMarshalAsyncInArgs( BinaryReader reader )
{
//
// Default is to do nothing.
//
}
//
// Summary:
// Process the Begin operateration.
//
protected override void OnProcess()
{
AsyncParams param = CreateAsyncParams();
param.AsyncResult.CancelCallback = new RpcAsyncResult.AsyncCancelCallback( AsyncCancel );
IDT.Assert( null != param, "Null AsyncParams" );
try
{
//
// Capture the async context handle.
//
m_asyncHandle = param.AsyncResult.Handle;
IDT.Assert( 0 != m_asyncHandle, "null async context handle" );
//
// Start the operation.
//
if (!ThreadPool.QueueUserWorkItem(IDT.ThunkCallback(new WaitCallback(AsyncEntry)), param))
{
throw IDT.ThrowHelperError( new CommunicationException( SR.GetString( SR.UnableToQueueThreadpool ) ) );
}
}
catch
{
//
// An error occured, be sure to dispose the async context.
// Should AsyncParams support dispose?
//
param.AsyncResult.Dispose();
throw;
}
}
//
// Summary:
// marshal the base async info out.
//
protected override void OnMarshalOutArgs()
{
BinaryWriter bwriter = new BinaryWriter( OutArgs, Encoding.Unicode );
IDT.Assert( 0 != m_asyncHandle, "null async handle" );
bwriter.Write( m_asyncHandle );
}
protected override void OnDisposeAsUser()
{
if( null != m_agentEventHandle )
{
m_agentEventHandle.Dispose();
m_agentEventHandle = null;
}
}
//
// Summary:
// Creates the AsyncParams for the async operation.
//
// Remarks:
// By default, a UIAgentAsyncParams object is created, but derived classes
// can change this by returning any AsyncParams derived class.
//
protected virtual AsyncParams CreateAsyncParams( )
{
//
// Get the name of this request type. We put this in the asyncParams to make debugging easier.
//
Type thisType = GetType();
AsyncParams retParams = new UIAgentAsyncParams( thisType.Name,
ParentRequest,
m_agentEventHandle,
this );
//
// The asyncParams takes ownership of the handle.
//
m_agentEventHandle = null;
return retParams;
}
//
// Summary:
// Abstract entry point for the execution of the async operation.
//
protected abstract object AsyncExecute( AsyncParams asyncParam );
//
// Summary:
// Abstract entry point for the cancelation of the async operation.
//
protected abstract void AsyncCancel( );
//
// Summary:
// This is a WaitCallback conformant wrapper for Async Requests
//
// Parameters:
// state - An instance of a a AsyncParams class..
//
// Remarks:
// Running on threadpool thread.
//
private void AsyncEntry( object state )
{
//
// Consider for future -- see bug 38733 -- Clear out thread context here (in a finally block)
//
AsyncParams param = (AsyncParams)state;
try
{
if( param.AsyncResult.IsCanceled )
{
param.AsyncResult.Exception = new Win32Exception( NativeMethods.ERROR_CANCELLED );
}
else
{
param.AsyncResult.Result = AsyncExecute( param );
}
}
#pragma warning disable 56500 // do not catch non-recoverable exceptions
catch( Exception e )
{
if( IDT.IsFatal( e ) )
{
InfoCardService.Crash( e );
}
//
// Ensure that the result is completed.
//
//
// Even if it is InfoCardBaseException, we're simply transferring it.
//
param.AsyncResult.Exception = e;
}
#pragma warning restore 56500
//
// The complete method should not throw any exceptions.
//
param.AsyncResult.Complete();
//
// Consider for future -- see bug 38733 -- Clear out thread context here (in a finally block)
//
}
}
}
// 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
- Propagator.Evaluator.cs
- PeekCompletedEventArgs.cs
- ContextMenuAutomationPeer.cs
- TdsRecordBufferSetter.cs
- TargetControlTypeAttribute.cs
- ExecutionProperties.cs
- ChannelServices.cs
- ArithmeticException.cs
- MetafileHeaderWmf.cs
- ActivationArguments.cs
- _emptywebproxy.cs
- TextFormatterHost.cs
- FrameworkContentElementAutomationPeer.cs
- ReflectionServiceProvider.cs
- DynamicEntity.cs
- OdbcConnectionString.cs
- ItemCheckedEvent.cs
- UrlPropertyAttribute.cs
- ObjectListItem.cs
- BinaryConverter.cs
- BackEase.cs
- TcpClientCredentialType.cs
- CombinedGeometry.cs
- LinkedResource.cs
- ObjectDisposedException.cs
- ArgumentException.cs
- RectAnimationUsingKeyFrames.cs
- SharedTcpTransportManager.cs
- ClientUtils.cs
- MDIWindowDialog.cs
- UMPAttributes.cs
- InheritanceContextHelper.cs
- AdditionalEntityFunctions.cs
- MetadataPropertyCollection.cs
- InputMethodStateChangeEventArgs.cs
- SchemaContext.cs
- AssemblyBuilder.cs
- FacetChecker.cs
- ProtocolsConfigurationHandler.cs
- PointAnimationUsingPath.cs
- PeerResolverElement.cs
- Token.cs
- ReferentialConstraint.cs
- DoubleAnimationClockResource.cs
- Metafile.cs
- CacheSection.cs
- SQLInt64Storage.cs
- ComplexPropertyEntry.cs
- ReadOnlyHierarchicalDataSourceView.cs
- ListBindableAttribute.cs
- UrlMappingsModule.cs
- DynamicRendererThreadManager.cs
- SmtpSection.cs
- TransferMode.cs
- AnnotationAuthorChangedEventArgs.cs
- TypeLibConverter.cs
- XMLSyntaxException.cs
- DiagnosticsConfiguration.cs
- InputProviderSite.cs
- BackStopAuthenticationModule.cs
- WaitHandle.cs
- HostedHttpContext.cs
- TrackingDataItemValue.cs
- ToolStripDesignerAvailabilityAttribute.cs
- NativeCppClassAttribute.cs
- PackWebRequestFactory.cs
- RelationalExpressions.cs
- ClassData.cs
- CompilerCollection.cs
- EventLogLink.cs
- AesManaged.cs
- Drawing.cs
- GetLedgerEntryForRecipientRequest.cs
- DiscoveryClientProtocol.cs
- nulltextcontainer.cs
- MetaModel.cs
- TemplateControl.cs
- XmlSyndicationContent.cs
- LoadedOrUnloadedOperation.cs
- XmlSchemaAttribute.cs
- AutoGeneratedFieldProperties.cs
- RichTextBox.cs
- PageBuildProvider.cs
- LayoutEditorPart.cs
- KeyValueConfigurationElement.cs
- PerformanceCounterPermission.cs
- DataListItem.cs
- UIntPtr.cs
- NetworkInterface.cs
- Array.cs
- _FtpControlStream.cs
- Margins.cs
- LoginNameDesigner.cs
- _NestedSingleAsyncResult.cs
- DynamicDataRouteHandler.cs
- ToolBarButton.cs
- HierarchicalDataSourceControl.cs
- StringValidatorAttribute.cs
- DesigntimeLicenseContextSerializer.cs
- AlternateViewCollection.cs