当前位置: 首页>>代码示例>>C#>>正文


C# BlockingCollection.TryAdd方法代码示例

本文整理汇总了C#中BlockingCollection.TryAdd方法的典型用法代码示例。如果您正苦于以下问题:C# BlockingCollection.TryAdd方法的具体用法?C# BlockingCollection.TryAdd怎么用?C# BlockingCollection.TryAdd使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在BlockingCollection的用法示例。


在下文中一共展示了BlockingCollection.TryAdd方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Listener

        public Listener(BlockingCollection<CommandPending> pendingCommands, BlockingCollection<CommandResult> commandResults, History history)
        {
            var server = new WebSocketServer("ws://localhost:8181");
            var commandMap = new Dictionary<int, IWebSocketConnection>();
            int commandID = 0;
            var userMap = new Dictionary<IWebSocketConnection, User>();

            history.EventApplied += (sender, e) => Broadcast(e);
            server.Start(socket => {
                socket.OnOpen = () => {
                    Console.WriteLine("Open!");
                    allSockets.Add(socket);

                    // send the current world
                    // TODO: race condition if event fires before world is sent
                    foreach (var entity in history.Head.Entities.Select(x => x.Value)) {
                        Send(socket, entity);
                    }
                };
                socket.OnClose = () => {
                    Console.WriteLine("Close!");
                    allSockets.Remove(socket);
                    User user;
                    if (userMap.TryGetValue(socket, out user)) {
                        var remove = new Remove() { name = user.Name };
                        pendingCommands.TryAdd(new CommandPending(commandID, new User(), remove));
                        commandMap.Add(commandID++, socket);
                    }
                };
                socket.OnMessage = message => {
                    Console.WriteLine(message);
                    try {
                        var update = JsonConvert.DeserializeObject<Update>(message);
                        userMap[socket] = new User() { Name = update.name };
                        pendingCommands.TryAdd(new CommandPending(commandID, new User(), update));
                        commandMap.Add(commandID++, socket);
                    } catch (Exception ex) {
                        Console.WriteLine(ex);
                    }
                };
            });

            CommandResult result;
            while (commandResults.TryTake(out result)) {
                IWebSocketConnection socket;
                if (commandMap.TryGetValue(result.Id, out socket)) {
                    Send(socket, result);
                    commandMap.Remove(result.Id);
                }
            }
        }
开发者ID:timothypratley,项目名称:locstream,代码行数:51,代码来源:Listener.cs

示例2: GenerateColors

 private static void GenerateColors(BlockingCollection<Color> colors)
 {
     while (true)
     {
         colors.TryAdd(ColorTranslator.FromHtml(string.Format("#{0:X6}", GetNumber())));
     }
 }
开发者ID:dev4s,项目名称:2013-photogenerator,代码行数:7,代码来源:Bitmaps.cs

示例3: NonBlockingProducer

        static void NonBlockingProducer(BlockingCollection<int> bc)
        {
            int itemToAdd = 0;
            bool success = false;

            do
            {
                // A shorter timeout causes more failures.
                success = bc.TryAdd(itemToAdd, 2);

                if (success)
                {
                    Console.WriteLine(" Add:{0}", itemToAdd);
                    itemToAdd++;
                }
                else
                {
                    Console.Write(" AddBlocked:{0} Count = {1} ", itemToAdd.ToString(), bc.Count);
                    // Don't increment nextItem. Try again on next iteration.

                    //Do something else useful instead.
                    UpdateProgress(itemToAdd);
                }

            } while (itemToAdd < inputs);

            // No lock required here because only one producer.
            bc.CompleteAdding();
        }
开发者ID:johnmmoss,项目名称:ProducerConsumerSample,代码行数:29,代码来源:Program.cs

示例4: InternalCancellation_CompleteAdding_Negative

        public static void InternalCancellation_CompleteAdding_Negative()
        {
            BlockingCollection<int> coll1 = new BlockingCollection<int>();

            Task.Run(() => coll1.CompleteAdding());
            //call Take.. it should wake up with an OCE. when CompleteAdding() is called.
            Assert.Throws<InvalidOperationException>(() => coll1.Take());
            // "InternalCancellation_WakingUpTake:  an IOE should be thrown if CompleteAdding occurs during blocking Take()");
            Assert.Throws<InvalidOperationException>(() => coll1.Add(1));
            // "InternalCancellation_WakingUpAdd:  an InvalidOpEx should be thrown if CompleteAdding occurs during blocking Add()");
            Assert.Throws<InvalidOperationException>(() => coll1.TryAdd(1, 1000000));  //an indefinite wait to add.. 1000 seconds.
            // "InternalCancellation_WakingUpTryAdd:  an InvalidOpEx should be thrown if CompleteAdding occurs during blocking Add()");
        }
开发者ID:noahfalk,项目名称:corefx,代码行数:13,代码来源:BlockingCollectionCancellationTests.cs

示例5: ProduceBitmap

        private static void ProduceBitmap(BlockingCollection<Color> colors, BlockingCollection<Bitmap> bitmaps, BlockingCollection<Point> points)
        {
            double counter = 0;

            while (true)
            {
                var b = new Bitmap(Global.ImageX, Global.ImageY);

                foreach (var p in points)
                {
                    Color color;
                    colors.TryTake(out color, Timeout.Infinite);

                    b.SetPixel(p.X, p.Y, color);
                }

                bitmaps.TryAdd(b, Timeout.Infinite);
                Console.WriteLine("Utworzono plik graficzny nr.: {0}", ++counter);
            }
        }
开发者ID:dev4s,项目名称:2013-photogenerator,代码行数:20,代码来源:Bitmaps.cs

示例6: ExternalCancel_Negative

        public static void ExternalCancel_Negative()
        {
            BlockingCollection<int> bc = new BlockingCollection<int>(); //empty collection.

            CancellationTokenSource cs = new CancellationTokenSource();
            Task.Run(() => cs.Cancel());

            int item;
            EnsureOperationCanceledExceptionThrown(
                () => bc.Take(cs.Token), cs.Token,
                "ExternalCancel_Take:  The operation should wake up via token cancellation.");
            EnsureOperationCanceledExceptionThrown(
               () => bc.TryTake(out item, 100000, cs.Token), cs.Token,
                "ExternalCancel_TryTake:  The operation should wake up via token cancellation.");
            EnsureOperationCanceledExceptionThrown(
                () => bc.Add(1, cs.Token), cs.Token,
                "ExternalCancel_Add:  The operation should wake up via token cancellation.");
            EnsureOperationCanceledExceptionThrown(
                () => bc.TryAdd(1, 100000, cs.Token), // a long timeout.
                cs.Token,
                "ExternalCancel_TryAdd:  The operation should wake up via token cancellation.");

            BlockingCollection<int> bc1 = new BlockingCollection<int>(1);
            BlockingCollection<int> bc2 = new BlockingCollection<int>(1);
            bc1.Add(1); //fill the bc.
            bc2.Add(1); //fill the bc.
            EnsureOperationCanceledExceptionThrown(
                () => BlockingCollection<int>.AddToAny(new[] { bc1, bc2 }, 1, cs.Token),
                cs.Token,
                "ExternalCancel_AddToAny:  The operation should wake up via token cancellation.");
            EnsureOperationCanceledExceptionThrown(
               () => BlockingCollection<int>.TryAddToAny(new[] { bc1, bc2 }, 1, 10000, cs.Token),
               cs.Token,
               "ExternalCancel_AddToAny:  The operation should wake up via token cancellation.");

            IEnumerable<int> enumerable = bc.GetConsumingEnumerable(cs.Token);
            EnsureOperationCanceledExceptionThrown(
               () => enumerable.GetEnumerator().MoveNext(),
               cs.Token, "ExternalCancel_GetConsumingEnumerable:  The operation should wake up via token cancellation.");
        }
开发者ID:noahfalk,项目名称:corefx,代码行数:40,代码来源:BlockingCollectionCancellationTests.cs

示例7: NotifySiblings

        private void NotifySiblings()
        {
            var notifications = new BlockingCollection<RavenConnectionStringOptions>();

            Task.Factory.StartNew(() => NotifySibling(notifications));

            int skip = 0;
            var replicationDestinations = GetReplicationDestinations();
            foreach (var replicationDestination in replicationDestinations)
            {
                notifications.TryAdd(replicationDestination.ConnectionStringOptions, 15 * 1000);
            }

            while (true)
            {
                int nextPageStart = skip; // will trigger rapid pagination
                var docs = docDb.Documents.GetDocumentsWithIdStartingWith(Constants.RavenReplicationSourcesBasePath, null, null, skip, 128, CancellationToken.None, ref nextPageStart);
                if (docs.Length == 0)
                {
                    notifications.TryAdd(null, 15 * 1000); // marker to stop notify this
                    return;
                }

                skip += docs.Length;

                foreach (RavenJObject doc in docs)
                {
                    var sourceReplicationInformation = doc.JsonDeserialization<SourceReplicationInformation>();
                    if (string.IsNullOrEmpty(sourceReplicationInformation.Source))
                        continue;

                    var match = replicationDestinations.FirstOrDefault(x =>
                                                           string.Equals(x.ConnectionStringOptions.Url,
                                                                         sourceReplicationInformation.Source,
                                                                         StringComparison.OrdinalIgnoreCase));

                    if (match != null)
                    {
                        notifications.TryAdd(match.ConnectionStringOptions, 15 * 1000);
                    }
                    else
                    {
                        notifications.TryAdd(new RavenConnectionStringOptions
                        {
                            Url = sourceReplicationInformation.Source
                        }, 15 * 1000);
                    }
                }
            }
        }
开发者ID:j2jensen,项目名称:ravendb,代码行数:50,代码来源:ReplicationTask.cs

示例8: ProcessFiles

        /// <summary>
        ///   Process files (get fingerprint signatures, hash them into storage)
        /// </summary>
        /// <param name = "files">List of files to be hashed</param>
        /// <param name = "processed">Callback invoked once 1 track is processed</param>
        /// <returns>List of processed tracks</returns>
        private List<TrackData> ProcessFiles(IEnumerable<string> files, Action<TrackData> processed)
        {
            /*preprocessing stage ended, now make sure to do the actual job*/

            int numProcs = Environment.ProcessorCount;

            // 1024 (Kb) * BufferSize / SampleRate * SecondsRead * 4 (1 float = 4 bytes) / 1024 (Kb)
            const int Buffersize =
                (int)((1024.0 * BufferSize) / ((double)SampleRate * SecondsToProcess / 1000 * 4 / 1024));

            // ~317 songs are allowed for 15 seconds snippet at 5512 Hz sample rate
            var buffer = new BlockingCollection<Tuple<TrackData, float[]>>(Buffersize);
            var processedtracks = new List<TrackData>();
            var consumers = new List<Task>();
            var producers = new List<Task>();
            CancellationToken token = cts.Token;
            var bag = new ConcurrentBag<string>(files);

            int maxprod = numProcs > 2 ? 2 : numProcs;
            for (var i = 0; i < maxprod; i++)
            {
                /*producers*/
                producers.Add(Task.Factory.StartNew(
                    () =>
                    {
                        while (!bag.IsEmpty)
                            {
                                if (token.IsCancellationRequested)
                                {
                                    return;
                                }

                                string file;
                                if (!bag.TryTake(out file))
                                {
                                    return;
                                }

                                TrackData track;
                                float[] samples;
                                try
                                {
                                    track = trackHelper.GetTrack(MinTrackLength, MaxTrackLength, file); // lame casting I know
                                    samples = trackHelper.GetTrackSamples(track, SampleRate, SecondsToProcess, StartProcessingAtSecond);
                                }
                                catch
                                {
                                    continue;
                                    /*Continue processing even if getting samples failed*/
                                    /*the failing might be caused by a bunch of File I/O factors, that cannot be considered critical*/
                                }

                                try
                                {
                                    buffer.TryAdd(new Tuple<TrackData, float[]>(track, samples), 1, token); /*producer*/
                                }
                                catch (OperationCanceledException)
                                {
                                    /*it is safe to break here, operation was canceled*/
                                    break;
                                }
                            }
                    },
                    token));
            }

            /*When all producers ended with their operations, call the CompleteAdding() to tell Consumers no more items are available*/
            Task.Factory.ContinueWhenAll(producers.ToArray(), p => buffer.CompleteAdding());

            for (int i = 0; i < numProcs * 4; i++)
            {
                /*consumer*/
                consumers.Add(Task.Factory.StartNew(
                    () =>
                    {
                        foreach (Tuple<TrackData, float[]> tuple in buffer.GetConsumingEnumerable()) /*If OCE is thrown it will be caught in the caller's AggregateException*/
                        {
                            if (tuple != null)
                            {
                                /*Long running procedure*/
                                duplicatesDetectorService.CreateInsertFingerprints(tuple.Item2, tuple.Item1);

                                processedtracks.Add(tuple.Item1);
                                if (processed != null)
                                {
                                    processed.Invoke(tuple.Item1);
                                }
                            }
                        }
                    },
                    token));
            }

            Task.WaitAll(consumers.ToArray()); /*wait for all consumers to end*/
//.........这里部分代码省略.........
开发者ID:RonAlimi,项目名称:soundfingerprinting,代码行数:101,代码来源:DuplicatesDetectorFacade.cs

示例9: CreateNodeSet

        public NodeSet CreateNodeSet(int size)
        {
            if(size > 1000)
                throw new ArgumentOutOfRangeException("size", "size should be less than 1000");
            TraceCorrelation trace = new TraceCorrelation(NodeServerTrace.Trace, "Creating node set of size " + size);
            NodeSet set = new NodeSet();
            using(trace.Open())
            {
                while(set.Count() < size)
                {
                    var peerToGet = size - set.Count();
                    var activePeers = PeerTable.GetActivePeers(1000);
                    activePeers = activePeers.Where(p => !set.Contains(p.NetworkAddress.Endpoint)).ToArray();
                    if(activePeers.Length < peerToGet)
                    {
                        DiscoverPeers(size);
                        continue;
                    }
                    NodeServerTrace.Information("Need " + peerToGet + " more nodes");

                    BlockingCollection<Node> handshakedNodes = new BlockingCollection<Node>(peerToGet);
                    CancellationTokenSource handshakedFull = new CancellationTokenSource();

                    try
                    {
                        Parallel.ForEach(activePeers,
                            new ParallelOptions()
                            {
                                MaxDegreeOfParallelism = 10,
                                CancellationToken = handshakedFull.Token
                            }, p =>
                        {
                            if(set.Contains(p.NetworkAddress.Endpoint))
                                return;
                            Node node = null;
                            try
                            {
                                node = GetNodeByPeer(p, handshakedFull.Token);
                                node.VersionHandshake(handshakedFull.Token);
                                if(node != null && node.State != NodeState.HandShaked)
                                    node.Disconnect();
                                if(!handshakedNodes.TryAdd(node))
                                {
                                    handshakedFull.Cancel();
                                    node.Disconnect();
                                }
                                else
                                {
                                    var remaining = (size - set.Count() - handshakedNodes.Count);
                                    if(remaining == 0)
                                    {
                                        handshakedFull.Cancel();
                                    }
                                    else
                                        NodeServerTrace.Information("Need " + remaining + " more nodes");
                                }
                            }
                            catch(Exception)
                            {
                                if(node != null)
                                    node.Disconnect();
                            }
                        });
                    }
                    catch(OperationCanceledException)
                    {
                    }
                    set.AddNodes(handshakedNodes.ToArray());
                }
            }
            return set;
        }
开发者ID:nikropht,项目名称:NBitcoin,代码行数:72,代码来源:NodeServer.cs

示例10: Initialize

        public async Task Initialize()
        {
            _error = null;
            _paths = new BlockingCollection<ProjectFilesClone>();
            ProjectFilesClone[] projectFilesClones = await Task.WhenAll(
                Enumerable.Range(0, _threadsCount)
                .Select(i => _fileManager.CreateCloneAsync("WhiteCache-"+i)));
            
            _filesPool = projectFilesClones.ToList();
            foreach (var projectFilesClone in _filesPool)
            {
                _paths.Add(projectFilesClone);
            }

            ProjectFilesClone filesClone = _paths.Take();

                _mainModules = Task.Run(() =>
                {
                    _referenceStrings = new List<string>();
                    var task = filesClone.Assemblies.Select(a =>
                    {
                        var cci = new CciModuleSource(a.Path);
                        cci.Guid = Guid.NewGuid();
                        _log.Debug("Whitecache#" + a.FileName + ": Created initial source: "+cci.Guid);
                        return cci;
                    }).ToList();

                    _paths.Add(filesClone);
                    return task;
                });
            

            new Thread(() =>
            {
                try
                {
                    InitializeModuleNames();


                    foreach (ProjectFilesClone item in _paths.GetConsumingEnumerable())
                    {
                        lock(this)
                        {
                            while (_whiteCaches.All(_ => _.Value.Count >= _maxCount) && !_paths.IsAddingCompleted)
                            {
                                Monitor.Wait(this);
                            }
                        }
                       
                        if(_paths.IsAddingCompleted)
                        {
                            return;
                        }
                        ProjectFilesClone item1 = item;
                        Task.Run(() => TryAdd(item1))
                            .ContinueWith(task =>
                            {
                                _paths.TryAdd(item1);
                                NotifyClients();
                            }).LogErrors();
                    }
                }
                catch (Exception e)
                {
                    _log.Error("Read assembly failed. ", e);
                    _error = e;
                    _paths.CompleteAdding();
                }

            }).Start();
        }
开发者ID:Refresh06,项目名称:visualmutator,代码行数:71,代码来源:WhiteCache.cs

示例11: NotifySiblings

		//Notifies servers which send us counters that we are back online
		private void NotifySiblings() //TODO: implement
		{
			var notifications = new BlockingCollection<RavenConnectionStringOptions>();

			Task.Factory.StartNew(() => NotifySibling(notifications));

			int skip = 0;
			var replicationDestinations = GetReplicationDestinations();
			foreach (var replicationDestination in replicationDestinations)
			{
				string lastError;
				notifications.TryAdd(GetConnectionOptionsSafe(replicationDestination, out lastError), 15 * 1000);
			}

			//TODO: add to notifications to the source server, the servers we get the replications from
		}
开发者ID:ReginaBricker,项目名称:ravendb,代码行数:17,代码来源:RavenCounterReplication.cs

示例12: InternalCancellation_WakingUpTryAdd

        //This tests that TryAdd wake up correctly if CompleteAdding() is called while the taker is waiting.
        public static bool InternalCancellation_WakingUpTryAdd()
        {
            TestHarness.TestLog("* BlockingCollectionCancellationTests.InternalCancellation_WakingUpTryAdd()");
            bool passed = true;

            BlockingCollection<int> coll1 = new BlockingCollection<int>(1);
            coll1.Add(1); //fills the collection.

            ThreadPool.QueueUserWorkItem(
                (obj) =>
                {
                    Thread.Sleep(500);
                    coll1.CompleteAdding();
                });

            passed &= TestHarnessAssert.IsFalse(coll1.IsAddingCompleted, "At this point CompleteAdding should not have occurred.");
            passed &= TestHarnessAssert.EnsureExceptionThrown(
                () => coll1.TryAdd(1, 1000000),  //an indefinite wait to add.. 1000 seconds.
                typeof(InvalidOperationException),
                "an InvalidOpEx should be thrown if CompleteAdding occurs during blocking Add()");

            return passed;
        }
开发者ID:modulexcite,项目名称:IL2JS,代码行数:24,代码来源:BlockingCollectionCancellationTests.cs

示例13: ExternalCancel_TryAdd

        public static bool ExternalCancel_TryAdd()
        {
            TestHarness.TestLog("* BlockingCollectionCancellationTests.ExternalCancel_TryAdd()");
            bool passed = true;

            BlockingCollection<int> bc = new BlockingCollection<int>(1);
            bc.Add(1); //fill the bc.

            CancellationTokenSource cs = new CancellationTokenSource();
            ThreadPool.QueueUserWorkItem(
                (obj) =>
                {
                    Thread.Sleep(100);
                    cs.Cancel();
                });

            passed &= TestHarnessAssert.IsFalse(cs.IsCancellationRequested, "At this point the cancel should not have occurred.");
            passed &= TestHarnessAssert.EnsureOperationCanceledExceptionThrown(
                () => bc.TryAdd(1, 100000, cs.Token), // a long timeout.
                cs.Token,
                "The operation should wake up via token cancellation.");

            return passed;
        }
开发者ID:modulexcite,项目名称:IL2JS,代码行数:24,代码来源:BlockingCollectionCancellationTests.cs

示例14: CreateFillQueueTask

        private static Task CreateFillQueueTask(Stream inputStream, List<string> blockIds, 
            BlockingCollection<ByteArrayWithBlockId> queue, CancellationTokenSource cts)
        {
            var fillQueueTask = Task.Run(() =>
            {
                // Put Block is limited to 4MB per block
                var buffer = new byte[OnePutBlockSizeLimitInBytes];

                try
                {
                    var blockNumber = 0;
                    while (true)
                    {
                        if (cts.IsCancellationRequested)
                            return;

                        var read = inputStream.Read(buffer, 0, buffer.Length);
                        if (read <= 0)
                            break;

                        while (read < buffer.Length)
                        {
                            var lastRead = inputStream.Read(buffer, read, buffer.Length - read);
                            if (lastRead <= 0)
                                break;
                            read += lastRead;
                        }

                        var destination = new byte[read];
                        Buffer.BlockCopy(buffer, 0, destination, 0, read);
                        var blockNumberInBytes = BitConverter.GetBytes(blockNumber++);
                        var blockIdString = Convert.ToBase64String(blockNumberInBytes);

                        blockIds.Add(blockIdString);
                        var byteArrayWithBlockId = new ByteArrayWithBlockId
                        {
                            StreamAsByteArray = destination,
                            BlockId = blockIdString
                        };

                        while (queue.TryAdd(byteArrayWithBlockId, millisecondsTimeout: 200) == false)
                        {
                            if (cts.IsCancellationRequested)
                                return;
                        }
                    }
                }
                catch (Exception)
                {
                    //if we can't read from the input stream,
                    //we need to cancel the upload tasks
                    cts.Cancel();
                    throw;
                }
                finally
                {
                    queue.CompleteAdding();
                    //we don't need the stream anymore
                    inputStream.Dispose();
                }
            });
            return fillQueueTask;
        }
开发者ID:IdanHaim,项目名称:ravendb,代码行数:63,代码来源:RavenAzureClient.cs

示例15: ProcessFiles

        /// <summary>
        ///   Process files (get fingerprint signatures, hash them into storage)
        /// </summary>
        /// <param name = "files">List of files to be hashed</param>
        /// <param name = "processed">Callback invoked once 1 track is processed</param>
        /// <returns>List of processed tracks</returns>
        private List<Track> ProcessFiles(IEnumerable<string> files, Action<Track> processed)
        {
            /*preprocessing stage ended, now make sure to do the actual job*/

            int numProcs = Environment.ProcessorCount;
            //1024 (Kb) * BufferSize / SampleRate * SecondsRead * 4 (1 float = 4 bytes) / 1024 (Kb)
            const int buffersize = (int) ((1024.0*BUFFER_SIZE)/((double) SAMPLE_RATE*MILLISECONDS_TO_PROCESS/1000*4/1024));
            //~317 songs are allowed for 15 seconds snippet at 5512 Hz sample rate
            BlockingCollection<Tuple<Track, float[]>> buffer = new BlockingCollection<Tuple<Track, float[]>>(buffersize);
            List<Track> processedtracks = new List<Track>();
            List<Task> consumers = new List<Task>();
            List<Task> producers = new List<Task>();
            CancellationToken token = _cts.Token;
            ConcurrentBag<string> bag = new ConcurrentBag<string>(files);

            int maxprod = numProcs > 2 ? 2 : numProcs;
            for (int i = 0; i < maxprod; i++) /*producers*/
            {
                producers.Add(Task.Factory.StartNew(
                    () =>
                    {
                        using (IAudio audioProxy = ServiceContainer.Kernel.Get<IAudio>())
                        {
                            while (!bag.IsEmpty)
                            {
                                if (token.IsCancellationRequested) return;
                                string file;
                                if (!bag.TryTake(out file)) return;
                                Track track;
                                float[] samples;
                                try
                                {
                                    track = TrackHelper.GetTrackInfo(MIN_TRACK_LENGTH, MAX_TRACK_LENGTH, file, (BassProxy)audioProxy); //lame casting I know
                                    samples = TrackHelper.GetTrackSamples(track, audioProxy, SAMPLE_RATE, MILLISECONDS_TO_PROCESS, MILLISECONDS_START);
                                }
                                catch
                                {
                                    continue;
                                    /*Continue processing even if getting samples failed*/
                                    /*the failing might be caused by a bunch of File I/O factors, that cannot be considered critical*/
                                }
                                try
                                {
                                    buffer.TryAdd(new Tuple<Track, float[]>(track, samples), 1, token); /*producer*/
                                }
                                catch (OperationCanceledException)
                                {
                                    /*it is safe to break here, operation was canceled*/
                                    break;
                                }
                            }
                        }
                    }, token));
            }

            /*When all producers ended with their operations, call the CompleteAdding() to tell Consumers no more items are available*/
            Task.Factory.ContinueWhenAll(producers.ToArray(), (p) => buffer.CompleteAdding());

            for (int i = 0; i < numProcs * 4; i++) /*consumer*/
            {
                consumers.Add(Task.Factory.StartNew(
                    () =>
                    {
                        foreach (Tuple<Track, float[]> tuple in buffer.GetConsumingEnumerable()) /*If OCE is thrown it will be caught in the caller's AggregateException*/
                        {
                            if (tuple != null)
                            {
                                /*Long running procedure*/
                                _repository.CreateInsertFingerprints(tuple.Item2, tuple.Item1, _createStride, NUMBER_OF_HASH_TABLES, NUMBER_OF_KEYS);

                                processedtracks.Add(tuple.Item1);
                                if (processed != null)
                                    processed.Invoke(tuple.Item1);
                            }
                        }
                    }, token));
            }
            
            Task.WaitAll(consumers.ToArray()); /*wait for all consumers to end*/
            return processedtracks;
        }
开发者ID:gvillarroel,项目名称:memoria,代码行数:87,代码来源:RepositoryGateway.cs


注:本文中的BlockingCollection.TryAdd方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。