本文整理汇总了C#中BlockingCollection.TryTake方法的典型用法代码示例。如果您正苦于以下问题:C# BlockingCollection.TryTake方法的具体用法?C# BlockingCollection.TryTake怎么用?C# BlockingCollection.TryTake使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BlockingCollection
的用法示例。
在下文中一共展示了BlockingCollection.TryTake方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: NegativePathWithCollectionsTyped
public void NegativePathWithCollectionsTyped()
{
var nestedNode = new Node
{
Name = "Parent",
Children = Enumerable.Range(0, 10).Select(x => new Node()
{
Name = "Child" + x,
Children = Enumerable.Range(0, 5).Select(y => new Node()
{
Name = "Grandchild",
Children = null
}).ToList()
}).ToList()
};
var simpleNode = new Node
{
Name = "ChildlessParent",
Children = null
};
using (var store = NewDocumentStore())
{
using (var session = store.OpenSession())
{
session.Store(nestedNode);
session.Store(simpleNode);
session.SaveChanges();
}
var subscriptionID = store.Subscriptions.Create<Node>(new SubscriptionCriteria<Node>
{
PropertiesNotMatch = new Dictionary<Expression<Func<Node, object>>, RavenJToken>()
{
{node => node.Children.SelectMany(x => x.Children).Select(x => x.Name), "Parent"}
}
});
var subscription = store.Subscriptions.Open<Node>(subscriptionID, new SubscriptionConnectionOptions());
var keys = new BlockingCollection<Node>();
subscription.Subscribe(x =>
{
keys.Add(x);
});
Node key;
Assert.True(keys.TryTake(out key, TimeSpan.FromSeconds(20)));
Assert.True(keys.TryTake(out key, TimeSpan.FromSeconds(20)));
subscription.Dispose();
}
}
示例2: CanGetNotificationsAboutConflictedDocuments
public void CanGetNotificationsAboutConflictedDocuments()
{
using (var store1 = CreateEmbeddableStore())
using (var store2 = CreateEmbeddableStore())
{
store1.DatabaseCommands.Put("users/1", null, new RavenJObject
{
{"Name", "Ayende"}
}, new RavenJObject());
store2.DatabaseCommands.Put("users/1", null, new RavenJObject
{
{"Name", "Rahien"}
}, new RavenJObject());
var list = new BlockingCollection<ReplicationConflictNotification>();
var taskObservable = store2.Changes();
taskObservable.Task.Wait();
var observableWithTask = taskObservable.ForAllReplicationConflicts();
observableWithTask.Task.Wait();
observableWithTask
.Subscribe(list.Add);
TellFirstInstanceToReplicateToSecondInstance();
ReplicationConflictNotification replicationConflictNotification;
Assert.True(list.TryTake(out replicationConflictNotification, TimeSpan.FromSeconds(10)));
Assert.Equal("users/1", replicationConflictNotification.Id);
Assert.Equal(replicationConflictNotification.ItemType, ReplicationConflictTypes.DocumentReplicationConflict);
Assert.Equal(2, replicationConflictNotification.Conflicts.Length);
Assert.Equal(ReplicationOperationTypes.Put, replicationConflictNotification.OperationType);
}
}
示例3: CanGetNotificationAboutDocumentIndexUpdate
public void CanGetNotificationAboutDocumentIndexUpdate()
{
using (var server = GetNewServer())
using (var store = NewRemoteDocumentStore(ravenDbServer: server))
{
var list = new BlockingCollection<IndexChangeNotification>();
var taskObservable = store.Changes();
taskObservable.Task.Wait();
var observableWithTask = taskObservable.ForIndex("Raven/DocumentsByEntityName");
observableWithTask.Task.Wait();
observableWithTask
.Subscribe(list.Add);
using (var session = store.OpenSession())
{
session.Store(new Item(), "items/1");
session.SaveChanges();
}
IndexChangeNotification indexChangeNotification;
Assert.True(list.TryTake(out indexChangeNotification, TimeSpan.FromSeconds(5)));
Assert.Equal("Raven/DocumentsByEntityName", indexChangeNotification.Name);
Assert.Equal(indexChangeNotification.Type, IndexChangeTypes.MapCompleted);
}
}
示例4: CanGetNotificationAboutDocumentDelete
public void CanGetNotificationAboutDocumentDelete()
{
using (GetNewServer())
using (var store = new DocumentStore
{
Url = "http://localhost:8079"
}.Initialize())
{
var list = new BlockingCollection<DocumentChangeNotification>();
var taskObservable = store.Changes();
taskObservable.Task.Wait();
var observableWithTask = taskObservable.ForDocument("items/1");
observableWithTask.Task.Wait();
observableWithTask
.Where(x => x.Type == DocumentChangeTypes.Delete)
.Subscribe(list.Add);
using (var session = store.OpenSession())
{
session.Store(new Item(), "items/1");
session.SaveChanges();
}
store.DatabaseCommands.Delete("items/1", null);
DocumentChangeNotification DocumentChangeNotification;
Assert.True(list.TryTake(out DocumentChangeNotification, TimeSpan.FromSeconds(2)));
Assert.Equal("items/1", DocumentChangeNotification.Id);
Assert.Equal(DocumentChangeNotification.Type, DocumentChangeTypes.Delete);
((RemoteDatabaseChanges) taskObservable).DisposeAsync().Wait();
}
}
示例5: CanGetNotificationAboutIndexUpdate
public void CanGetNotificationAboutIndexUpdate()
{
using (GetNewServer())
using (var store = new DocumentStore
{
Url = "http://localhost:8079"
}.Initialize())
{
var list = new BlockingCollection<IndexChangeNotification>();
var taskObservable = store.Changes();
taskObservable.Task.Wait();
taskObservable
.ForIndex("Raven/DocumentsByEntityName")
.Subscribe(list.Add);
using (var session = store.OpenSession())
{
session.Store(new ClientServer.Item(), "items/1");
session.SaveChanges();
}
IndexChangeNotification changeNotification;
Assert.True(list.TryTake(out changeNotification, TimeSpan.FromSeconds(2)));
Assert.Equal("Raven/DocumentsByEntityName", changeNotification.Name);
Assert.Equal(changeNotification.Type, IndexChangeTypes.MapCompleted);
}
}
示例6: CanGetNotificationAboutDocumentDelete
public void CanGetNotificationAboutDocumentDelete()
{
using (var store = NewDocumentStore())
{
var list = new BlockingCollection<DocumentChangeNotification>();
store.Changes()
.ForDocument("items/1")
.Where(x=>x.Type == DocumentChangeTypes.Delete)
.Subscribe(list.Add);
using (var session = store.OpenSession())
{
session.Store(new Item(), "items/1");
session.SaveChanges();
}
store.DatabaseCommands.Delete("items/1", null);
DocumentChangeNotification documentChangeNotification;
Assert.True(list.TryTake(out documentChangeNotification, TimeSpan.FromSeconds(2)));
Assert.Equal("items/1", documentChangeNotification.Id);
Assert.Equal(documentChangeNotification.Type, DocumentChangeTypes.Delete);
}
}
示例7: CanGetNotificationsFromTenant_ExplicitDatabase
public void CanGetNotificationsFromTenant_ExplicitDatabase()
{
using (GetNewServer())
using (var store = new DocumentStore
{
Url = "http://localhost:8079",
}.Initialize())
{
store.DatabaseCommands.EnsureDatabaseExists("test");
var list = new BlockingCollection<DocumentChangeNotification>();
var taskObservable = store.Changes("test");
taskObservable.Task.Wait();
taskObservable
.ForDocument("items/1")
.Subscribe(list.Add);
using (var session = store.OpenSession("test"))
{
session.Store(new ClientServer.Item(), "items/1");
session.SaveChanges();
}
DocumentChangeNotification DocumentChangeNotification;
Assert.True(list.TryTake(out DocumentChangeNotification, TimeSpan.FromSeconds(15)));
Assert.Equal("items/1", DocumentChangeNotification.Id);
Assert.Equal(DocumentChangeNotification.Type, DocumentChangeTypes.Put);
}
}
示例8: WithWindowsAuth
public void WithWindowsAuth()
{
using (GetNewServer())
using (var store = new DocumentStore
{
Url = "http://localhost:8079",
}.Initialize())
{
var list = new BlockingCollection<DocumentChangeNotification>();
var taskObservable = store.Changes();
taskObservable.Task.Wait();
var documentSubscription = taskObservable.ForDocument("items/1");
documentSubscription.Task.Wait();
documentSubscription
.Subscribe(list.Add);
using (var session = store.OpenSession())
{
session.Store(new ClientServer.Item(), "items/1");
session.SaveChanges();
}
DocumentChangeNotification changeNotification;
Assert.True(list.TryTake(out changeNotification, TimeSpan.FromSeconds(2)));
Assert.Equal("items/1", changeNotification.Id);
Assert.Equal(changeNotification.Type, DocumentChangeTypes.Put);
}
}
示例9: ForDocumentsInCollectionEmbedded2
public void ForDocumentsInCollectionEmbedded2()
{
using (var store = NewDocumentStore())
{
var list = new BlockingCollection<DocumentChangeNotification>();
store.Changes().Task.Result
.ForDocumentsInCollection<Person>().Task.Result
.Subscribe(list.Add);
using (var session = store.OpenSession())
{
session.Store(new Person());
session.Store(new User());
session.SaveChanges();
}
DocumentChangeNotification documentChangeNotification;
Assert.True(list.TryTake(out documentChangeNotification, TimeSpan.FromSeconds(2)));
Assert.Equal("people/1", documentChangeNotification.Id);
Assert.Equal("People", documentChangeNotification.CollectionName);
Assert.Equal(documentChangeNotification.Type, DocumentChangeTypes.Put);
}
}
示例10: CanGetNotificationAboutDocumentPut
public void CanGetNotificationAboutDocumentPut()
{
using(GetNewServer())
{using (var store = new DocumentStore
{
Url = "http://localhost:8079",
Conventions =
{
FailoverBehavior = FailoverBehavior.FailImmediately
}
}.Initialize())
{
var list = new BlockingCollection<DocumentChangeNotification>();
var taskObservable = store.Changes();
taskObservable.Task.Wait();
var observableWithTask = taskObservable.ForDocument("items/1");
observableWithTask.Task.Wait();
observableWithTask.Subscribe(list.Add);
using (var session = store.OpenSession())
{
session.Store(new Item(), "items/1");
session.SaveChanges();
}
DocumentChangeNotification documentChangeNotification;
Assert.True(list.TryTake(out documentChangeNotification, TimeSpan.FromSeconds(3)));
Assert.Equal("items/1", documentChangeNotification.Id);
Assert.Equal(documentChangeNotification.Type, DocumentChangeTypes.Put);
Assert.NotNull(documentChangeNotification.Etag);
}
Thread.Sleep(1000);
}
}
示例11: InternalCancellation_WakingUp
public static void InternalCancellation_WakingUp()
{
for (int test = 0; test < 2; test++)
{
BlockingCollection<int> coll1 = new BlockingCollection<int>(1);
coll1.Add(1); //fills the collection.
Assert.False(coll1.IsAddingCompleted,
"InternalCancellation_WakingUp: At this point CompleteAdding should not have occurred.");
// This is racy on what we want to test, in that it's possible this queued work could execute
// so quickly that CompleteAdding happens before the tested method gets invoked, but the test
// should still pass in such cases, we're just testing something other than we'd planned.
Task t = Task.Run(() => coll1.CompleteAdding());
// Try different methods that should wake up once CompleteAdding has been called
int item = coll1.Take(); // remove the existing item in the collection
switch (test)
{
case 0:
Assert.Throws<InvalidOperationException>(() => coll1.Take());
break;
case 1:
Assert.False(coll1.TryTake(out item));
break;
}
t.Wait();
Assert.True(coll1.IsAddingCompleted,
"InternalCancellation_WakingUp: At this point CompleteAdding should have occurred.");
}
}
示例12: CheckNotificationInIIS
public void CheckNotificationInIIS()
{
using (var store = NewDocumentStore())
{
var list = new BlockingCollection<DocumentChangeNotification>();
var taskObservable = store.Changes();
taskObservable.Task.Wait();
var observableWithTask = taskObservable.ForDocument("items/1");
observableWithTask.Task.Wait();
observableWithTask.Subscribe(list.Add);
using (var session = store.OpenSession())
{
session.Store(new Item(), "items/1");
session.SaveChanges();
}
DocumentChangeNotification documentChangeNotification;
Assert.True(list.TryTake(out documentChangeNotification, TimeSpan.FromSeconds(5)));
Assert.Equal("items/1", documentChangeNotification.Id);
Assert.Equal(documentChangeNotification.Type, DocumentChangeTypes.Put);
}
}
示例13: CanGetNotificationsAboutConflictedAttachements
public void CanGetNotificationsAboutConflictedAttachements()
{
using(var store1 = CreateStore())
using (var store2 = CreateStore())
{
store1.DatabaseCommands.PutAttachment("attachment/1", null, new MemoryStream(new byte[] {1, 2, 3}),
new RavenJObject());
store2.DatabaseCommands.PutAttachment("attachment/1", null, new MemoryStream(new byte[] {1, 2, 3}),
new RavenJObject());
var list = new BlockingCollection<ReplicationConflictNotification>();
var taskObservable = store2.Changes();
taskObservable.Task.Wait();
var observableWithTask = taskObservable.ForAllReplicationConflicts();
observableWithTask.Task.Wait();
observableWithTask
.Subscribe(list.Add);
TellFirstInstanceToReplicateToSecondInstance();
ReplicationConflictNotification replicationConflictNotification;
Assert.True(list.TryTake(out replicationConflictNotification, TimeSpan.FromSeconds(10)));
Assert.Equal("attachment/1", replicationConflictNotification.Id);
Assert.Equal(replicationConflictNotification.ItemType, ReplicationConflictTypes.AttachmentReplicationConflict);
Assert.Equal(2, replicationConflictNotification.Conflicts.Length);
Assert.Equal(ReplicationOperationTypes.Put, replicationConflictNotification.OperationType);
}
}
示例14: AddTryTake
public void AddTryTake(int producerThreads, int consumerThreads)
{
var stack = new BlockingCollection<int>();
var startEvent = new ManualResetEventSlim(false);
var finished = 0;
var stop = false;
var producerTasks = Enumerable.Range(0, producerThreads).Select(i => Task.Factory.StartNew(() =>
{
var count = iterations/producerThreads;
startEvent.Wait();
for (var j = 0; j < count; j++)
stack.Add(0);
Interlocked.Increment(ref finished);
if (finished >= producerThreads) stop = true;
}, TaskCreationOptions.LongRunning)).ToArray();
var consumerTasks = Enumerable.Range(0, consumerThreads).Select(i => Task.Factory.StartNew(() =>
{
int num;
startEvent.Wait();
while (!stop) stack.TryTake(out num);
}, TaskCreationOptions.LongRunning)).ToArray();
var stopwatch = Stopwatch.StartNew();
startEvent.Set();
stop = true;
Task.WaitAll(producerTasks);
Task.WaitAll(consumerTasks);
stopwatch.StopAndLog(iterations);
}
示例15: Add
public void Add(IEventTracker eventTracker, Node node)
{
var message = node.CreateStartJobMessage(eventTracker,
Singleton<DIFactory>.Instance.Create<IChannel>("OutOfNodeInSameProcess"));
var queue = new BlockingCollection<IMessage>(1);
var callback = new QueueBasedMessageReceiver(queue);
var obj = Singleton<DIFactory>.Instance.Create<IObject>(
new Dictionary<string, object>() {
{ TypeBasedMessageReceiverRegistryInfoObjectPattern.Type.Name, "StartJobResponse" }
}
);
MessageBus.Connect(obj, callback);
MessageBus.Send(message);
IMessage response;
var failMessage = "Job " + eventTracker.GetType().Name + " could not start.";
if (!queue.TryTake(out response, 2000))
{
throw new Exception(failMessage);
}
if (!ResponseMessageObjectPattern.Success[response])
{
throw new Exception(failMessage);
}
_trackers.Add(eventTracker);
}