本文整理汇总了C#中ConcurrentBag.All方法的典型用法代码示例。如果您正苦于以下问题:C# ConcurrentBag.All方法的具体用法?C# ConcurrentBag.All怎么用?C# ConcurrentBag.All使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ConcurrentBag
的用法示例。
在下文中一共展示了ConcurrentBag.All方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Instance_ThreadSafe
public void Instance_ThreadSafe()
{
using (var gate = new Barrier(5))
{
var result = new ConcurrentBag<AnyConstructorFinder>();
Action test = () =>
{
gate.SignalAndWait(20);
var instance = AnyConstructorFinder.Instance;
Thread.MemoryBarrier();
result.Add(instance);
};
var cycleState = Parallel.For(0, 200,
new ParallelOptions { MaxDegreeOfParallelism = 15 },
x => { test(); })
;
while (!cycleState.IsCompleted)
{
Thread.Sleep(100);
}
Assert.IsTrue(result.All(x => x != null));
Assert.IsTrue(result.Distinct().Count() == 1);
}
}
示例2: AddressUidExtension_should_always_report_same_value
public void AddressUidExtension_should_always_report_same_value()
{
var values = new ConcurrentBag<int>();
var parallelOps = 1000;
var loop = Parallel.For(0, parallelOps, i =>
{
values.Add(AddressUidExtension.Uid(Sys));
});
SpinWait.SpinUntil(() => loop.IsCompleted);
Assert.True(values.All(x => x == AddressUidExtension.Uid(Sys)));
}
示例3: GetService_can_be_accessed_from_multiple_threads_concurrently
public void GetService_can_be_accessed_from_multiple_threads_concurrently()
{
for (var i = 0; i < 30; i++)
{
var bag = new ConcurrentBag<DbProviderServices>();
var resolver = new DefaultProviderServicesResolver();
ExecuteInParallel(() => bag.Add(resolver.GetService<DbProviderServices>("System.Data.SqlClient")));
Assert.Equal(20, bag.Count);
Assert.True(bag.All(c => SqlProviderServices.Instance == c));
}
}
示例4: Should_Concurrently_Load_A_Session_And_Unlock_It_Afterwards
public void Should_Concurrently_Load_A_Session_And_Unlock_It_Afterwards()
{
SessionDatabase.CreateSession(Constants.FullSessionId, ShortData);
var store = new SqlSessionStateStore(Constants.ConnectionString, Constants.SessionTimeout);
var results = new ConcurrentBag<bool>();
for (var i = 0; i < 20; i++) ThreadPool.QueueUserWorkItem(
x => results.Add((store.Load(_sessionId) ?? new byte[] {}).SequenceEqual(ShortData)));
while (results.Count < 20) Thread.Sleep(100);
results.All(x => x).ShouldBeTrue();
}
示例5: TestSaveUpdatesAutoIncrementingField
public void TestSaveUpdatesAutoIncrementingField()
{
//---------------Set up test pack-------------------
ClassDef.ClassDefs.Clear();
TestAutoInc.LoadClassDefWithAutoIncrementingID();
var newIds = new ConcurrentBag<int?>();
//---------------Execute Test ----------------------
Parallel.For(0, 1000, i => {
//---------------Set up test pack-------------------
var bo = new TestAutoInc();
bo.SetPropertyValue("testfield", "testing 123");
//---------------Assert Precondition----------------
Assert.IsFalse(bo.TestAutoIncID.HasValue);
//---------------Execute Test ----------------------
bo.Save();
//---------------Test Result -----------------------
newIds.Add(bo.TestAutoIncID);
});
//---------------Test Result -----------------------
Assert.IsTrue(newIds.All(i => i.HasValue));
Assert.IsTrue(newIds.All(i => i > 0));
Assert.That(newIds.Distinct().Count(), Is.EqualTo(1000), "Every generated ID must be unique");
}
示例6: GetService_can_be_accessed_from_multiple_threads_concurrently
public void GetService_can_be_accessed_from_multiple_threads_concurrently()
{
for (var i = 0; i < 30; i++)
{
var bag = new ConcurrentBag<IPilkington>();
var karl = new Karl();
var resolver = new TransientDependencyResolver<IPilkington>(() => karl, "Karl");
ExecuteInParallel(() => bag.Add(resolver.GetService<IPilkington>("Karl")));
Assert.Equal(20, bag.Count);
Assert.True(bag.All(c => karl == c));
}
}
示例7: GetService_can_be_accessed_from_multiple_threads_concurrently
public void GetService_can_be_accessed_from_multiple_threads_concurrently()
{
for (var i = 0; i < 30; i++)
{
var bag = new ConcurrentBag<IPilkington>();
var karl1 = new Mock<IPilkington>().Object;
var resolver = new CompositeResolver<IDbDependencyResolver, IDbDependencyResolver>(
new SingletonDependencyResolver<IPilkington>(karl1),
new SingletonDependencyResolver<IPilkington>(new Mock<IPilkington>().Object));
ExecuteInParallel(() => bag.Add(resolver.GetService<IPilkington>()));
Assert.Equal(20, bag.Count);
Assert.True(bag.All(c => karl1 == c));
}
}
示例8: GetService_can_be_accessed_from_multiple_threads_concurrently
public void GetService_can_be_accessed_from_multiple_threads_concurrently()
{
for (var i = 0; i < 30; i++)
{
var bag = new ConcurrentBag<AttributeProvider>();
var resolver = new RootDependencyResolver();
resolver.AddDefaultResolver(new SingletonDependencyResolver<AttributeProvider>(new AttributeProvider()));
resolver.AddDefaultResolver(new SingletonDependencyResolver<AttributeProvider>(new AttributeProvider()));
ExecuteInParallel(() => bag.Add(resolver.GetService<AttributeProvider>()));
Assert.Equal(20, bag.Count);
Assert.True(bag.All(c => c.GetType() == typeof(AttributeProvider)));
}
}
示例9: Main
static void Main(string[] args)
{
var stopWatch = new Stopwatch();
using (var eventStore = WireupEventStore())
{
Console.Write("Inserting {0} AggregateRoots with {1} commits each", AmountOfRoots, AmountOfCommitsPerRoot);
stopWatch.Start();
byte[] payload = new byte[80];
var resetEvents = new ConcurrentBag<ManualResetEventSlim>();
for (int i = 0; i < AmountOfRoots; ++i)
{
if (i % (AmountOfRoots / 100) == 0)
{
Thread.Sleep(500);
Console.Write(".");
}
var resetEvent = new ManualResetEventSlim();
resetEvents.Add(resetEvent);
ThreadPool.QueueUserWorkItem(x =>
{
var currentRoot = Guid.NewGuid();
for (int commits = 0; commits < AmountOfCommitsPerRoot; ++commits)
{
using (var stream = eventStore.OpenStream(currentRoot, 0, int.MaxValue))
{
stream.Add(new EventMessage() { Body = payload });
stream.CommitChanges(Guid.NewGuid());
}
}
((ManualResetEventSlim)x).Set();
}, resetEvent);
}
while (!resetEvents.All(x => x.IsSet))
{
Thread.Sleep(100);
}
stopWatch.Stop();
Console.WriteLine("done in {0} ms.", stopWatch.ElapsedMilliseconds);
Console.ReadKey();
}
}
示例10: GetService_can_be_accessed_from_multiple_threads_concurrently
public void GetService_can_be_accessed_from_multiple_threads_concurrently()
{
for (var i = 0; i < 30; i++)
{
var bag = new ConcurrentBag<IDatabaseInitializer<FakeContext1>>();
var resolver = new DatabaseInitializerResolver();
var initializer = new Mock<IDatabaseInitializer<FakeContext1>>().Object;
ExecuteInParallel(
() =>
{
resolver.SetInitializer(typeof(FakeContext1), initializer);
bag.Add(resolver.GetService<IDatabaseInitializer<FakeContext1>>());
});
Assert.Equal(20, bag.Count);
Assert.True(bag.All(c => initializer == c));
}
}
示例11: GetService_can_be_accessed_from_multiple_threads_concurrently
public void GetService_can_be_accessed_from_multiple_threads_concurrently()
{
var appConfig = new AppConfig(
CreateEmptyConfig().AddDefaultConnectionFactory(typeof(FakeConnectionFactory).AssemblyQualifiedName));
for (var i = 0; i < 30; i++)
{
var bag = new ConcurrentBag<IDbConnectionFactory>();
var resolver = new AppConfigDependencyResolver(appConfig);
ExecuteInParallel(() => bag.Add(resolver.GetService<IDbConnectionFactory>()));
Assert.Equal(20, bag.Count);
Assert.True(bag.All(c => resolver.GetService<IDbConnectionFactory>() == c));
}
}
示例12: ConfigurationThreadTest
private static void ConfigurationThreadTest(Action<DbConfigurationManager> beforeThreads, Action<DbConfigurationManager> inThreads)
{
for (var i = 0; i < 30; i++)
{
var configurationBag = new ConcurrentBag<DbConfiguration>();
var manager = new DbConfigurationManager(new DbConfigurationLoader(), new DbConfigurationFinder());
beforeThreads(manager);
ExecuteInParallel(
() =>
{
inThreads(manager);
configurationBag.Add(manager.GetConfiguration());
});
Assert.Equal(20, configurationBag.Count);
Assert.True(configurationBag.All(c => manager.GetConfiguration() == c));
}
}
示例13: Should_Return_Session_State_For_All_Classic_Concurent_Requests
public void Should_Return_Session_State_For_All_Classic_Concurent_Requests()
{
var counts = new ConcurrentBag<int>();
var session = Common.GetClassic("command=add&key=state&value=CO&datatype=String");
for (var i = 0; i < 20; i++) ThreadPool.QueueUserWorkItem(x => counts.Add(session.GetClassic().Data.Count));
while (counts.Count < 20) Thread.Sleep(100);
counts.All(x => x == 1).ShouldBeTrue();
}
示例14: Model_hash_can_be_calculated_from_multiple_threads_using_a_single_DbCompiledModel
public void Model_hash_can_be_calculated_from_multiple_threads_using_a_single_DbCompiledModel()
{
var hashes = new ConcurrentBag<string>();
ExecuteInParallel(
() =>
{
using (var context = new SimpleModelContext())
{
#pragma warning disable 612,618
var hash = EdmMetadata.TryGetModelHash(context);
#pragma warning restore 612,618
Assert.NotNull(hash);
hashes.Add(hash);
}
});
Assert.True(hashes.All(h => hashes.First() == h));
}
示例15: EDMX_can_be_written_from_multiple_threads_using_a_single_DbCompiledModel
public void EDMX_can_be_written_from_multiple_threads_using_a_single_DbCompiledModel()
{
var edmxs = new ConcurrentBag<string>();
ExecuteInParallel(
() =>
{
var edmxBuilder = new StringBuilder();
using (var context = new SimpleModelContext())
{
// Cached DbCompiledModel will be used each time
EdmxWriter.WriteEdmx(context, XmlWriter.Create(edmxBuilder));
}
var edmx = edmxBuilder.ToString();
Assert.True(edmx.Contains("EntitySet Name=\"Products\""));
Assert.True(edmx.Contains("EntitySet Name=\"Categories\""));
edmxs.Add(edmx);
});
Assert.True(edmxs.All(m => edmxs.First() == m));
}