Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / tx / System / Transactions / Oletx / HandleTable.cs / 1305376 / HandleTable.cs
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Transactions.Diagnostics;
namespace System.Transactions.Oletx
{
static class HandleTable
{
private static Dictionary handleTable = new Dictionary(256);
private static object syncRoot = new object();
private static int currentHandle;
public static IntPtr AllocHandle(object target)
{
lock(syncRoot)
{
int handle = FindAvailableHandle();
handleTable.Add(handle, target);
return new IntPtr(handle);
}
}
public static bool FreeHandle(IntPtr handle)
{
Debug.Assert(handle != IntPtr.Zero, "handle is invalid");
lock(syncRoot)
{
return handleTable.Remove(handle.ToInt32());
}
}
public static object FindHandle(IntPtr handle)
{
Debug.Assert(handle != IntPtr.Zero, "handle is invalid");
lock(syncRoot)
{
object target;
if (!handleTable.TryGetValue(handle.ToInt32(), out target))
{
return null;
}
return target;
}
}
private static int FindAvailableHandle()
{
int handle = 0;
do
{
handle = (++currentHandle != 0) ? currentHandle : ++currentHandle;
} while(handleTable.ContainsKey(handle));
Debug.Assert(handle != 0, "invalid handle selected");
return handle;
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Transactions.Diagnostics;
namespace System.Transactions.Oletx
{
static class HandleTable
{
private static Dictionary handleTable = new Dictionary(256);
private static object syncRoot = new object();
private static int currentHandle;
public static IntPtr AllocHandle(object target)
{
lock(syncRoot)
{
int handle = FindAvailableHandle();
handleTable.Add(handle, target);
return new IntPtr(handle);
}
}
public static bool FreeHandle(IntPtr handle)
{
Debug.Assert(handle != IntPtr.Zero, "handle is invalid");
lock(syncRoot)
{
return handleTable.Remove(handle.ToInt32());
}
}
public static object FindHandle(IntPtr handle)
{
Debug.Assert(handle != IntPtr.Zero, "handle is invalid");
lock(syncRoot)
{
object target;
if (!handleTable.TryGetValue(handle.ToInt32(), out target))
{
return null;
}
return target;
}
}
private static int FindAvailableHandle()
{
int handle = 0;
do
{
handle = (++currentHandle != 0) ? currentHandle : ++currentHandle;
} while(handleTable.ContainsKey(handle));
Debug.Assert(handle != 0, "invalid handle selected");
return 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
- DropDownButton.cs
- CounterSampleCalculator.cs
- DiscoveryMessageSequence.cs
- XXXInfos.cs
- RNGCryptoServiceProvider.cs
- ListView.cs
- ICspAsymmetricAlgorithm.cs
- InternalConfigRoot.cs
- IDReferencePropertyAttribute.cs
- WebZone.cs
- ObjectReferenceStack.cs
- SqlDataSourceAdvancedOptionsForm.cs
- MediaTimeline.cs
- WebColorConverter.cs
- BitVector32.cs
- BitmapEffect.cs
- ChannelManager.cs
- Set.cs
- Base64Encoding.cs
- RemoteWebConfigurationHost.cs
- SQLGuidStorage.cs
- SoapSchemaExporter.cs
- ThemeDirectoryCompiler.cs
- TextClipboardData.cs
- SQLResource.cs
- SafeNativeMethods.cs
- NullableIntSumAggregationOperator.cs
- ExpressionNormalizer.cs
- MouseGestureValueSerializer.cs
- TypeGeneratedEventArgs.cs
- FlowLayoutPanel.cs
- AssertHelper.cs
- Color.cs
- FormViewInsertEventArgs.cs
- GlyphElement.cs
- FullTextLine.cs
- ViewStateException.cs
- SessionPageStateSection.cs
- Selection.cs
- BindingWorker.cs
- ObservableDictionary.cs
- EntityContainerEmitter.cs
- ipaddressinformationcollection.cs
- MailDefinitionBodyFileNameEditor.cs
- SqlDataSource.cs
- SQLStringStorage.cs
- CodeTypeReferenceExpression.cs
- ListView.cs
- SQLDoubleStorage.cs
- IdentitySection.cs
- PositiveTimeSpanValidator.cs
- DataControlFieldCollection.cs
- LocalValueEnumerator.cs
- ProvidersHelper.cs
- DataAccessException.cs
- BulletedListEventArgs.cs
- PasswordTextContainer.cs
- WSSecurityPolicy.cs
- RawUIStateInputReport.cs
- MSG.cs
- EntityDataSourceContextCreatedEventArgs.cs
- JoinTreeNode.cs
- indexingfiltermarshaler.cs
- SvcMapFile.cs
- OrderToken.cs
- HttpRequestCacheValidator.cs
- MetadataProperty.cs
- UnsupportedPolicyOptionsException.cs
- DeferredElementTreeState.cs
- TransactionScope.cs
- NonParentingControl.cs
- SynchronizedInputAdaptor.cs
- TimeIntervalCollection.cs
- Perspective.cs
- MSHTMLHostUtil.cs
- PeerToPeerException.cs
- Int32Rect.cs
- BevelBitmapEffect.cs
- XmlProcessingInstruction.cs
- FilterRepeater.cs
- StorageMappingItemCollection.cs
- ZipIOBlockManager.cs
- VectorCollectionConverter.cs
- AuthenticationServiceManager.cs
- XmlComment.cs
- ToolStripKeyboardHandlingService.cs
- TypedReference.cs
- CanonicalFontFamilyReference.cs
- ProfileManager.cs
- SizeAnimationUsingKeyFrames.cs
- XmlTextEncoder.cs
- NativeMethods.cs
- KoreanLunisolarCalendar.cs
- FacetChecker.cs
- EntitySqlQueryCacheKey.cs
- MatrixStack.cs
- DefaultParameterValueAttribute.cs
- StylusPointDescription.cs
- PersonalizationEntry.cs
- MatrixCamera.cs