Code:
/ Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / whidbey / netfxsp / ndp / clr / src / BCL / System / Runtime / Remoting / TrackingServices.cs / 1 / TrackingServices.cs
// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
/*============================================================
**
** File: TrackingServices.cs
**
**
** Purpose: Defines the services for tracking lifetime and other
** operations on objects.
**
**
===========================================================*/
namespace System.Runtime.Remoting.Services {
using System.Security.Permissions;
using System;
using System.Threading;
using System.Globalization;
[System.Runtime.InteropServices.ComVisible(true)]
public interface ITrackingHandler
{
// Notify a handler that an object has been marshaled
[SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.Infrastructure)]
void MarshaledObject(Object obj, ObjRef or);
// Notify a handler that an object has been unmarshaled
[SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.Infrastructure)]
void UnmarshaledObject(Object obj, ObjRef or);
// Notify a handler that an object has been disconnected
[SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.Infrastructure)]
void DisconnectedObject(Object obj);
}
[SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.Infrastructure)]
[System.Runtime.InteropServices.ComVisible(true)]
public class TrackingServices
{
// Private member variables
private static ITrackingHandler[] _Handlers = new ITrackingHandler[0]; // Array of registered tracking handlers
private static int _Size = 0; // Number of elements in the array
private static Object s_TrackingServicesSyncObject = null;
private static Object TrackingServicesSyncObject
{
get
{
if (s_TrackingServicesSyncObject == null)
{
Object o = new Object();
Interlocked.CompareExchange(ref s_TrackingServicesSyncObject, o, null);
}
return s_TrackingServicesSyncObject;
}
}
public static void RegisterTrackingHandler(ITrackingHandler handler)
{
lock(TrackingServicesSyncObject)
{
// Validate arguments
if(null == handler)
{
throw new ArgumentNullException("handler");
}
// Check to make sure that the handler has not been registered
if(-1 == Match(handler))
{
// Allocate a new array if necessary
if((null == _Handlers) || (_Size == _Handlers.Length))
{
ITrackingHandler[] temp = new ITrackingHandler[_Size*2+4];
if(null != _Handlers)
{
Array.Copy(_Handlers, temp, _Size);
}
_Handlers = temp;
}
_Handlers[_Size++] = handler;
}
else
{
throw new RemotingException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Remoting_TrackingHandlerAlreadyRegistered"), "handler"));
}
}
}
public static void UnregisterTrackingHandler(ITrackingHandler handler)
{
lock(TrackingServicesSyncObject)
{
// Validate arguments
if(null == handler)
{
throw new ArgumentNullException("handler");
}
// Check to make sure that the channel has been registered
int matchingIdx = Match(handler);
if(-1 == matchingIdx)
{
throw new RemotingException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Remoting_HandlerNotRegistered"), handler));
}
// Delete the entry by copying the remaining entries
Array.Copy(_Handlers, matchingIdx+1, _Handlers, matchingIdx, _Size-matchingIdx-1);
_Size--;
}
}
public static ITrackingHandler[] RegisteredHandlers
{
get
{
lock(TrackingServicesSyncObject)
{
if(0 == _Size)
{
return new ITrackingHandler[0];
}
else
{
// Copy the array of registered handlers into a new array
// and return
ITrackingHandler[] temp = new ITrackingHandler[_Size];
for(int i = 0; i < _Size; i++)
{
temp[i] = _Handlers[i];
}
return temp;
}
}
}
}
// Notify all the handlers that an object has been marshaled
internal static void MarshaledObject(Object obj, ObjRef or)
{
try{
ITrackingHandler[] temp = _Handlers;
for(int i = 0; i < _Size; i++)
{
temp[i].MarshaledObject(obj, or);
}
}
catch {}
}
// Notify all the handlers that an object has been unmarshaled
internal static void UnmarshaledObject(Object obj, ObjRef or)
{
try{
ITrackingHandler[] temp = _Handlers;
for(int i = 0; i < _Size; i++)
{
temp[i].UnmarshaledObject(obj, or);
}
}
catch {}
}
// Notify all the handlers that an object has been disconnected
internal static void DisconnectedObject(Object obj)
{
try{
ITrackingHandler[] temp = _Handlers;
for(int i = 0; i < _Size; i++)
{
temp[i].DisconnectedObject(obj);
}
}
catch {}
}
private static int Match(ITrackingHandler handler)
{
int idx = -1;
for(int i = 0; i < _Size; i++)
{
if(_Handlers[i] == handler)
{
idx = i;
break;
}
}
return idx;
}
}
} // namespace
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
/*============================================================
**
** File: TrackingServices.cs
**
**
** Purpose: Defines the services for tracking lifetime and other
** operations on objects.
**
**
===========================================================*/
namespace System.Runtime.Remoting.Services {
using System.Security.Permissions;
using System;
using System.Threading;
using System.Globalization;
[System.Runtime.InteropServices.ComVisible(true)]
public interface ITrackingHandler
{
// Notify a handler that an object has been marshaled
[SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.Infrastructure)]
void MarshaledObject(Object obj, ObjRef or);
// Notify a handler that an object has been unmarshaled
[SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.Infrastructure)]
void UnmarshaledObject(Object obj, ObjRef or);
// Notify a handler that an object has been disconnected
[SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.Infrastructure)]
void DisconnectedObject(Object obj);
}
[SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.Infrastructure)]
[System.Runtime.InteropServices.ComVisible(true)]
public class TrackingServices
{
// Private member variables
private static ITrackingHandler[] _Handlers = new ITrackingHandler[0]; // Array of registered tracking handlers
private static int _Size = 0; // Number of elements in the array
private static Object s_TrackingServicesSyncObject = null;
private static Object TrackingServicesSyncObject
{
get
{
if (s_TrackingServicesSyncObject == null)
{
Object o = new Object();
Interlocked.CompareExchange(ref s_TrackingServicesSyncObject, o, null);
}
return s_TrackingServicesSyncObject;
}
}
public static void RegisterTrackingHandler(ITrackingHandler handler)
{
lock(TrackingServicesSyncObject)
{
// Validate arguments
if(null == handler)
{
throw new ArgumentNullException("handler");
}
// Check to make sure that the handler has not been registered
if(-1 == Match(handler))
{
// Allocate a new array if necessary
if((null == _Handlers) || (_Size == _Handlers.Length))
{
ITrackingHandler[] temp = new ITrackingHandler[_Size*2+4];
if(null != _Handlers)
{
Array.Copy(_Handlers, temp, _Size);
}
_Handlers = temp;
}
_Handlers[_Size++] = handler;
}
else
{
throw new RemotingException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Remoting_TrackingHandlerAlreadyRegistered"), "handler"));
}
}
}
public static void UnregisterTrackingHandler(ITrackingHandler handler)
{
lock(TrackingServicesSyncObject)
{
// Validate arguments
if(null == handler)
{
throw new ArgumentNullException("handler");
}
// Check to make sure that the channel has been registered
int matchingIdx = Match(handler);
if(-1 == matchingIdx)
{
throw new RemotingException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Remoting_HandlerNotRegistered"), handler));
}
// Delete the entry by copying the remaining entries
Array.Copy(_Handlers, matchingIdx+1, _Handlers, matchingIdx, _Size-matchingIdx-1);
_Size--;
}
}
public static ITrackingHandler[] RegisteredHandlers
{
get
{
lock(TrackingServicesSyncObject)
{
if(0 == _Size)
{
return new ITrackingHandler[0];
}
else
{
// Copy the array of registered handlers into a new array
// and return
ITrackingHandler[] temp = new ITrackingHandler[_Size];
for(int i = 0; i < _Size; i++)
{
temp[i] = _Handlers[i];
}
return temp;
}
}
}
}
// Notify all the handlers that an object has been marshaled
internal static void MarshaledObject(Object obj, ObjRef or)
{
try{
ITrackingHandler[] temp = _Handlers;
for(int i = 0; i < _Size; i++)
{
temp[i].MarshaledObject(obj, or);
}
}
catch {}
}
// Notify all the handlers that an object has been unmarshaled
internal static void UnmarshaledObject(Object obj, ObjRef or)
{
try{
ITrackingHandler[] temp = _Handlers;
for(int i = 0; i < _Size; i++)
{
temp[i].UnmarshaledObject(obj, or);
}
}
catch {}
}
// Notify all the handlers that an object has been disconnected
internal static void DisconnectedObject(Object obj)
{
try{
ITrackingHandler[] temp = _Handlers;
for(int i = 0; i < _Size; i++)
{
temp[i].DisconnectedObject(obj);
}
}
catch {}
}
private static int Match(ITrackingHandler handler)
{
int idx = -1;
for(int i = 0; i < _Size; i++)
{
if(_Handlers[i] == handler)
{
idx = i;
break;
}
}
return idx;
}
}
} // namespace
// 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
- CancellationHandlerDesigner.cs
- UIHelper.cs
- PropertyGrid.cs
- nulltextcontainer.cs
- BinaryFormatterSinks.cs
- DetailsViewPageEventArgs.cs
- ITreeGenerator.cs
- BinaryMethodMessage.cs
- XomlSerializationHelpers.cs
- ActivitySurrogate.cs
- ToolStripComboBox.cs
- DSASignatureDeformatter.cs
- LinqDataSourceDeleteEventArgs.cs
- TranslateTransform.cs
- XamlSerializerUtil.cs
- XMLUtil.cs
- AppSettingsReader.cs
- CapabilitiesPattern.cs
- Panel.cs
- ClassDataContract.cs
- IDReferencePropertyAttribute.cs
- ExpandCollapsePattern.cs
- DefaultAuthorizationContext.cs
- MessageQueueConverter.cs
- Region.cs
- CodeLabeledStatement.cs
- ScriptControlManager.cs
- ResourceType.cs
- RestHandler.cs
- SelectionHighlightInfo.cs
- Int64KeyFrameCollection.cs
- SizeChangedInfo.cs
- TextEditorContextMenu.cs
- ResourceType.cs
- ListBoxItemWrapperAutomationPeer.cs
- XmlText.cs
- TimeoutTimer.cs
- WebBrowserBase.cs
- Animatable.cs
- UniformGrid.cs
- ProfileService.cs
- ColumnMapProcessor.cs
- StringAnimationBase.cs
- BeginStoryboard.cs
- WebPartEditorOkVerb.cs
- XmlSchemaAll.cs
- UInt64.cs
- SelectionItemPattern.cs
- TypeReference.cs
- x509store.cs
- _StreamFramer.cs
- StylusDevice.cs
- SqlInternalConnectionTds.cs
- DeviceContexts.cs
- COAUTHINFO.cs
- ItemCollection.cs
- QuadraticBezierSegment.cs
- ExtensionQuery.cs
- PolicyStatement.cs
- TextServicesDisplayAttributePropertyRanges.cs
- JsonFormatGeneratorStatics.cs
- BitmapSizeOptions.cs
- SmuggledIUnknown.cs
- ToolStripDropDownClosingEventArgs.cs
- HashAlgorithm.cs
- KeyConverter.cs
- _ScatterGatherBuffers.cs
- XPathAncestorIterator.cs
- DomainLiteralReader.cs
- ZipFileInfo.cs
- XmlSchemaComplexType.cs
- LayoutTable.cs
- WebHeaderCollection.cs
- Graphics.cs
- Renderer.cs
- StoreAnnotationsMap.cs
- WebServiceHost.cs
- GridViewItemAutomationPeer.cs
- InlineCollection.cs
- ComboBoxRenderer.cs
- FormsAuthenticationUserCollection.cs
- AuthenticationManager.cs
- GridItemProviderWrapper.cs
- SQLDateTime.cs
- ReachDocumentReferenceCollectionSerializer.cs
- SyndicationElementExtensionCollection.cs
- DbParameterCollectionHelper.cs
- OleAutBinder.cs
- SessionEndedEventArgs.cs
- DecoderBestFitFallback.cs
- PagedDataSource.cs
- Parser.cs
- ObjectViewListener.cs
- BuildProviderCollection.cs
- ProfileSettings.cs
- WebPartDisplayMode.cs
- DocumentProperties.cs
- XmlDictionaryWriter.cs
- DataServiceQueryContinuation.cs
- EpmTargetPathSegment.cs