예제 #1
0
        static void HandleOrderedQueue <T>(IOrderedQueue <IndexedValue <T> > orderedQueue, Func <bool> enqueuingCompleted, CancellationToken cancellationToken = default)
        {
            bool IsRunning()
            {
                var queueCompleted = enqueuingCompleted() && orderedQueue.Count == 0;

                return(!queueCompleted);
            };

            var lookingForIndex = 0;

            while (IsRunning())
            {
                // if (lookingForIndex == 3) throw new Exception("!!!HandleOrderedQueue!!!");

                // wait for an item being added to the queue
                Semaphore.Wait(cancellationToken);

                while (orderedQueue.Count > 0)
                {
                    cancellationToken.ThrowIfCancellationRequested();

                    if (!orderedQueue.TryPeek(out var pickedItem))
                    {
                        throw new InvalidOperationException("Could not peek an item");
                    }

                    if (pickedItem.Index != lookingForIndex)
                    {
                        Console.WriteLine($"                                                      [wrong-index] Peeked an item with index {pickedItem.Index}, but looking for an item with index {lookingForIndex}");
                        break;
                    }

                    if (!orderedQueue.TryDequeue(out var item))
                    {
                        throw new InvalidOperationException($"Could not dequeue already peeked item {pickedItem.Index}");
                    }

                    Console.WriteLine($"           Writing to FS item {item.Index}");

                    // emulate some work
                    Thread.Sleep(300);

                    lookingForIndex++;
                }
            }
        }
예제 #2
0
 public SampleStream(ICredentials credentials, OrderedQueue orderedQueue)
 {
     Auth.SetCredentials(new Tweetinvi.Models.TwitterCredentials(credentials.ConsumerKey, credentials.ConsumerSecret, credentials.AccessToken, credentials.AccessTokenSecret));
     this.Queue          = orderedQueue;
     this.filteredStream = Tweetinvi.Stream.CreateFilteredStream();
 }