CSharpTest.Net
ObjectKeepAlive Constructor(Int32,Int32,TimeSpan,Boolean)
See Also  Example Send Feedback Download Help File
CSharpTest.Net.Library Assembly > CSharpTest.Net.Utils Namespace > ObjectKeepAlive Class > ObjectKeepAlive Constructor : ObjectKeepAlive Constructor(Int32,Int32,TimeSpan,Boolean)

minItems
The minimum number of items desired in the list (kept event after age expires)
maxItems
The maximum number of items desired in the list (discarded even if age has not expired)
maxAge
Determines how long to keep an object if the count is between min and max
externalTicks
True if you want to perform cleanup exclusivly on another thread by calling Tick(), otherwise false

Glossary Item Box

Configures the keep-alive policy for this container

Syntax

Visual Basic (Declaration) 
Public Function New( _
   ByVal minItems As Integer, _
   ByVal maxItems As Integer, _
   ByVal maxAge As TimeSpan, _
   ByVal externalTicks As Boolean _
)
C# 
public ObjectKeepAlive( 
   int minItems,
   int maxItems,
   TimeSpan maxAge,
   bool externalTicks
)

Parameters

minItems
The minimum number of items desired in the list (kept event after age expires)
maxItems
The maximum number of items desired in the list (discarded even if age has not expired)
maxAge
Determines how long to keep an object if the count is between min and max
externalTicks
True if you want to perform cleanup exclusivly on another thread by calling Tick(), otherwise false

Example

Library/Library.Test/TestObjectKeepAlive.cs

C#Copy Code
WeakReference r;
TimeSpan timeout = TimeSpan.FromMilliseconds(100);
ObjectKeepAlive keep = new ObjectKeepAlive(1, 10, timeout, false);

if (true)
{
    object target = new MyObject();
    r = new WeakReference(target);
    keep.Add(target);
    target = null;
}

_destroyed = false;
Assert.IsTrue(r.IsAlive);

for (int i = 0; i < 5; i++)
    keep.Add(i);

Assert.IsTrue(r.IsAlive);

GC.GetTotalMemory(true);
GC.WaitForPendingFinalizers();
Assert.IsTrue(r.IsAlive);
Assert.IsFalse(_destroyed);

long start = DateTime.UtcNow.Ticks;
while ((start + timeout.Ticks) > DateTime.UtcNow.Ticks)
    System.Threading.Thread.SpinWait(100);

//Time has elapsed, yet it nothing is added, and Tick() is not called, it remains in memory
GC.GetTotalMemory(true);
GC.WaitForPendingFinalizers();
Assert.IsTrue(r.IsAlive);
Assert.IsFalse(_destroyed);

//Once the collection is touched with either a call to Add or Tick, timeout will expire
keep.Add(new object());

GC.GetTotalMemory(true);
GC.WaitForPendingFinalizers();
Assert.IsFalse(r.IsAlive);
Assert.IsTrue(_destroyed);
VB.NETCopy Code
Dim r As WeakReference
Dim timeout As TimeSpan = TimeSpan.FromMilliseconds(100)
Dim keep As New ObjectKeepAlive(1, 10, timeout, False)

If True Then
    Dim target As Object = New MyObject()
    r = New WeakReference(target)
    keep.Add(target)
    target = Nothing
End If

_destroyed = False
Assert.IsTrue(r.IsAlive)

Dim i As Integer = 0
While i < 5
    keep.Add(i)
    System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
End While

Assert.IsTrue(r.IsAlive)

GC.GetTotalMemory(True)
GC.WaitForPendingFinalizers()
Assert.IsTrue(r.IsAlive)
Assert.IsFalse(_destroyed)

Dim start As Long = DateTime.UtcNow.Ticks
While (start + timeout.Ticks) > DateTime.UtcNow.Ticks
    System.Threading.Thread.SpinWait(100)
End While

'Time has elapsed, yet it nothing is added, and Tick() is not called, it remains in memory
GC.GetTotalMemory(True)
GC.WaitForPendingFinalizers()
Assert.IsTrue(r.IsAlive)
Assert.IsFalse(_destroyed)

'Once the collection is touched with either a call to Add or Tick, timeout will expire
keep.Add(New Object())

GC.GetTotalMemory(True)
GC.WaitForPendingFinalizers()
Assert.IsFalse(r.IsAlive)
Assert.IsTrue(_destroyed)

Requirements

Target Platforms: Windows XP, Windows Server 2003, Windows Vista, Windows Server 2008, Windows 7

See Also

Generated with Document! X 2011 by Innovasys