本文整理汇总了C#中Pool.AcquireHolder方法的典型用法代码示例。如果您正苦于以下问题:C# Pool.AcquireHolder方法的具体用法?C# Pool.AcquireHolder怎么用?C# Pool.AcquireHolder使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Pool
的用法示例。
在下文中一共展示了Pool.AcquireHolder方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Test_Dispose_4
public void Test_Dispose_4()
{
Disposable disposable;
PoolResourceHolder<Disposable> poolResourceHolder;
var pool = new Pool<Disposable>(() => new Disposable(), AccessStrategy.LIFO, LoadingStrategy.Eager, 1);
pool.Dispose();
Assert.Throws<ObjectDisposedException>(() => pool.Acquire());
Assert.Throws<ObjectDisposedException>(() => pool.AcquireHolder());
Assert.Throws<ObjectDisposedException>(() => pool.TryAcquire(TimeSpan.FromDays(1), out disposable));
Assert.Throws<ObjectDisposedException>(() => pool.TryAcquireHolder(TimeSpan.FromDays(1), out poolResourceHolder));
Assert.Throws<ObjectDisposedException>(() => pool.TryAcquireImmediately(out disposable));
Assert.Throws<ObjectDisposedException>(() => pool.TryAcquireHolderImmediately(out poolResourceHolder));
// Асинхронные методы.
try
{
pool.AcquireAsync().Wait();
Assert.Fail("Must throw exception.");
}
catch (AggregateException error)
{
Assert.True(error.InnerExceptions.Single() is ObjectDisposedException);
}
try
{
pool.AcquireHolderAsync().Wait();
Assert.Fail("Must throw exception.");
}
catch (AggregateException error)
{
Assert.True(error.InnerExceptions.Single() is ObjectDisposedException);
}
}
示例2: Test_HolderWorks
public void Test_HolderWorks()
{
var pool = new Pool<object>(() => new object(), AccessStrategy.LIFO, LoadingStrategy.Eager, 1);
using (pool.AcquireHolder())
{
Assert.AreEqual(1, pool.Allocated);
Assert.AreEqual(0, pool.Available);
}
Assert.AreEqual(1, pool.Allocated);
Assert.AreEqual(1, pool.Available);
}
示例3: Test_Speed_Holder
public void Test_Speed_Holder()
{
var pool = new Pool<object>(() => new object(), AccessStrategy.LIFO, LoadingStrategy.Eager, 1);
long cycles = 0;
TimeSpan testTime = TimeSpan.FromSeconds(1);
var sw = Stopwatch.StartNew();
while (sw.Elapsed < testTime)
using (pool.AcquireHolder())
cycles++;
Console.Out.WriteLine("Done {0} Acquire-Release cycles in 1 second (using resource holder).", cycles);
}
示例4: Test_HolderMultipleDispose
public void Test_HolderMultipleDispose()
{
var pool = new Pool<object>(() => new object(), AccessStrategy.LIFO, LoadingStrategy.Eager, 1);
PoolResourceHolder<object> poolResourceHolder = pool.AcquireHolder();
Assert.AreEqual(1, pool.Allocated);
Assert.AreEqual(0, pool.Available);
poolResourceHolder.Dispose();
poolResourceHolder.Dispose();
poolResourceHolder.Dispose();
Assert.AreEqual(1, pool.Allocated);
Assert.AreEqual(1, pool.Available);
}