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


C# Queue.Aggregate方法代码示例

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


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

示例1: Run

        public void Run()
        {
            var backupDirectory = fs.GetDirectory(@"D:\!saved");

            var dt = new DateTime(2010, 9, 3);
            var dirSpec = Spec<IDirectory>.Create(d => d.LastWriteTimeUtc() > dt);

            var fileSpec = Spec<IFile>.Create(f => f.LastModifiedTimeUtc > dt)
                .And(f => f.Extension == ".mp3");

            var files = fs.GetDirectory(@"D:\!music")
                .Directories()
                .Where(dirSpec.IsSatisfied)
                .SelectMany(d => d.Files("*", SearchScope.SubFolders)
                                     .Where(fileSpec.IsSatisfied))
                .ToList();

            foreach (var file in files)
            {
                var pathQueue = new Queue<string>(file.Path.Segments);

                pathQueue.Dequeue();
                pathQueue.Dequeue();

                var remainingPath = pathQueue.Aggregate("", (p, c) => string.Concat(p, c, "/")).TrimEnd('/');

                var newPath = Path.Combine(backupDirectory.Path.FullPath, remainingPath);

                var dest = fs.GetFile(newPath);

                file.CopyTo(dest);
            }
            //            const string PATH = @"C:\Users\cmartin.NTSPHX1\Downloads\phonecalls";
            //
            //            var minutes = new VerizonMinutesReader(new[]
            //            {
            //                fs.GetFile(Path.Combine(PATH, "6023265200.csv")),
            //                fs.GetFile(Path.Combine(PATH, "6025096418.csv")),
            //                fs.GetFile(Path.Combine(PATH, "6025096464.csv")),
            //                fs.GetFile(Path.Combine(PATH, "6025097062.csv")),
            //            });
            //
            //            ISpec<VerizonRecord> spec = VerizonRecord.NumberIsNot("Misty")
            //                .And(VerizonRecord.NotTollFree)
            //                .And(VerizonRecord.MinutesGreaterThan(9));
            //
            //            foreach (VerizonRecord record in minutes.GetFriendsAndFamilyRecommendations(spec))
            //            {
            //                log.Info(record);
            //            }
            //
            //            foreach (VerizonRecord record in minutes.GroupByNumber())
            //            {
            //                log.Info(record);
            //            }
        }
开发者ID:paveltimofeev,项目名称:Caliberweb-Core,代码行数:56,代码来源:Runner.cs

示例2: Main


//.........这里部分代码省略.........
                                    lock(pool) {
                                        if(pool == myPool) {
                                            Console.WriteLine("{0}: creating new pool", workerInfo.Id);
                                            Interlocked.Exchange(ref poolUseCount, 0);
                                            pool = ConnectionPool.Create(host, port);
                                            pool.MaxConnections = poolSize;
                                            myPool = pool;
                                            oldPoolUse = 0;
                                        }
                                    }
                                } else if(myPool != pool) {
                                    oldPoolUse++;
                                    if(oldPoolUse > maxOldPoolIterations) {
                                        myPool = pool;
                                        oldPoolUse = 0;
                                    }
                                }
                                using(var client = new ClacksClient(myPool)) {
                                    for(var k = 0; k < 50; k++) {
                                        workerInfo.Status = WorkerStatus.GeneratingPayload;
                                        var payload = new StringBuilder();
                                        payload.AppendFormat("id:{0},data:", workerInfo.Id);
                                        var size = r.Next(2, 100);
                                        for(var l = 0; l < size; l++) {
                                            payload.Append(Guid.NewGuid().ToString());
                                        }
                                        var data = payload.ToString();
                                        var bytes = Encoding.ASCII.GetBytes(data);
                                        workerInfo.Status = WorkerStatus.MakingRequest;
                                        var response = client.Exec(new Client.Request("BIN").WithArgument(workerInfo.Id).WithData(bytes).ExpectData("OK"));
                                        workerInfo.Status = WorkerStatus.CheckingResponse;
                                        if(response.Status != "OK") {
                                            throw new Exception("wrong status: " + response.Status);
                                        }
                                        if(response.Arguments.Length != 1) {
                                            throw new Exception("wrong arg length: " + response.Arguments.Length);
                                        }
                                        var responseData = Encoding.ASCII.GetString(response.Data);
                                        if(data != responseData) {
                                            lock(workers) {
                                                Console.WriteLine("bad data response for worker {0}", workerInfo.Id);
                                                Console.WriteLine("sent:     {0}", data);
                                                Console.WriteLine("received: {0}", responseData);
                                            }
                                        }
                                        Interlocked.Increment(ref workerInfo.Requests);
                                    }
                                }
                                workerInfo.Status = WorkerStatus.Sleeping;
                                Thread.Sleep(sleepInterval);
                            } catch(Exception e) {
                                workerInfo.Status = WorkerStatus.Failed;
                                Console.WriteLine("{0} failed: [{1}] {2}", workerInfo.Id, e.GetType(), e.Message);
                                Thread.Sleep(sleepInterval);
                            }
                        }
                    });
                    tasks.Add(workerInfo.Task);
                }
                Console.WriteLine("waiting for workers to get ready");
                WaitHandle.WaitAll(readySignals.ToArray());
                startSignal.Set();
                Console.WriteLine("starting work");
            }
            var t = Stopwatch.StartNew();
            Thread.Sleep(TimeSpan.FromMinutes(1));
            var lastRequests = workers.ToDictionary(k => k.Id, v => v.Requests);
            var requests = new Queue<Tuple<int, TimeSpan>>();
            while(true) {
                if(workerCount > 0) {
                    var total = workers.Sum(x => x.Requests) - lastRequests.Values.Sum();
                    requests.Enqueue(new Tuple<int, TimeSpan>(total, t.Elapsed));
                    if(requests.Count > 5) {
                        requests.Dequeue();
                    }
                    var rate = requests.Aggregate((a, b) => new Tuple<int, TimeSpan>(a.Item1 + b.Item1, a.Item2 + b.Item2));
                    Console.WriteLine("{0} Executing requests at {1:0} requests/second",
                                      DateTime.Now,
                                      rate.Item1 / rate.Item2.TotalSeconds
                        );
                    foreach(var stuckGroup in from worker in workers
                                              where worker.Requests == lastRequests[worker.Id]
                                              let x = new { worker.Id, worker.Requests, worker.Status }
                                              group x by x.Status
                                                  into xg
                                                  select new { Status = xg.Key, Workers = xg }) {
                        Console.WriteLine("  {0} workers stuck in '{1}': {2}",
                                      stuckGroup.Workers.Count(),
                                      stuckGroup.Status,
                                      string.Join(", ", from worker in stuckGroup.Workers orderby worker.Id select string.Format("{0} ({1})", worker.Id, worker.Requests))
                            );
                    }
                    foreach(var worker in workers) {
                        lastRequests[worker.Id] = worker.Requests;
                    }
                    t.Restart();
                }
                Thread.Sleep(TimeSpan.FromMinutes(1));
            }
        }
开发者ID:MindTouch,项目名称:MindTouch.Clacks,代码行数:101,代码来源:Program.cs

示例3: GetLast20

        /// <summary>
        /// Retrives the lastest 20 logs
        /// </summary>
        /// <returns>The latest 20 logs</returns>
        public static string GetLast20()
        {
            var q = new Queue<String>();

            for (var i = 0; i < 20; i++)
                q.Enqueue("");

            //Super line to fix all the problems (Totally not an XP thing btw)
            if (!File.Exists(LogFilePath))
                return "";

            using (var sr = new StreamReader(LogFilePath))
            {
                while (sr.Peek() != -1)
                {
                    q.Dequeue();
                    q.Enqueue(sr.ReadLine() + "\n");
                }
            }

            return q.Aggregate("", (current, s) => current + s);
        }
开发者ID:rwlamont,项目名称:AllItUp,代码行数:26,代码来源:EventLogger.cs


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