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


C# ConcurrentQueue.Any方法代码示例

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


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

示例1: Can_Use_Any_Method_to_Determine_if_Items_Are_In_the_Queue

        public void Can_Use_Any_Method_to_Determine_if_Items_Are_In_the_Queue()
        {
            var queue = new ConcurrentQueue<string>();

            for (int i = 0; i <= 4; i++)
            {
                var msg = string.Format("I am item {0}", i);
                queue.Enqueue(msg);
            }

            Assert.AreEqual(true, queue.Any());
        }
开发者ID:Ripper555,项目名称:wcf-demo,代码行数:12,代码来源:ConcurrentQueueTests.cs

示例2: ExecuteAsParallel

        public static void ExecuteAsParallel(this IEnumerable<Action> actions)
        {
            var _exceptions = new ConcurrentQueue<Exception>();

            System.Threading.CancellationToken cts = default(System.Threading.CancellationToken);
            try
            {
                Parallel.ForEach<Action>(actions, new ParallelOptions() { CancellationToken = cts }, a =>
                                a.Invoke());
            }
            catch (AggregateException agex)
            {
                agex.InnerExceptions.ToList().ForEach(_exceptions.Enqueue);
            }

            if (_exceptions.Any())
                throw new ApplicationException(string.Format("Error: {0}", string.Join("\r\nError: ", _exceptions.Select(e => e.Message))));
        }
开发者ID:ebbarbosa,项目名称:WCFCallBack.Server,代码行数:18,代码来源:ActionsExtensions.cs

示例3: RunToCompletion

        public static Tuple<IEnumerable<ViewDefinitionCompiledArgs>, IEnumerable<CycleCompletedArgs>> RunToCompletion(IViewExecutionOptions options)
        {
            using (var remoteViewClient = Context.ViewProcessor.CreateClient())
            {
                var cycles = new ConcurrentQueue<CycleCompletedArgs>();
                var compiles = new ConcurrentQueue<ViewDefinitionCompiledArgs>();

                using (var manualResetEvent = new ManualResetEvent(false))
                {
                    var listener = new EventViewResultListener();
                    listener.ViewDefinitionCompiled += (sender, e) => compiles.Enqueue(e);
                    listener.CycleCompleted += (sender, e) => cycles.Enqueue(e);
                    listener.ProcessCompleted += (sender, e) => manualResetEvent.Set();
                    remoteViewClient.SetResultListener(listener);
                    remoteViewClient.AttachToViewProcess(ViewName, options);
                    manualResetEvent.WaitOne();
                }

                Assert.InRange(compiles.Count, cycles.Any() ? 1 : 0, cycles.Count + 1);
                Assert.True(remoteViewClient.IsCompleted);
                return new Tuple<IEnumerable<ViewDefinitionCompiledArgs>, IEnumerable<CycleCompletedArgs>>(compiles, cycles);
            }
        }
开发者ID:BietteMaxime,项目名称:OG-DotNet,代码行数:23,代码来源:RemoteViewClientBatchTests.cs

示例4: SubmitRequestsAsync

        public async Task<TimeSpan> SubmitRequestsAsync(int numRequests, int tps)
        {
            var delayBetweenRequests = TimeSpan.FromMilliseconds(1000.0/tps);

            var requestTimes = new ConcurrentQueue<TimeSpan>();
            var responseTasks = new List<Task>();
            for (int i = 0; i < numRequests; i++)
            {
                var stopwatch = Stopwatch.StartNew();
                var request = new TestRequest
                {
                    RequestNumber = i,
                    StartTime = DateTimeOffset.UtcNow
                };
                Debug.WriteLine("Submitting request {0}", i);
                var responseTask = serviceClient.SubmitRequestAsync(request, requestTimeout);
                Task continueWith = responseTask.ContinueWith(task =>
                {
                    var testResponse = task.Result;
                    var now = DateTimeOffset.UtcNow;
                    var processingTime = now - testResponse.StartTime;
                    Debug.WriteLine("Request {0} took {1}", testResponse.RequestNumber, processingTime);
                    requestTimes.Enqueue(processingTime);
                });
                responseTasks.Add(continueWith);
                responseTasks.Add(responseTask);

                stopwatch.Stop();
                var delayTime = delayBetweenRequests - stopwatch.Elapsed;

                if (delayTime > TimeSpan.Zero)
                {
                    await Task.Delay(delayTime);
                }
            }

            await Task.WhenAll(responseTasks.ToArray());

            if (!requestTimes.Any())
            {
                throw new Exception("Failed to retrieve any results. Are you sure your queue names are configured correctly?");
            }
            
            double averageMs = requestTimes.Select(t => t.TotalMilliseconds).Average();

            return TimeSpan.FromMilliseconds(averageMs);
        }
开发者ID:yonglehou,项目名称:AzureDistributedService,代码行数:47,代码来源:TestServiceQueueSubmitter.cs

示例5: SubmitRequestsAsync

        public async Task<TimeSpan> SubmitRequestsAsync(int numRequests, int tps)
        {
            var delayBetweenRequests = TimeSpan.FromMilliseconds(1000.0 / tps);

            var requestTimes = new ConcurrentQueue<TimeSpan>();
            var responseTasks = new List<Task>();
            using (var client = new HttpClient())
            {
                client.BaseAddress = serviceUri;
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                for (int i = 0; i < numRequests; i++)
                {
                    var stopwatch = Stopwatch.StartNew();
                    var request = new TestRequest
                    {
                        RequestNumber = i,
                        StartTime = DateTimeOffset.UtcNow
                    };
                    Debug.WriteLine("Submitting request {0}", i);

                    // New code:
                    Task<HttpResponseMessage> responseTask = client.PostAsJsonAsync("api/service/", request);

                    Task continueWith = responseTask.ContinueWith(
                        async task =>
                        {
                            var response = task.Result;
                            if (response.IsSuccessStatusCode)
                            {
                                var testResponse = await response.Content.ReadAsAsync<TestResponse>();
                                var now = DateTimeOffset.UtcNow;
                                var processingTime = now - testResponse.StartTime;
                                Debug.WriteLine("Request {0} took {1}", testResponse.RequestNumber, processingTime);
                                requestTimes.Enqueue(processingTime);
                            }
                        });
                    responseTasks.Add(continueWith);
                    responseTasks.Add(responseTask);

                    stopwatch.Stop();
                    var delayTime = delayBetweenRequests - stopwatch.Elapsed;

                    if (delayTime > TimeSpan.Zero)
                    {
                        await Task.Delay(delayTime);
                    }
                }

                await Task.WhenAll(responseTasks.ToArray());
            }

            if (!requestTimes.Any())
            {
                throw new Exception("Failed to retrieve any results. Are you sure your queue names are configured correctly?");
            }

            double averageMs = requestTimes.Select(t => t.TotalMilliseconds).Average();

            return TimeSpan.FromMilliseconds(averageMs);                
        }
开发者ID:yonglehou,项目名称:AzureDistributedService,代码行数:62,代码来源:TestWebApiSubmitter.cs

示例6: SendOutgoingMessages

        async Task SendOutgoingMessages(ConcurrentQueue<OutgoingMessage> outgoingMessages, ITransactionContext context)
        {
            if (!outgoingMessages.Any()) return;

            var client = GetClientFromTransactionContext(context);

            var messagesByDestination = outgoingMessages
                .GroupBy(m => m.DestinationAddress)
                .ToList();

            await Task.WhenAll(
                messagesByDestination
                    .Select(async batch =>
                    {
                        var entries = batch
                            .Select(message =>
                            {
                                var transportMessage = message.TransportMessage;

                                var headers = transportMessage.Headers;

                                return new SendMessageBatchRequestEntry
                                {
                                    Id = headers[Headers.MessageId],
                                    MessageBody = GetBody(transportMessage.Body),
                                    MessageAttributes = CreateAttributesFromHeaders(headers),
                                    DelaySeconds = GetDelaySeconds(headers)
                                };
                            })
                            .ToList();

                        var destinationUrl = GetDestinationQueueUrlByName(batch.Key, context);

                        var request = new SendMessageBatchRequest(destinationUrl, entries);

                        var response = await client.SendMessageBatchAsync(request);

                        if (response.Failed.Any())
                        {
                            var failed = response.Failed.Select(f => new AmazonSQSException($"Failed {f.Message} with Id={f.Id}, Code={f.Code}, SenderFault={f.SenderFault}"));

                            throw new AggregateException(failed);
                        }
                    })

                );
        }
开发者ID:RichieYang,项目名称:Rebus,代码行数:47,代码来源:AmazonSQSTransport.cs

示例7: ProcessCombinations

        List<Combination> ProcessCombinations(List<Combination> combinations, int threadsCount)
        {
            combinations = combinations.Take(CombinationsToProcess).ToList();

            ProcessedCombinationsResult existingResult = null;
            object existingObjResult = null;

            if (Core.MsCache.TryGet(CacheKey, ref existingObjResult))
                existingResult = (ProcessedCombinationsResult)existingObjResult;

            List<Combination> existingCaseSet = null;
            if (existingResult != null && existingResult.ThreadCount == threadsCount)
            {
                existingCaseSet = existingResult.ProcessedCombinations;
            }
            else
            {
                Core.MsCache.TryRemove(CacheKey);
            }

            if (existingCaseSet != null)
                return existingCaseSet;

            // Use ConcurrentQueue to enable safe enqueueing from multiple threads.
            var exceptions = new ConcurrentQueue<Exception>();

            var fbClient = FogBugzGateway.GetClientForParallel();

            Func<object, int> action = (object obj) =>
            {
                Combination combo = obj as Combination;
                //System.Diagnostics.Debug.WriteLine("{0} Thread: {1} Project: {2}, Milestone: {3}, StartAction",
                //										DateTime.Now.ToString("hh:mm:ss"), Thread.CurrentThread.ManagedThreadId,
                //										combo.ProjectName, combo.MilestoneName);

                var cases = FogBugzGateway.GetCases(combo.ProjectId.Value, combo.MilestoneId.Value, null, 1, fbClient);

                Core.MsCache.Set(ListProgressStatusKey + "_" + _cacheKey, new ProgressStatusInfo { Value = 33, Label = String.Format("Checking {0} {1} for tasks {2} of {3}", combo.ProjectName, combo.MilestoneName, combinations.IndexOf(combo) + 1, combinations.Count) });

                combo.HasCases = cases.Any(c => !c.DateClosed.HasValue);

                //System.Diagnostics.Debug.WriteLine("{0} Thread: {1} Project: {2}, Milestone: {3}, HasCases={4}",
                //										DateTime.Now.ToString("hh:mm:ss"), Thread.CurrentThread.ManagedThreadId,
                //										combo.ProjectName, combo.MilestoneName, combo.HasCases);
                return 0;
            };

            var factory = new TaskFactory();

            int step = 0;

            var combinationsPerStep = threadsCount;

            while (true)
            {
                //Console.WriteLine("ProcessCominations Step " + step);
                var stepCombinations = combinations.Skip(step * combinationsPerStep).Take(combinationsPerStep).ToArray();

                if (!stepCombinations.Any()) break;

                var tasks = new Task<int>[stepCombinations.Count()];

                for (int i = 0; i < stepCombinations.Count(); i++)
                {
                    tasks[i] = factory.StartNew(action, stepCombinations[i]);
                }

                //Exceptions thrown by tasks will be propagated to the main thread
                //while it waits for the tasks. The actual exceptions will be wrapped in AggregateException.

                Task.WaitAll(tasks);

                if (combinations.Count(c => c.HasCases) >= CombinationsToFind || stepCombinations.Count() < combinationsPerStep)
                    break;

                step++;
            }

            if (exceptions.Any())
            {
                throw new AggregateException(exceptions);
            }

            // return legal combinations
            var result = combinations.Where(c => c.HasCases) //combinations with at least one task
                //.Where(c => (c.MilestoneEndDate ?? DateTime.Now) > DateTime.Now) //moved to process method
                                     .OrderBy(c => c.MilestoneEndDate) //Sort by Milestone End Date in future
                                     .Take(CombinationsToFind) //Get top 20
                                     .ToList();

            Core.MsCache.Set(CacheKey,
                                 new ProcessedCombinationsResult { ProcessedCombinations = result, ThreadCount = threadsCount },
                                 new TimeSpan(1, 0, 0));

            return result;
        }
开发者ID:nagyistoce,项目名称:FogBugz-Project-Dashboard,代码行数:96,代码来源:ProjectMilestoneList.cs

示例8: InsertData

        public static void InsertData(DataSet dataSet)
        {
            if (dataSet == null)
                throw new ArgumentNullException("DataSet param is null");

            try
            {
                //ConcurrentDictionary<string, object> cache = new ConcurrentDictionary<string, object>();
                ICacheManager cache = new MemoryCacheManager();

                var exceptions = new ConcurrentQueue<Exception>();
                List<Task> tasks = new List<Task>();
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();
                using (var container = new ModelsContainer())
                {
                    foreach (DataTable dt in dataSet.Tables)
                    {
                        //Task task = Task.Factory.StartNew(() =>
                        //{
                        try
                        {
                            Console.WriteLine("----------Table name is : {0}---------", dt.TableName);
                            int cursor = 0;
                            foreach (DataRow dr in dt.Rows)
                            {
                                Record record = new Record();

                                string brandsName = dr[0].ToString();
                                var brands = cache.Get<Brands>(brandsName, () =>
                                {
                                    return new Brands() { Name = brandsName };
                                });
                                record.Brands = brands;

                                string modelsName = dr[1].ToString();
                                var models = cache.Get<Models>(modelsName, () =>
                                {
                                    return new Models() { Name = modelsName };
                                });
                                record.Models = models;

                                record.City = dr[2].ToString();
                                string dv = dr[3].ToString().Replace(".", "");
                                string d = string.Format("{0}-{1}-01", dv.Substring(0, 4), dv.Substring(4, 2)).Trim();
                                var buyYear = cache.Get<BuyYear>(d, () =>
                                {
                                    return new BuyYear() { Time = Convert.ToDateTime(d) };
                                });
                                record.BuyYear = buyYear;

                                d = string.Format("{0}-01-01", dr[4].ToString());
                                record.Both = DateTime.Parse(d);
                                bool g = dr[5].ToString().Equals("男") ? true : false;
                                record.Gender = Convert.ToBoolean(g);
                                record.Address = dr[6].ToString();
                                record.Zip = dr[7].ToString();

                                container.Set<Record>().Add(record);
                                Console.WriteLine("address {0}, cursor = {1}, threadId = {2}", record.Address, cursor, Thread.CurrentThread.ManagedThreadId);
                                cursor++;
                                if (cursor == 100)
                                {
                                    cursor = 0;
                                    container.SaveChanges();
                                }
                            }

                        }
                        catch (Exception ex)
                        {
                            exceptions.Enqueue(ex);
                        }
                        //});
                        //tasks.Add(task);
                        container.SaveChanges();
                    }
                }

                //Task.WaitAll(tasks.ToArray());

                stopwatch.Stop();
                TimeSpan ts = stopwatch.Elapsed;
                string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                ts.Hours, ts.Minutes, ts.Seconds,
                ts.Milliseconds / 10);
                Console.WriteLine("RunTime " + elapsedTime);
                if (exceptions.Any())
                {
                    Console.WriteLine("Parallel have exceptions, count = {0}", exceptions.Count());
                }
            }
            catch (Exception ex)
            {
                string msg = ex.OutputMessage();
                Console.WriteLine("{0}", msg);
            }
        }
开发者ID:shfanfei,项目名称:CarMatrix,代码行数:98,代码来源:DbOperator.cs


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