Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / xsp / System / Web / Util / ResourcePool.cs / 1305376 / ResourcePool.cs
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
namespace System.Web.Util {
using System.Collections;
using System.Threading;
/*
* ResourcePool provides a place to store expensive resources,
* such as network connections, that you want to dispose of when
* they are underused. A resource pool can be configured to timeout
* resources at a given interval, and to have a max limit of resources.
*/
class ResourcePool : IDisposable {
ArrayList _resources; // the resources
int _iDisposable; // resources below this index are candidates for disposal
int _max; // max number of resources
Timer _timer; // periodic timer
TimerCallback _callback; // callback delegate
TimeSpan _interval; // callback interval
bool _disposed;
internal ResourcePool(TimeSpan interval, int max) {
_interval = interval;
_resources = new ArrayList(4);
_max = max;
_callback = new TimerCallback(this.TimerProc);
Debug.Validate("ResourcePool", this);
}
~ResourcePool() {
Dispose(false);
}
public void Dispose() {
Dispose(true);
}
void Dispose(bool disposing) {
lock (this) {
if (!_disposed) {
if (_resources != null) {
foreach (IDisposable resource in _resources) {
resource.Dispose();
}
_resources.Clear();
}
if (_timer != null) {
_timer.Dispose();
}
Debug.Trace("ResourcePool", "Disposed");
_disposed = true;
// Suppress finalization of this disposed instance.
if (disposing)
{
GC.SuppressFinalize(this);
}
}
}
}
internal object RetrieveResource() {
object result = null;
// avoid lock in common case
if (_resources.Count != 0) {
lock (this) {
Debug.Validate("ResourcePool", this);
if (!_disposed) {
if (_resources.Count == 0) {
result = null;
Debug.Trace("ResourcePool", "RetrieveResource returned null");
} else {
result = _resources[_resources.Count-1];
_resources.RemoveAt(_resources.Count-1);
if (_resources.Count < _iDisposable) {
_iDisposable = _resources.Count;
}
}
Debug.Validate("ResourcePool", this);
}
}
}
return result;
}
internal void StoreResource(IDisposable o) {
lock (this) {
Debug.Validate("ResourcePool", this);
if (!_disposed) {
if (_resources.Count < _max) {
_resources.Add(o);
o = null;
if (_timer == null) {
#if DBG
if (!Debug.IsTagPresent("Timer") || Debug.IsTagEnabled("Timer"))
#endif
{
_timer = new Timer(_callback, null, _interval, _interval);
}
}
}
Debug.Validate("ResourcePool", this);
}
}
if (o != null) {
Debug.Trace("ResourcePool", "StoreResource reached max=" + _max);
o.Dispose();
}
}
void TimerProc(Object userData) {
IDisposable[] a = null;
lock (this) {
Debug.Validate("ResourcePool", this);
if (!_disposed) {
if (_resources.Count == 0) {
if (_timer != null) {
_timer.Dispose();
_timer = null;
}
Debug.Validate("ResourcePool", this);
return;
}
a = new IDisposable[_iDisposable];
_resources.CopyTo(0, a, 0, _iDisposable);
_resources.RemoveRange(0, _iDisposable);
// It means that whatever remain in _resources will be disposed
// next time the timer proc is called.
_iDisposable = _resources.Count;
Debug.Trace("ResourcePool", "Timer disposing " + a.Length + "; remaining=" + _resources.Count);
Debug.Validate("ResourcePool", this);
}
}
if (a != null) {
for (int i = 0; i < a.Length; i++) {
try {
a[i].Dispose();
}
catch {
// ignore all errors
}
}
}
}
#if DBG
internal void DebugValidate() {
Debug.CheckValid(_resources != null, "_resources != null");
Debug.CheckValid(0 <= _iDisposable && _iDisposable <= _resources.Count,
"0 <= _iDisposable && _iDisposable <= _resources.Count" +
";_iDisposable=" + _iDisposable +
";_resources.Count=" + _resources.Count);
Debug.CheckValid(_interval > TimeSpan.Zero, "_interval > TimeSpan.Zero" +
";_interval=" + _interval);
}
#endif
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
namespace System.Web.Util {
using System.Collections;
using System.Threading;
/*
* ResourcePool provides a place to store expensive resources,
* such as network connections, that you want to dispose of when
* they are underused. A resource pool can be configured to timeout
* resources at a given interval, and to have a max limit of resources.
*/
class ResourcePool : IDisposable {
ArrayList _resources; // the resources
int _iDisposable; // resources below this index are candidates for disposal
int _max; // max number of resources
Timer _timer; // periodic timer
TimerCallback _callback; // callback delegate
TimeSpan _interval; // callback interval
bool _disposed;
internal ResourcePool(TimeSpan interval, int max) {
_interval = interval;
_resources = new ArrayList(4);
_max = max;
_callback = new TimerCallback(this.TimerProc);
Debug.Validate("ResourcePool", this);
}
~ResourcePool() {
Dispose(false);
}
public void Dispose() {
Dispose(true);
}
void Dispose(bool disposing) {
lock (this) {
if (!_disposed) {
if (_resources != null) {
foreach (IDisposable resource in _resources) {
resource.Dispose();
}
_resources.Clear();
}
if (_timer != null) {
_timer.Dispose();
}
Debug.Trace("ResourcePool", "Disposed");
_disposed = true;
// Suppress finalization of this disposed instance.
if (disposing)
{
GC.SuppressFinalize(this);
}
}
}
}
internal object RetrieveResource() {
object result = null;
// avoid lock in common case
if (_resources.Count != 0) {
lock (this) {
Debug.Validate("ResourcePool", this);
if (!_disposed) {
if (_resources.Count == 0) {
result = null;
Debug.Trace("ResourcePool", "RetrieveResource returned null");
} else {
result = _resources[_resources.Count-1];
_resources.RemoveAt(_resources.Count-1);
if (_resources.Count < _iDisposable) {
_iDisposable = _resources.Count;
}
}
Debug.Validate("ResourcePool", this);
}
}
}
return result;
}
internal void StoreResource(IDisposable o) {
lock (this) {
Debug.Validate("ResourcePool", this);
if (!_disposed) {
if (_resources.Count < _max) {
_resources.Add(o);
o = null;
if (_timer == null) {
#if DBG
if (!Debug.IsTagPresent("Timer") || Debug.IsTagEnabled("Timer"))
#endif
{
_timer = new Timer(_callback, null, _interval, _interval);
}
}
}
Debug.Validate("ResourcePool", this);
}
}
if (o != null) {
Debug.Trace("ResourcePool", "StoreResource reached max=" + _max);
o.Dispose();
}
}
void TimerProc(Object userData) {
IDisposable[] a = null;
lock (this) {
Debug.Validate("ResourcePool", this);
if (!_disposed) {
if (_resources.Count == 0) {
if (_timer != null) {
_timer.Dispose();
_timer = null;
}
Debug.Validate("ResourcePool", this);
return;
}
a = new IDisposable[_iDisposable];
_resources.CopyTo(0, a, 0, _iDisposable);
_resources.RemoveRange(0, _iDisposable);
// It means that whatever remain in _resources will be disposed
// next time the timer proc is called.
_iDisposable = _resources.Count;
Debug.Trace("ResourcePool", "Timer disposing " + a.Length + "; remaining=" + _resources.Count);
Debug.Validate("ResourcePool", this);
}
}
if (a != null) {
for (int i = 0; i < a.Length; i++) {
try {
a[i].Dispose();
}
catch {
// ignore all errors
}
}
}
}
#if DBG
internal void DebugValidate() {
Debug.CheckValid(_resources != null, "_resources != null");
Debug.CheckValid(0 <= _iDisposable && _iDisposable <= _resources.Count,
"0 <= _iDisposable && _iDisposable <= _resources.Count" +
";_iDisposable=" + _iDisposable +
";_resources.Count=" + _resources.Count);
Debug.CheckValid(_interval > TimeSpan.Zero, "_interval > TimeSpan.Zero" +
";_interval=" + _interval);
}
#endif
}
}
// 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
- AutomationPropertyInfo.cs
- DynamicFilterExpression.cs
- FontClient.cs
- XmlAtomicValue.cs
- AddInAttribute.cs
- BitmapFrame.cs
- CompilerHelpers.cs
- XmlSchemaInferenceException.cs
- _IPv4Address.cs
- IntSumAggregationOperator.cs
- AttributeSetAction.cs
- _AuthenticationState.cs
- BrowserCapabilitiesCompiler.cs
- SelectionPatternIdentifiers.cs
- TypeInitializationException.cs
- Application.cs
- ToolStripPanelCell.cs
- HttpRawResponse.cs
- EllipseGeometry.cs
- IIS7WorkerRequest.cs
- ShutDownListener.cs
- PersonalizationDictionary.cs
- WebConfigurationFileMap.cs
- KeySplineConverter.cs
- CheckBoxField.cs
- DecoderFallbackWithFailureFlag.cs
- CollectionViewGroupRoot.cs
- ImpersonationContext.cs
- SvcMapFileSerializer.cs
- SecurityRuntime.cs
- InternalDispatchObject.cs
- Empty.cs
- CompositeKey.cs
- UnmanagedMemoryStreamWrapper.cs
- UiaCoreApi.cs
- XmlSchemaAttribute.cs
- XmlNodeReader.cs
- FileDialog.cs
- ReceiveCompletedEventArgs.cs
- RadioButtonAutomationPeer.cs
- KeyFrames.cs
- CodeChecksumPragma.cs
- MLangCodePageEncoding.cs
- AssemblyAssociatedContentFileAttribute.cs
- ConfigurationElementProperty.cs
- SettingsPropertyValueCollection.cs
- XPathNodeInfoAtom.cs
- ColumnWidthChangingEvent.cs
- SemaphoreSecurity.cs
- ArgumentNullException.cs
- ProjectedSlot.cs
- ADRole.cs
- EmptyReadOnlyDictionaryInternal.cs
- panel.cs
- DiagnosticsConfigurationHandler.cs
- DataErrorValidationRule.cs
- HtmlControlPersistable.cs
- UmAlQuraCalendar.cs
- PersistChildrenAttribute.cs
- EmptyStringExpandableObjectConverter.cs
- DataGridPagerStyle.cs
- DefaultAsyncDataDispatcher.cs
- Queue.cs
- CodeMethodReturnStatement.cs
- FontSource.cs
- SelectionItemPatternIdentifiers.cs
- ErrorWebPart.cs
- SplayTreeNode.cs
- DemultiplexingClientMessageFormatter.cs
- hresults.cs
- OleAutBinder.cs
- ItemDragEvent.cs
- RemotingServices.cs
- IEnumerable.cs
- EventLogRecord.cs
- ModuleElement.cs
- HttpWebRequestElement.cs
- DefaultAsyncDataDispatcher.cs
- ProviderBase.cs
- Line.cs
- EncoderFallback.cs
- HyperLinkStyle.cs
- TdsParserStaticMethods.cs
- BlobPersonalizationState.cs
- InstallerTypeAttribute.cs
- Stacktrace.cs
- DelegatingTypeDescriptionProvider.cs
- XmlSchemaException.cs
- EdmSchemaError.cs
- ContainerTracking.cs
- WCFServiceClientProxyGenerator.cs
- SimpleFileLog.cs
- VersionPair.cs
- Inflater.cs
- TextServicesPropertyRanges.cs
- Popup.cs
- PageEventArgs.cs
- MenuEventArgs.cs
- XmlArrayItemAttribute.cs
- FaultDescriptionCollection.cs