Home
Discussion Forums
Tutorials & Source Code
Developer Blogs
Developer Events
Developer Jobs
Labs
We need you!
We're working hard on the next version of Developer Fusion -
Let us know
what you think we should be up to!
The Labs
\
Source Viewer
\
SSCLI
\
System.Runtime.InteropServices
\ HandleCollector
Object Browser
Solution Explorer
SSCLI
Expand
Microsoft.Win32
Expand
Microsoft.Win32.SafeHandles
Expand
System
Expand
System.Runtime.Hosting
Expand
System.Runtime.InteropServices
Expand
_Activator
Expand
_Assembly
Expand
_AssemblyBuilder
Expand
_AssemblyName
Expand
_Attribute
Expand
_ConstructorBuilder
Expand
_ConstructorInfo
Expand
_CustomAttributeBuilder
Expand
_EnumBuilder
Expand
_EventBuilder
Expand
_EventInfo
Expand
_Exception
Expand
_FieldBuilder
Expand
_FieldInfo
Expand
_ILGenerator
Expand
_LocalBuilder
Expand
_MemberInfo
Expand
_MethodBase
Expand
_MethodBuilder
Expand
_MethodInfo
Expand
_MethodRental
Expand
_Module
Expand
_ModuleBuilder
Expand
_ParameterBuilder
Expand
_ParameterInfo
Expand
_PropertyBuilder
Expand
_PropertyInfo
Expand
_SignatureHelper
Expand
_Thread
Expand
_Type
Expand
_TypeBuilder
Expand
ArrayWithOffset
Expand
BestFitMappingAttribute
Expand
CallingConvention
Expand
CharSet
Expand
ClassInterfaceAttribute
Expand
ClassInterfaceType
Expand
ComDefaultInterfaceAttribute
Expand
COMException
Expand
ComImportAttribute
Expand
ComInterfaceType
Expand
ComVisibleAttribute
Expand
CriticalHandle
Expand
DefaultCharSetAttribute
Expand
DefaultParameterValueAttribute
Expand
DispIdAttribute
Expand
DllImportAttribute
Expand
ExternalException
Expand
FieldOffsetAttribute
Expand
GCHandle
Expand
GCHandleType
Expand
GuidAttribute
Expand
HandleCollector
Add()
Count()
HandleCollector(string, int)
HandleCollector(string, int, int)
InitialThreshold()
MaximumThreshold()
Name()
Remove()
Expand
HandleRef
Expand
ICustomMarshaler
Expand
InAttribute
Expand
InterfaceTypeAttribute
Expand
InvalidOleVariantTypeException
Expand
LayoutKind
Expand
Marshal
Expand
MarshalAsAttribute
Expand
MarshalDirectiveException
Expand
OptionalAttribute
Expand
OutAttribute
Expand
PInvokeMap
Expand
PreserveSigAttribute
Expand
RuntimeEnvironment
Expand
SafeHandle
Expand
SEHException
Expand
SetWin32ContextInIDispatchAttribute
Expand
StructLayoutAttribute
Expand
TypeLibImportClassAttribute
Expand
UnmanagedFunctionPointerAttribute
Expand
UnmanagedType
Expand
VarEnum
Expand
System.Runtime.CompilerServices
Expand
System.Collections
Expand
System.Collections.Generic
Expand
System.Collections.ObjectModel
Expand
System.Configuration.Assemblies
Expand
System.Diagnostics
Expand
System.Diagnostics.CodeAnalysis
Expand
System.Diagnostics.SymbolStore
Expand
System.Globalization
Expand
System.IO
Expand
System.IO.IsolatedStorage
Expand
System.Reflection
Expand
System.Reflection.Cache
Expand
System.Reflection.Emit
Expand
System.Resources
Expand
System.Runtime
Expand
System.Runtime.InteropServices.Expando
Expand
System.Runtime.ConstrainedExecution
Expand
System.Runtime.Remoting.Activation
Expand
System.Runtime.Remoting.Messaging
Expand
System.Runtime.Remoting.Channels
Expand
System.Runtime.Remoting.Lifetime
Expand
System.Runtime.Remoting
Expand
System.Runtime.Remoting.Contexts
Expand
System.Runtime.Remoting.Proxies
Expand
System.Runtime.Remoting.Metadata
Expand
System.Runtime.Remoting.Metadata.W3cXsd2001
Expand
System.Runtime.Remoting.Services
Expand
System.Runtime.Serialization
Expand
System.Runtime.Serialization.Formatters
Expand
System.Runtime.Serialization.Formatters.Binary
Expand
System.Runtime.Versioning
Expand
System.Security
Expand
System.Security.Cryptography
Expand
System.Security.Cryptography.X509Certificates
Expand
System.Security.Permissions
Expand
System.Security.Policy
Expand
System.Security.Principal
Expand
System.Security.Util
Expand
System.Text
Expand
System.Threading
Expand
System.Runtime.Remoting.Channels.Http
Expand
System.Runtime.Remoting.Channels.Tcp
Expand
System.Runtime.Remoting.MetadataServices
Expand
System.Runtime.Serialization.Formatters.Soap.Xml
Expand
System.Runtime.Serialization.Formatters.Soap
Expand
Microsoft.Tools.Caspol
Expand
Microsoft.Tools.Build
Expand
Microsoft.CSharp
Expand
System.CodeDom
Expand
System.CodeDom.Compiler
Expand
System.Collections.Specialized
Expand
System.ComponentModel
Expand
System.ComponentModel.Design
Expand
System.ComponentModel.Design.Serialization
Expand
System.Configuration
Expand
System.Web
Expand
System.Configuration.Provider
Expand
System.Configuration.Internal
Expand
System.Internal
Expand
System.Windows.Forms
Expand
System.Net
Expand
System.Net.Sockets
Expand
System.Net.NetworkInformation
Expand
System.Net.ComImports
Expand
System.Net.Security
Expand
System.Net.Cache
Expand
System.Net.Configuration
Expand
System.Text.RegularExpressions
Expand
System.Tools
Expand
System.IO.Compression
Expand
System.IO.Ports
Expand
System.Security.AccessControl
Expand
System.Xml
Expand
System.Xml.XPath.DataBinding
Expand
System.Xml.Schema
Expand
System.Xml.Serialization
Expand
System.Xml.Serialization.Advanced
Expand
System.Xml.Serialization.Configuration
Expand
System.Xml.XPath
Expand
System.Xml.Xsl
Expand
System.Xml.Xsl.IlGen
Expand
System.Xml.Xsl.Qil
Expand
System.Xml.Xsl.Runtime
Expand
System.Xml.Xsl.XPath
Expand
System.Xml.Xsl.Xslt
Expand
System.Xml.Xsl.XsltOld
Expand
System.Xml.Xsl.XsltOld.Debugger
Expand
Microsoft.JScript
Expand
Microsoft.Vsa
Expand
Microsoft.JScript.Vsa
C#
VB.NET
// ==++==
//
//
// Copyright (c) 2006 Microsoft Corporation. All rights reserved.
//
// The use and distribution terms for this software are contained in the file
// named license.txt, which can be found in the root of this distribution.
// By using this software in any fashion, you are agreeing to be bound by the
// terms of this license.
//
// You must not remove this notice, or any other, from this software.
//
//
// ==--==
namespace
System.Runtime.InteropServices
{
using
System
;
using
System.Threading
;
public
sealed
class
HandleCollector
{
private
const
int
deltaPercent = 10;
// this is used for increasing the threshold.
private
string
name;
private
int
initialThreshold;
private
int
maximumThreshold;
private
int
threshold;
private
int
handleCount;
private
int
[] gc_counts =
new
int
[3];
private
int
gc_gen = 0;
public
HandleCollector(
string
name,
int
initialThreshold) :
this
(name, initialThreshold,
int
.MaxValue)
{
}
public
HandleCollector(
string
name,
int
initialThreshold,
int
maximumThreshold)
{
if
(initialThreshold < 0) {
throw
new
ArgumentOutOfRangeException
("initialThreshold", SR.GetString(SR.ArgumentOutOfRange_NeedNonNegNumRequired));
}
if
(maximumThreshold < 0) {
throw
new
ArgumentOutOfRangeException
("maximumThreshold", SR.GetString(SR.ArgumentOutOfRange_NeedNonNegNumRequired));
}
if
(initialThreshold > maximumThreshold) {
throw
new
ArgumentException
(SR.GetString(SR.Argument_InvalidThreshold));
}
if
(name !=
null
) {
this
.name = name;
}
else
{
this
.name = String.Empty;
}
this
.initialThreshold = initialThreshold;
this
.maximumThreshold = maximumThreshold;
this
.threshold = initialThreshold;
this
.handleCount = 0;
}
public
int
Count {
get {
return
handleCount; }
}
public
int
InitialThreshold {
get {
return
initialThreshold; }
}
public
int
MaximumThreshold {
get {
return
maximumThreshold; }
}
public
string
Name {
get {
return
name; }
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2001:AvoidCallingProblematicMethods")]
// Keep call to GC.Collect()
public
void
Add()
{
int
gen_collect = -1;
Interlocked.Increment(
ref
handleCount);
if
(handleCount < 0) {
throw
new
InvalidOperationException
(SR.GetString(SR.InvalidOperation_HCCountOverflow));
}
if
(handleCount > threshold) {
lock
(
this
) {
threshold = handleCount + (handleCount / deltaPercent);
gen_collect = gc_gen;
if
(gc_gen < 2) {
gc_gen++;
}
}
}
if
((gen_collect >= 0) && ((gen_collect == 0) || (gc_counts[gen_collect] == GC.CollectionCount(gen_collect)))) {
GC.Collect(gen_collect);
Thread.Sleep(10 * gen_collect);
}
//don't bother with gen0.
for
(
int
i = 1; i < 3; i++) {
gc_counts[i] = GC.CollectionCount(i);
}
}
public
void
Remove()
{
Interlocked.Decrement(
ref
handleCount);
if
(handleCount < 0) {
throw
new
InvalidOperationException
(SR.GetString(SR.InvalidOperation_HCCountOverflow));
}
int
newThreshold = handleCount + handleCount / deltaPercent;
if
(newThreshold < (threshold - threshold / deltaPercent)) {
lock
(
this
) {
if
(newThreshold > initialThreshold) {
threshold = newThreshold;
}
else
{
threshold = initialThreshold;
}
gc_gen = 0;
}
}
for
(
int
i = 1; i < 3; i++) {
gc_counts[i] = GC.CollectionCount(i);
}
}
}
}
Developer Fusion