本文整理汇总了C#中ObjectPool.Free方法的典型用法代码示例。如果您正苦于以下问题:C# ObjectPool.Free方法的具体用法?C# ObjectPool.Free怎么用?C# ObjectPool.Free使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ObjectPool
的用法示例。
在下文中一共展示了ObjectPool.Free方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestAllocateAndFree
public void TestAllocateAndFree()
{
var pool = new ObjectPool<TestElement>();
pool.Initialize();
Assert.IsTrue(pool.UsedListSize == 0 && pool.FreeListSize == 0);
// allocate some instances
var e1 = pool.Allocate();
var e2 = pool.Allocate();
Assert.IsTrue(pool.UsedListSize == 2 && pool.FreeListSize == 0);
Assert.IsTrue(e1 != e2);
Assert.IsTrue(e1.value == 0);
// free both and then allocate two more again - we should get both back
pool.Free(e1);
pool.Free(e2);
Assert.IsTrue(pool.UsedListSize == 0 && pool.FreeListSize == 2);
var e3 = pool.Allocate();
var e4 = pool.Allocate();
Assert.IsTrue(pool.UsedListSize == 2 && pool.FreeListSize == 0);
Assert.IsTrue(e3 == e2);
Assert.IsTrue(e4 == e1);
}
示例2: WillReturnObjectToPoolIfExcessCapacity
public void WillReturnObjectToPoolIfExcessCapacity()
{
var value = new Mock<IDisposable>();
using (var objectPool = new ObjectPool<IDisposable>(() => value.Object, item => item.Dispose()))
{
objectPool.Free(objectPool.Allocate());
value.Verify(mock => mock.Dispose(), Times.Never());
}
}
示例3: WillAllocateExistingObjectIfPoolNotEmpty
public void WillAllocateExistingObjectIfPoolNotEmpty()
{
using (var objectPool = new ObjectPool<Object>(() => new Object()))
{
var value = objectPool.Allocate();
objectPool.Free(value);
Assert.Equal(value, objectPool.Allocate());
}
}
示例4: TestAllocAndFree
public void TestAllocAndFree()
{
var pool = new ObjectPool<PoolableObject>(10);
var poolableObject = pool.Alloc();
poolableObject.Value = 22;
pool.Free(poolableObject);
Assert.AreEqual(0, poolableObject.Value);
}
示例5: TestFactoryAndReset
public void TestFactoryAndReset()
{
var pool = new ObjectPool<TestElement>();
pool.Initialize(() => { var e = new TestElement(); e.value = 42; return e; });
// allocate a new instance using this custom factory, make sure the factory code got called
var e1 = pool.Allocate();
Assert.IsTrue(e1.value == 42);
// free it, and make sure it got reset
pool.Free(e1);
Assert.IsTrue(e1.value == 0);
// now allocate again, this time since it's coming from the object pool,
// the factory code won't be called
var e2 = pool.Allocate();
Assert.IsTrue(e1 == e2);
Assert.IsTrue(e2.value == 0); // because this is coming from the pool, not factory
}
示例6: TestPoolRelease
public void TestPoolRelease()
{
var pool = new ObjectPool<TestElement>();
pool.Initialize(() => { var e = new TestElement(); e.value = 42; return e; });
// allocate a new instance using this custom factory, make sure the factory code got called
var e1 = pool.Allocate();
Assert.IsTrue(e1.value == 42);
Assert.IsTrue(pool.UsedListSize == 1);
Assert.IsTrue(pool.FreeListSize == 0);
// allocate and free an instance. this will grow the pool to size one
pool.Free(pool.Allocate());
Assert.IsTrue(pool.UsedListSize == 1);
Assert.IsTrue(pool.FreeListSize == 1);
// now release the entire object pool. this will clear out the free list, but will not
// affect the object already in circulation, since we're not tracking them.
pool.Release();
Assert.IsTrue(e1.value == 42);
Assert.IsTrue(pool.UsedListSize == 1);
Assert.IsTrue(pool.FreeListSize == -1);
}
示例7: FreeEventQueue
private static void FreeEventQueue(AsyncQueue<CompilationEvent> eventQueue, ObjectPool<AsyncQueue<CompilationEvent>> eventQueuePool)
{
if (eventQueue == null || ReferenceEquals(eventQueue, s_EmptyEventQueue))
{
return;
}
if (eventQueue.Count > 0)
{
CompilationEvent discarded;
while (eventQueue.TryDequeue(out discarded)) ;
}
if (!eventQueue.IsCompleted)
{
eventQueuePool.Free(eventQueue);
}
else
{
eventQueuePool.ForgetTrackedObject(eventQueue);
}
}
示例8: WillDisposeObjectIfPoolCapacityExceeded
public void WillDisposeObjectIfPoolCapacityExceeded()
{
var first = new Mock<IDisposable>();
var second = new Mock<IDisposable>();
var objects = new Queue<IDisposable>(new[] { first.Object, second.Object });
using (var objectPool = new ObjectPool<IDisposable>(() => objects.Dequeue(), item => item.Dispose(), size: 1))
{
var value1 = objectPool.Allocate();
var value2 = objectPool.Allocate();
objectPool.Free(value1);
objectPool.Free(value2);
first.Verify(mock => mock.Dispose(), Times.Never());
second.Verify(mock => mock.Dispose(), Times.Once());
}
}