/// <summary>
 /// clears all streams and forces the underlying carrier streams to flush
 /// </summary>
 public override void Flush()
 {
     _logger.Debug("Flushing");
     _carrierStreams
     .AsParallel()
     .ForAll(s => s.CarrierStream.Flush());
 }
Пример #2
0
        static void Main(string[] args)
        {
            ThreadPool.SetMaxThreads(100, 100);

            ConcurrentBag <Minah> minahs = new ConcurrentBag <Minah>();
            Stopwatch             ti     = new Stopwatch();

            ti.Start();

            Task.Factory.StartNew(() => {
                for (int i = 1; i < 2501; i++)
                {
                    minahs.Add(new Minah(i));
                }
                ;
            });

            Task.Factory.StartNew(() => {
                for (int i = 1; i < 2501; i++)
                {
                    minahs.Add(new Minah(i));
                }
                ;
            });

            Task.Factory.StartNew(() => {
                for (int i = 1; i < 2501; i++)
                {
                    minahs.Add(new Minah(i));
                }
                ;
            });

            Task.Factory.StartNew(() => {
                for (int i = 1; i < 2501; i++)
                {
                    minahs.Add(new Minah(i));
                }
                ;
            });

            Task.WaitAll();

            string[] alpha = { "Alpha", "Bravo", "Charlie", "Delta", "Eagle", "Foxtrot", "Gulf", "Hotel" };

            foreach (string s in alpha)
            {
                Console.WriteLine(s);
                Task.Factory.StartNew(() => minahs.AsParallel().ForAll(m => m.RepeatWord = s)).Wait();
            }

            minahs.AsParallel().ForAll(m => m.s.OnCompleted());

            ti.Stop();

            Console.WriteLine("{1} birds : {0} seconds", ti.Elapsed.TotalSeconds, minahs.Count);
            Console.ReadLine();
        }
Пример #3
0
        private static void ReleasePreviousFlashlights()
        {
            if (FlashlightCtsCollection.Any())
            {
                FlashlightCtsCollection.AsParallel()
                .ForAll(_ => _.Cancel());

                FlashlightCtsCollection.Clear();
            }
        }
Пример #4
0
 private void cbShowEnableIp_CheckedChanged(object sender, EventArgs e)
 {
     if (cbShowEnableIp.Checked)
     {
         ipStateBindingList.Clear();
         ipStateBindingList.Add("IP地址              " + "    端口       " + "  端口状态 " + "             服务");
         ipStateBindingList = (BindingList <string>)(ipStateList.AsParallel().Where(x => x.IsConnected == true).OrderBy(x => x.IpAddress).ThenBy(x => x.Port).Select(x => string.Format("{0}                  {1}         {2}              {3}", x.IpAddress, x.Port, x.IsConnected ? "open" : "close", x.ServiceName)).AsEnumerable());
     }
     else
     {
         ipStateBindingList.Clear();
         ipStateBindingList.Add("IP地址              " + "    端口       " + "  端口状态 " + "             服务");
         ipStateBindingList = (BindingList <string>)(ipStateList.AsParallel().OrderBy(x => x.IpAddress).ThenBy(x => x.Port).Select(x => string.Format("{0}                  {1}         {2}              {3}", x.IpAddress, x.Port, x.IsConnected ? "open" : "close", x.ServiceName)).AsEnumerable());
     }
 }
Пример #5
0
 private static ValidationResult GetBestResult(ConcurrentBag <ValidationResult> validations)
 {
     return(validations
            .AsParallel()
            .OrderByDescending(res => res.F1Score)
            .First());
 }
Пример #6
0
 /// <summary>
 /// find a show
 /// </summary>
 /// <param name="showId"></param>
 /// <param name="checkCache"></param>
 /// <returns></returns>
 private Show FindShow(int showId, bool checkCache = true)
 {
     if (showId <= 0)
     {
         return(null);
     }
     if (checkCache)
     {
         var result = (from show in _Cache.AsParallel()
                       where show.ShowId == showId
                       select show).FirstOrDefault();
         if (result != null)
         {
             return(result);
         }
     }
     try
     {
         var document = XDocument.Load(SHOW_LOOKUP + showId);
         var show     = new Show(showId, document.Element("Show"));
         _Cache.Add(show);
         return(show);
     }
     catch (XmlException)
     {
     }
     return(null);
 }
Пример #7
0
        public static async Task <IEnumerable <string> > ConcurrentTaskTreeTraverseInParallelAsync(string root, string searchPattern)
        {
            var sw           = Stopwatch.StartNew();
            var taskBag      = new ConcurrentBag <Task>();
            var fileNameBags = new ConcurrentBag <ConcurrentBag <string> >();

            var directoryQueue = new ConcurrentQueue <ConcurrentQueue <string> >();
            var directory      = new ConcurrentQueue <string>();

            directory.Enqueue(root);
            directoryQueue.Enqueue(directory);

            while (directoryQueue.Count > 0)
            {
                if (directoryQueue.TryDequeue(out ConcurrentQueue <string> dirs))
                {
                    await dirs.DequeueExisting().ParallelForEachAsync(async dir =>
                    {
                        await GetDirectoryQueuesAsync(dir, directoryQueue).ConfigureAwait(false);
                        taskBag.Add(GetFileNamesAsync(dir, searchPattern, fileNameBags));
                    });
                }
            }

            sw.Stop();

            return(fileNameBags.AsParallel().SelectMany(f => f));
        }
Пример #8
0
        public async Task ProcessAlertsAsync()
        {
            if (!IsInProgress)
            {
                var listOfAlerts = new Dictionary <long, DateTime>();
                IsInProgress = true;

                try
                {
                    _alerts.AsParallel().ForEach(async alert =>
                    {
                        int hourValue  = GetHourPeriod(alert);
                        double hourGap = DateTime.UtcNow.Subtract(alert.LastExecutionTime).TotalHours;
                        if (hourGap >= hourValue)
                        {
                            await SendAlertFor(alert);
                            listOfAlerts.Add(alert.Id, DateTime.UtcNow);
                        }
                    });

                    await UpdateLastExecTimes(listOfAlerts);
                }
                catch (Exception ex)
                {
                    Logger.LogError(ex);
                }
                finally
                {
                    IsInProgress = false;
                }
            }
        }
Пример #9
0
        /// <summary>
        ///     Create Palylist with selected test cases
        /// </summary>
        /// <param name="selectedTcId"></param>
        /// <param name="fileLocation"></param>
        public static void CreatePlaylist(List <int> selectedTcId, string fileLocation)
        {
            Stp.Restart();
            string automatedTestName, playlistFileContent = "<Playlist Version=\"1.0\">";
            var    automatedTestList = new ConcurrentBag <string>();

            automatedTestList.AsParallel();
            Parallel.ForEach(selectedTcId, testcaseId =>
            {
                var pt            = _tfsStore.GetWorkItem(testcaseId);
                automatedTestName = pt.Fields["Automated Test Name"].Value.ToString();
                lock (automatedTestList)
                {
                    automatedTestList.Add(automatedTestName);
                }
            });
            var dedup = automatedTestList.Distinct().ToList();

            Stp.Stop();
            AutomationMethodTime = (float)Stp.ElapsedMilliseconds / 1000;
            Stp.Restart();
            playlistFileContent = dedup.Aggregate(playlistFileContent, (current, testName) => current + "<Add Test=\"" + testName + "\" />");

            playlistFileContent += "</Playlist>";
            var fs = new FileStream(fileLocation, FileMode.Create);

            using (var writer = new StreamWriter(fs))
            {
                writer.WriteLine(playlistFileContent);
            }
            fs.Dispose();
            Stp.Stop();
            AutomationPlaylistAddition = (float)Stp.ElapsedMilliseconds / 1000;
        }
Пример #10
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="this"></param>
        public static List <T> Clone <T>(this ConcurrentBag <T> @this)
        {
            var toAdd = new ConcurrentBag <T>();

            @this.AsParallel().ForAll(t => toAdd.Add(t));
            return(toAdd.ToList());
        }
Пример #11
0
        public static async Task <IEnumerable <string> > TreeTraverseAsync(string root,
                                                                           string searchPattern)
        {
            var taskBag      = new ConcurrentBag <Task>();
            var fileNameBags = new ConcurrentBag <ConcurrentBag <string> >();

            var directories = new ConcurrentQueue <string>();

            directories.Enqueue(root);

            while (directories.Count > 0)
            {
                string currentDir = string.Empty;
                if (directories.TryDequeue(out currentDir))
                {
                    await GetDirectoriesAsync(currentDir, directories).ConfigureAwait(false);

                    taskBag.Add(GetFileNamesAsync(currentDir, searchPattern, fileNameBags));
                }
            }

            await Task.WhenAll(taskBag).ConfigureAwait(false);

            return(fileNameBags.AsParallel().SelectMany(f => f));
        }
Пример #12
0
        private static decimal queryTotals(string storeId, int year, int week, string suplierName, string suplierType)
        {
            var xxx = orders55.AsParallel().AsEnumerable()
                      .Where(x => x.Key.Id == (FirstValueOrDefault(storeId, x.Key.Id)))
                      .Where(x => x.Key.DateF.Year == (FirstValueOrDefault(year, x.Key.DateF.Year)))
                      .Where(x => x.Key.DateF.Week == (FirstValueOrDefault(week, x.Key.DateF.Week)))
            ;

            ConcurrentBag <decimal> sumTotal = new ConcurrentBag <decimal>();

            ////Parallel.ForEach(xxx, entry =>
            //            //foreach (KeyValuePair<Index, IEnumerable<OrderDetail>> entry in xxx)
            xxx.AsParallel().ForAll(entry =>
            {
                sumTotal.Add(
                    entry.Value/*.IeList*/  /*.AsParallel()*/
                    .Where(z => z.SuplierName == (FirstValueOrDefault(suplierName, z.SuplierName)))
                    .Where(z => z.SuplierType == (FirstValueOrDefault(suplierType, z.SuplierType)))
                    .Select(x => x.cost)
                    .Sum()
                    )
                ;
            });

            return(sumTotal.AsParallel().Sum());
        }
Пример #13
0
        public void PutRecord(T item)
        {
            var addedIndexes = new ConcurrentBag <object>();

            _indexers.AsParallel().ForAll(x =>
            {
                var indexInfo = x.CreateIndex(item);
                if (indexInfo.Index != null)
                {
                    _index.TryAdd(indexInfo.Index, new ConcurrentBag <T>());
                    _index[indexInfo.Index].Add(item);
                    addedIndexes.Add(indexInfo.Index);
                }
            });

            var indexSet = new HashSet <object>(addedIndexes);

            _analyzers.AsParallel().ForAll(x =>
            {
                if (x.CanAnalyze(indexSet, item))
                {
                    x.Analyzer.Analyze(this, item);
                }
            });
        }
        public static int ParallelWithoutLocking(string[] file, Func <string, string[]> splitFunction)
        {
            ConcurrentBag <string> distinct = new ConcurrentBag <string>();

            System.Threading.Tasks.Parallel.ForEach(
                file,                                        // source

                () => new List <string>(),                   // local init

                (line, state, localTotal) =>                 // main body
            {
                localTotal.AddRange(splitFunction(line));
                return(localTotal);
            },

                localTotal =>                                // Add the local value
            {
                foreach (string word in localTotal)
                {
                    distinct.Add(word);
                }
            }
                );

            return(distinct.AsParallel().Distinct().Count());
        }
Пример #15
0
        public void ReduceUncovered()
        {
            print(costs);
            print(lines);
            ConcurrentBag<Cell> uncoveredCells = new ConcurrentBag<Cell>();
            Parallel.For(0, size * size, i =>
            {
                int r = (int)i / size;
                int c = i % size;
                if (lines[r][c] == 0)
                {
                    uncoveredCells.Add(new Cell(r, c, costs[r][c]));
                }
            });
            Cell minCell = uncoveredCells.AsParallel().Aggregate((cell1, cell2) => cell1.val < cell2.val ? cell1 : cell2);
            Console.WriteLine("Minimum Uncovered Cell is (" + minCell.row + "," + minCell.col + ") = " + minCell.val);

            Parallel.For(0, size, r =>
            {
                Parallel.For(0, size, c =>
                {
                    if (lines[r][c] == 0)
                    {
                        costs[r][c] -= minCell.val;
                    }
                    else if (lines[r][c] > 1)//intersection lines
                    {
                        costs[r][c] += minCell.val;
                    }
                });
            });

            //print(costs);
        }
Пример #16
0
        void DequeueBatch()
        {
            var messages = Dequeue(BatchSize);

            Task.Factory.StartNew(() =>
                                  Targets.AsParallel().ForAll(target =>
                                                              iter(messages, m => target.Receive(m))));
        }
Пример #17
0
            public IDisposable Subscribe(IObserver <string> observer)
            {
                var expireSeconds = TimeSpan.FromSeconds(10);

                Seconds.AsParallel()
                .WithDegreeOfParallelism(2)                         // Maximum working thread number in this parallel query
                .ForAll(async sec =>
                {
                    try
                    {
                        Print($"Start to process task {sec}");
                        var random = new Random((int)DateTime.Now.Ticks);

                        var completeTime = DateTime.UtcNow + TimeSpan.FromSeconds(sec);
                        var expireTime   = DateTime.UtcNow + expireSeconds;

                        bool expired = false;
                        while (!IsCompleted(completeTime, sec))
                        {
                            var delayTime = TimeSpan.FromSeconds(1 + random.Next(0, 2));

                            await Task.Delay(delayTime).ConfigureAwait(false);
                            if (DateTime.UtcNow > expireTime)
                            {
                                expired = true;
                                observer.OnNext("Expired");
                                break;
                            }
                        }

                        if (!expired)
                        {
                            observer.OnNext($"Task completed at {DateTime.Now:u}");
                        }
                    }
                    catch (Exception)
                    {
                        observer.OnNext("Expired");
                    }
                    finally
                    {
                        if (Seconds.TryTake(out sec))
                        {
                            if (Seconds.Count == 0)
                            {
                                observer.OnCompleted();
                            }
                        }
                        else
                        {
                            observer.OnError(new Exception("Unable to take"));
                        }
                    }
                });

                return(Disposable.Empty);
            }
Пример #18
0
 private static double[] GetF1Scores(ConcurrentBag <ValidationResult> validations, ValidationResult bestResult)
 {
     return(validations
            .AsParallel()
            .Where(res => res.DistanceFunc == bestResult.DistanceFunc && res.KernelFunc == bestResult.KernelFunc)
            .OrderBy(res => res.NeighborCount)
            .Select(res => res.F1Score)
            .ToArray());
 }
Пример #19
0
        private void Loop(CancellationToken token)
        {
            new Thread(() =>
            {
                while (!token.IsCancellationRequested)
                {
                    try
                    {
                        var tick = queue.DequeueAsync(token).Result;
                        logger.LogDebug("receive quote: {0}", tick);

                        quotes.AsParallel().Where(q => q.Source == tick.Symbol).ForAll(q =>
                        {
                            q.SetQuote(tick, logger);
                            if (q.Change)
                            {
                                var quoteString = q.ToUniFeederStringFormat();
                                logger.LogDebug("receive ({0} {1} {2}) => ({3} {4} {5}) use translate: {6}", tick.Symbol, tick.Bid, tick.Ask, q.Symbol, q.LastBid, q.LastAsk, q.ToStringTranslates());
                                var quoteUniFeederFormat = quoteString.ToUniFeederByteArray();
                                clients.AsParallel().ForAll(c =>
                                {
                                    try
                                    {
                                        c.Value.Send(quoteUniFeederFormat);
                                    }
                                    catch (Exception e)
                                    {
                                        logger.LogError("client: {0} error send quote: {1}", c.Key, e.Message);
                                        Task.Run(() => RemoveClient(c.Key, 5));
                                    }
                                });
                                logger.LogDebug("quote sending {0} clients", clients.Count);
                            }
                            else
                            {
                                logger.LogDebug("quote not changed therefore it is not sent by the client");
                            }
                        });
                    }
                    catch (Exception e)
                    {
                        if (e is OperationCanceledException || e.InnerException is OperationCanceledException)
                        {
                            break;
                        }
                        else
                        {
                            logger.LogError("error in queue processing: {0}", e.Message);
                        }
                    }
                }
            })
            {
                IsBackground = true
            }.Start();
        }
    static void Main(string[] args)
    {
        var list = new ConcurrentBag <string>();

        for (int i = 0; i <= 1000; i++)
        {
            list.Add(i.ToString());
        }
        var result = list.AsParallel().Where(q => Convert(q) < 500).Select(x => Convert(x)).ToList();
    }
Пример #21
0
        public void LearnParallel()
        {
            var c = new ConcurrentBag <int>();
            ParallelLoopResult result = Parallel.For(0, 1000, i => { c.Add(i); });

            c.Count.Should().Be(1000);

            var modThreeIsZero = (from num in c.AsParallel()
                                  where num % 2 == 0
                                  orderby num descending
                                  select num).ToList <int>();

            modThreeIsZero.Count.Should().Be(500);
        }
        public static int ParallelNaive(string[] file, Func <string, string[]> splitFunction)
        {
            ConcurrentBag <string> distinct = new ConcurrentBag <string>();

            System.Threading.Tasks.Parallel.ForEach(file, line =>
            {
                foreach (string s in splitFunction(line))
                {
                    distinct.Add(s);
                }
            });

            return(distinct.AsParallel().Distinct().Count());
        }
Пример #23
0
        private void UpdateRequestsItems()
        {
            if (!HasAmazonResult)
            {
                _List.AsParallel().ForAll(i => i.ResetData());
                return;
            }

            _LastReportResults.AsParallel().ForAll(r => {
                var req = _List.FirstOrDefault(i => i.Id == r.ReportRequestId);
                if (req != null)
                {
                    req.UpdateData(r);
                }
            });
        }
Пример #24
0
        public void Bfs(int start)
        {
            _vertexes[start].Value = 0;
            _currentLayer.Add(start);

            _depth = 0;
            while (_currentLayer.Any())
            {
                _currentLayer
                .AsParallel()
                .ForAll(v => BfsRoutine(_vertexes[v]));

                _currentLayer = _nextLayer;
                _nextLayer    = new ConcurrentBag <int>();

                _depth++;
            }
        }
        public void Bfs()
        {
            _currentLayer.Add(_start);

            while (_currentLayer.Any())
            {
                _currentLayer
                .AsParallel()
                .ForAll(BfsRoutine);

                _currentLayer = _nextLayer;
                _nextLayer    = new ConcurrentBag <Cell>();

                if (!_finish.NotVisited)
                {
                    return;
                }
            }
        }
Пример #26
0
 public ImgCutter(ConcurrentBag <string> imgPathQueue, int width, int height, string rootPath, string savePath = "", bool isSetWatermark = false, WatermarkTypeEnum watermarkType = WatermarkTypeEnum.Text, WatermarkPositionEnum watermarkPosition = WatermarkPositionEnum.RightButtom, string watermarkTextOrImgPath = "", int taskMaxNum = 0)
 {
     imgPathQueue.AsParallel().ForAll(imgPath =>
     {
         CuttingSettingQueue.Enqueue(new CutterSetting
         {
             CutHeight           = height,
             CutWidth            = width,
             ImgPath             = imgPath,
             RootPath            = rootPath,
             SavePath            = savePath,
             IsSetWatermark      = isSetWatermark,
             WatermarkPosition   = watermarkPosition,
             WatermarkType       = watermarkType,
             WatermarkTextOrPath = watermarkTextOrImgPath
         });
     });
     TotalCount     = CuttingSettingQueue.Count;
     TaskMaxNum     = taskMaxNum <= 0 ? Environment.ProcessorCount + 2 : taskMaxNum;
     TaskSupervisor = new TaskSupervisor(TaskMaxNum);
 }
Пример #27
0
        private void tagsTextBox_TextChanged(object sender, EventArgs e)
        {
            imagesPanel.Controls.Clear();
            if (!String.IsNullOrEmpty(tagsTextBox.Text))
            {
                var result = refToBagWithImgs.AsParallel().Where(x => x.Tags.Contains(tagsTextBox.Text));

                foreach (var x in result.Take(10))
                {
                    PictureBox imgOnScreen = new PictureBox
                    {
                        Width  = 50,
                        Height = 50,
                        Image  = Util.resizeImage(Util.BitArrayTo256x256Image(x.Image), new Size(50, 50)),
                        Tag    = x
                    };
                    imgOnScreen.MouseDown += imgMouseDown;
                    imagesPanel.Controls.Add(imgOnScreen);
                }
            }
        }
Пример #28
0
        public List <Result> Query(Query query)
        {
            if (_updateSource != null && !_updateSource.IsCancellationRequested)
            {
                _updateSource.Cancel();
                Logger.WoxDebug($"cancel init {_updateSource.Token.GetHashCode()} {Thread.CurrentThread.ManagedThreadId} {query.RawQuery}");
                _updateSource.Dispose();
            }

            var source = new CancellationTokenSource();

            _updateSource = source;
            var token = source.Token;

            var resultRaw = new ConcurrentBag <Result>();

            if (token.IsCancellationRequested)
            {
                return(new List <Result>());
            }
            Parallel.ForEach(_win32s, (program, state) =>
            {
                if (token.IsCancellationRequested)
                {
                    state.Break();
                }
                if (program.Enabled)
                {
                    var r = program.Result(query.Search, _context.API);
                    if (r != null && r.Score > 0)
                    {
                        resultRaw.Add(r);
                    }
                }
            });
            if (token.IsCancellationRequested)
            {
                return(new List <Result>());
            }
            Parallel.ForEach(_uwps, (program, state) =>
            {
                if (token.IsCancellationRequested)
                {
                    state.Break();
                }
                if (program.Enabled)
                {
                    var r = program.Result(query.Search, _context.API);
                    if (token.IsCancellationRequested)
                    {
                        state.Break();
                    }
                    if (r != null && r.Score > 0)
                    {
                        resultRaw.Add(r);
                    }
                }
            });

            if (token.IsCancellationRequested)
            {
                return(new List <Result>());
            }
            var sorted  = resultRaw.AsParallel().OrderByDescending(r => r.Score);
            var results = new List <Result>();

            foreach (var r in sorted)
            {
                if (token.IsCancellationRequested)
                {
                    return(new List <Result>());
                }
                var ignored = _settings.IgnoredSequence.Any(entry =>
                {
                    if (entry.IsRegex)
                    {
                        return(Regex.Match(r.Title, entry.EntryString).Success || Regex.Match(r.SubTitle, entry.EntryString).Success);
                    }
                    return(r.Title.ToLower().Contains(entry.EntryString) || r.SubTitle.ToLower().Contains(entry.EntryString));
                });
                if (!ignored)
                {
                    results.Add(r);
                }
                if (results.Count == 30)
                {
                    break;
                }
            }

            return(results);
        }
Пример #29
0
        private void ConvertCore(string basePath)
        {
            Logger.LogVerbose($"Reading files from {basePath}");
            HtmlTransformer(basePath);
            var manifest = JsonUtility.Deserialize <Manifest>(Path.Combine(basePath, ManifestConstants.ManifestFileName));

            if (manifest == null)
            {
                Logger.LogError("Manifest file is not found.");
                throw new FileNotFoundException("Manifest file is not found.");
            }

            if (manifest.Files == null || manifest.Files.Count == 0)
            {
                Logger.LogWarning($"There is no file in manifest under {_pdfOptions.SourceDirectory}");
                return;
            }

            var tocFiles = FindTocInManifest(manifest);

            if (tocFiles.Count > 0)
            {
                Logger.LogVerbose($"Found {tocFiles.Count} TOC.json files, will generate pdf.");
            }
            else
            {
                Logger.LogWarning("No TOC file is included, no PDF file will be generated.");
                return;
            }

            var tocHtmls = new ConcurrentBag <string>();

            IDictionary <string, PdfInformation> pdfInformations = new ConcurrentDictionary <string, PdfInformation>();

            var manifestItems    = manifest.Files.Where(f => IsType(f, ManifestItemType.Content)).ToArray();
            var manifestUrlCache = new UrlCache(basePath, manifestItems);

            Parallel.ForEach(
                tocFiles,
                new ParallelOptions {
                MaxDegreeOfParallelism = _pdfOptions.PdfConvertParallelism
            },
                tocFile =>
            {
                var tocJson    = ManifestUtility.GetRelativePath(tocFile, OutputType.TocJson);
                var tocAssetId = ManifestUtility.GetAssetId(tocFile);
                try
                {
                    Logger.LogVerbose($"Starting to handle {tocJson}.");
                    var tocFilePath     = NormalizeFilePath(tocJson);
                    var tocPageFilePath = Path.Combine(basePath, Path.GetDirectoryName(tocFilePath), TocPageFileName);
                    var tocModels       = LoadTocModels(basePath, tocFile) ?? new List <TocModel>();

                    var currentTocHtmls = new ConcurrentBag <string>();
                    var htmlModels      = BuildHtmlModels(basePath, tocModels, currentTocHtmls);

                    HtmlNotInTocTransformer(basePath, manifestUrlCache, currentTocHtmls);

                    if (_pdfOptions.GenerateAppendices)
                    {
                        currentTocHtmls.AsParallel().ForAll(tocHtmls.Add);
                    }

                    if (File.Exists(tocPageFilePath))
                    {
                        RemoveQueryStringAndBookmarkTransformer(tocPageFilePath);
                        AbsolutePathInTocPageFileTransformer(tocPageFilePath);
                        htmlModels.Insert(0, new HtmlModel {
                            Title = _pdfOptions.TocTitle, HtmlFilePath = tocPageFilePath
                        });
                    }

                    var coverPage = FindSiblingCoverPageInManifest(manifest, tocFile);
                    if (coverPage != null)
                    {
                        var coverPageHtmlRelativeFilePath = coverPage.OutputFiles[".html"].RelativePath;
                        var coverPageHtmlFilePath         = Path.Combine(basePath, coverPageHtmlRelativeFilePath);
                        if (File.Exists(coverPageHtmlFilePath))
                        {
                            htmlModels.Insert(0, new HtmlModel {
                                Title = _pdfOptions.CoverPageTitle, HtmlFilePath = coverPageHtmlFilePath
                            });
                        }
                    }

                    if (_pdfOptions.ExcludeTocs == null || _pdfOptions.ExcludeTocs.All(p => NormalizeFilePath(p) != tocFilePath))
                    {
                        var pdfNameFragments = new List <string> {
                            _pdfOptions.PdfDocsetName
                        };
                        if (!string.IsNullOrEmpty(_pdfOptions.Locale))
                        {
                            pdfNameFragments.Add(_pdfOptions.Locale);
                        }

                        // TODO: not consistent with OPS logic Path.ChangeExtension(tocAssetId, FileExtensions.PdfExtension).Replace('/', '_')
                        // TODO: need to update the logic in OPS when merging with OPS
                        if (!string.IsNullOrWhiteSpace(tocAssetId))
                        {
                            var subFolder = Path.GetDirectoryName(tocAssetId);
                            if (!string.IsNullOrEmpty(subFolder))
                            {
                                pdfNameFragments.Add(subFolder.Replace('/', '_'));
                            }
                        }

                        var pdfName = pdfNameFragments.ToDelimitedString("_") + FileExtensions.PdfExtension;
                        ConvertCore(basePath, pdfName, htmlModels);
                        Logger.LogInfo($"{pdfName} is generated based on {tocJson} under folder {_pdfOptions.DestDirectory}");
                        pdfInformations.Add(
                            pdfName,
                            new PdfInformation
                        {
                            DocsetName = _pdfOptions.PdfDocsetName,
                            TocFiles   = new string[] { tocFile.SourceRelativePath },
                            Version    = tocFile.Group,
                            AssetId    = tocAssetId,
                        });
                    }
                    else
                    {
                        Logger.LogVerbose($"Skipped to convert {tocJson} to pdf because of custom exclude tocs.");
                    }
                    Logger.LogVerbose($"Finished to handle {tocJson}.");
                }
                catch (Exception ex)
                {
                    Logger.LogError($"Error happen when converting {tocJson} to Pdf. Details: {ex.ToString()}");
                }
            });

            using (var fileStream = new FileStream(Path.Combine(_pdfOptions.DestDirectory, _pdfOptions.PdfDocsetName + FileExtensions.JsonExtension), FileMode.Create, FileAccess.Write))
            {
                using var ws = new StreamWriter(fileStream);
                JsonUtility.Serialize(ws, pdfInformations);
            }

            if (_pdfOptions.GenerateAppendices)
            {
                var otherConceptuals = ManifestHtmlsExceptTocHtmls(manifest, tocHtmls);
                if (otherConceptuals.Count > 0)
                {
                    var htmlModels = new List <HtmlModel>();
                    var htmlModel  = new HtmlModel
                    {
                        Title    = "appendices",
                        Children = otherConceptuals.Select((other, index) => new HtmlModel
                        {
                            Title        = $"Appendix {index + 1}",
                            HtmlFilePath = other
                        }).ToList()
                    };
                    htmlModels.Add(htmlModel);
                    Logger.LogVerbose("Starting to convert appendices to pdf.");
                    ConvertCore(basePath, "appendices", htmlModels);
                }
            }
            else
            {
                Logger.LogVerbose("Skipped to convert appendices to pdf.");
            }
        }
Пример #30
0
        private ConcurrentBag<cluster> binaryFilter(ConcurrentBag<cluster> listIn, char mode)
        {
            ConcurrentBag<cluster> returnList = new ConcurrentBag<cluster>();
            int amount;

            if (listIn.Count % 2 == 0) // even
                amount = (int)listIn.Count / 2;
            else //odd
                amount = (int)(listIn.Count + 1) / 2;

            switch (mode)
            {
                case 'C':
                    { //cost efficiency
                        List<cluster> sortedList = listIn.AsParallel().OrderBy(o => o.getPerformance_to_Cost_ratio()).ToList<cluster>();

                        Parallel.ForEach(sortedList, c =>
                        {
                            if (returnList.Count < amount)
                                returnList.Add(c);
                        });

                        break;
                    }

                case 'S':
                    { // space efficiency
                        List<cluster> sortedList = listIn.AsParallel().OrderBy(o => o.getPerformance_to_Space_ratio()).ToList<cluster>();

                        Parallel.ForEach(sortedList, c =>
                        {
                            if (returnList.Count < amount)
                                returnList.Add(c);
                        });
                        break;
                    }
                case 'P':
                    { //power efficiency
                        List<cluster> sortedList = listIn.AsParallel().OrderBy(o => o.getPerformance_to_Power_Usage()).ToList<cluster>();

                        Parallel.ForEach(sortedList, c =>
                        {
                            if (returnList.Count < amount)
                                returnList.Add(c);
                        });
                        break;
                    }
                case 'M':
                    { //max power
                        List<cluster> sortedList = listIn.AsParallel().OrderBy(o => o.getTotal_Performance()).ToList<cluster>();

                        Parallel.ForEach(sortedList, c =>
                        {
                            if (returnList.Count < amount)
                                returnList.Add(c);
                        });
                        break;
                    }

                case 'Q':
                    { //max power
                        List<cluster> sortedList = listIn.AsParallel().OrderByDescending(o => o.getClusterCost()).ToList<cluster>();

                        Parallel.ForEach(sortedList, c =>
                        {
                            if (returnList.Count < amount)
                                returnList.Add(c);
                        });
                        break;
                    }
            }

            return returnList;
        }
Пример #31
0
        public async Task <HttpResponseMessage> ExecuteScheduledNotifications(string userName, string password)
        {
            var bguser = await(from u in DataContext.Users where u.UserName == userName && u.Active && !u.Deleted select u).FirstOrDefaultAsync();

            if (bguser == null || bguser.UserType != DTO.Enums.UserTypes.BackgroundTask || bguser.PasswordHash != password.ComputeHash())
            {
                return(Request.CreateErrorResponse(HttpStatusCode.Forbidden, "You do not have permission to execute this notification. Either the login, password or User Type is wrong for the specified credentials"));
            }

            //Handle cleaning of documents
            //This is done here because we're executing notifications on a daily basis. This can be moved if necessary.
            var days = WebConfigurationManager.AppSettings["KeepResponseDocumentsDays"].ToInt32();

            if (days > 0)
            {
                try
                {
                    var dt = DateTime.UtcNow.AddDays(days * -1);

                    var docIds = await(from d in DataContext.Documents where d.CreatedOn < dt select d.ID).ToArrayAsync();

                    Logger.Debug($"{docIds.Count()} Documents pending deletion.");

                    await DataContext.Database.ExecuteSqlCommandAsync("DELETE FROM Documents where CreatedOn < {0}", dt);
                }
                catch (Exception ex)
                {
                    Logger.Error("Error happened when deleting documents: ", ex);
                }
            }

            var runTime = DateTime.UtcNow;

            var Logging = new ConcurrentBag <object>();

            //Get all of the loggers
            foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies().AsParallel())
            {
                var logTypesToRegister = assembly.GetTypes().Where(
                    type => type.BaseType != null && !type.IsAbstract &&
                    type.BaseType.IsGenericType &&
                    (type.BaseType.GetGenericTypeDefinition() == typeof(EntityLoggingConfiguration <,>)));

                foreach (object configurationInstance in logTypesToRegister.Select(Activator.CreateInstance))
                {
                    Logging.Add(configurationInstance);
                }
            }


            var notifications = new ConcurrentBag <Notification>();

            //Loop through all of them generating notifications
            foreach (var log in Logging.AsParallel())
            {
                try
                {
                    var method = log.GetType().GetMethod("GenerateNotificationsFromLogs");
                    var task   = (Task <IEnumerable <Notification> >)method.Invoke(log, new object[] { DataContext });
                    foreach (var n in await task)
                    {
                        notifications.Add(n);
                    }
                }
                catch (Exception ex)
                {
                    Logger.Error("Error Occured while executing notifications", ex);
                }
            }

            //Flip the results so that we get a list of users, and a sub list of their notifications.
            var users = (from n in notifications from r in n.Recipients select r).DistinctBy(n => n.Email);

            var sendNotifications = new ConcurrentBag <Notification>();

            foreach (var user in users.AsParallel())
            {
                try
                {
                    //We are multi-threading this so that we can send as many as possible at one time.
                    //Create a single notification for the user

                    var notification = new Notification
                    {
                        Recipients = new Recipient[] { user },
                    };

                    var uNotes = notifications.Where(n => n.Recipients.Any(r => r.Email == user.Email)).Select(n => new { n.Body, n.Subject });

                    if (uNotes.Count() > 1)
                    {//If there is more than one notification being aggrigated then set the subject to a generic title.
                        notification.Subject = "Notifications and Reminders";
                    }
                    else //If there is only one, use the specific subject created by the notification.
                    {
                        notification.Subject = uNotes.First().Subject;
                    }

                    notification.Body = string.Join("<hr/>", uNotes.Select(n => n.Body).ToArray());

                    //This adds it the aggrigate notification to the list of notifications that will be sent.
                    //Each of these notifications only have one recipient.
                    sendNotifications.Add(notification);
                }
                catch (Exception ex)
                {
                    Logger.Error("Error Occured while sending notifications", ex);
                }
            }

            //Create the notifier
            var notifier = new Notifier();

            //Asynchronously send all of the notifications
            await Task.Run(() => notifier.Notify(sendNotifications.AsEnumerable()));

            //Upon completion update all of the notifications that should have run with the new time that they should be run again.
            var subscriptions = await(from s in DataContext.UserEventSubscriptions where (s.NextDueTime == null || s.NextDueTime.Value < runTime) && s.User.Active && s.Frequency != DTO.Enums.Frequencies.Immediately select s).ToArrayAsync();

            foreach (var sub in subscriptions)
            {
                switch (sub.Frequency)
                {
                case DTO.Enums.Frequencies.Daily:
                    sub.NextDueTime = runTime.AddDays(1);
                    sub.LastRunTime = runTime;
                    break;

                case DTO.Enums.Frequencies.Monthly:
                    sub.NextDueTime = runTime.AddMonths(1);
                    sub.LastRunTime = runTime;
                    break;

                case DTO.Enums.Frequencies.Weekly:
                    sub.NextDueTime = runTime.AddDays(7);
                    sub.LastRunTime = runTime;
                    break;
                }
            }

            var mySubscriptions = await(from s in DataContext.UserEventSubscriptions where (s.NextDueTime == null || s.NextDueTime.Value < runTime) && s.User.Active && s.FrequencyForMy != DTO.Enums.Frequencies.Immediately select s).ToArrayAsync();

            foreach (var mySub in subscriptions)
            {
                switch (mySub.FrequencyForMy)
                {
                case DTO.Enums.Frequencies.Daily:
                    mySub.NextDueTimeForMy = runTime.AddDays(1);
                    mySub.LastRunTime      = runTime;
                    break;

                case DTO.Enums.Frequencies.Monthly:
                    mySub.NextDueTimeForMy = runTime.AddMonths(1);
                    mySub.LastRunTime      = runTime;
                    break;

                case DTO.Enums.Frequencies.Weekly:
                    mySub.NextDueTimeForMy = runTime.AddDays(7);
                    mySub.LastRunTime      = runTime;
                    break;
                }
            }

            await DataContext.SaveChangesAsync();

            if (Request != null)
            {
                return(Request.CreateResponse(HttpStatusCode.OK));
            }
            else
            {
                //request does not exist when run in test harness.
                return(new HttpResponseMessage(HttpStatusCode.OK));
            }
        }
Пример #32
0
 private async Task SearchContactsWithLetter(string sLetters, ContactQuery query)
 {
     var bExceptionRaised = false;
     try
     {
         var agenda = await ContactManager.RequestStoreAsync();
         var contacts = await agenda.FindContactsAsync(sLetters);
         var finallist = new ConcurrentBag<ContactLite>();
         FilterReturnedContacts(contacts, finallist, query);
         finallist.AsParallel().ForAll(contact => _finalUsers.TryAdd(contact.ID, contact));
     }
     catch (Exception ex)
     {
         //Security Error --> '@' character in websites collection URL
         if (ex.HResult == -2146697202)
         {
             bExceptionRaised = true;
         }
     }
     if (bExceptionRaised)
     {
         if (sLetters.Length < 2)
         {
             _startingLetters.Add((sLetters));
             await StartContactSeach(sLetters, query);
         }
         else
             _faultyLetters.Add(sLetters);
     }
 }
Пример #33
0
        public override async Task ListContacts(ContactQuery query)
        {

            ContactLite[] returnUsers = null;
            Task.Run(async () =>
            {
                var bExceptionRaised = false;
                try
                {
                    var finallist = new ConcurrentBag<ContactLite>();
                    var agenda = await ContactManager.RequestStoreAsync();
                    var foundContactList = await agenda.FindContactsAsync();

                    if (query != null)
                    {
                        WindowsPhoneUtils.Log("Listing contacts by query: " + query);
                        FilterReturnedContacts(foundContactList, finallist, query);
                    }
                    else
                    {
                        WindowsPhoneUtils.Log("Listing ALL contacts...");
                        foundContactList.AsParallel()
                        .ForAll(contact => finallist.Add(new ContactLite
                        {
                            ID = contact.Id.Replace("{", "").Replace(".", "").Replace("}", ""),
                            DisplayName = contact.DisplayName,
                            Name = contact.FirstName,
                            Firstname = contact.MiddleName,
                            Lastname = contact.LastName,
                            Phones = GetContactPhonesArray(contact),
                            Emails = GetContactEmailArray(contact)
                        }));
                    }
                    returnUsers = finallist.ToArray();
                }
                catch (UnauthorizedAccessException ex)
                {
                    WindowsPhoneUtils.Log("Not enough privileges to access Contacts");
                    WindowsPhoneUtils.InvokeCallback(AccessDeniedContactsCallback, WindowsPhoneUtils.CALLBACKID, null);
                    return;
                }
                catch (Exception ex)
                {
                    //error
                    bExceptionRaised = true;
                }

                if (bExceptionRaised)
                {
                    try
                    {
                        _faultyLetters = new ConcurrentBag<string>();
                        _startingLetters = new ConcurrentBag<string>();
                        _finalUsers.Clear();
                        await StartContactSeach(String.Empty, query);
                        returnUsers = _finalUsers.Values.ToArray();


                        _startingLetters.AsParallel().ForAll(startingLetter =>
                        {
                            if (!_faultyLetters.Any(
                                    faultySilabe =>
                                        faultySilabe.StartsWith(startingLetter,
                                            StringComparison.CurrentCultureIgnoreCase)))
                                _faultyLetters.Add(startingLetter);
                        });

                        WindowsPhoneUtils.InvokeCallback(WpContactsFailedCallback, WindowsPhoneUtils.CALLBACKID, JsonConvert.SerializeObject(_faultyLetters.OrderBy(x => x).ToArray()));
                    }
                    catch (Exception ex)
                    {
                        //UNHANDLED ERROR
                        WindowsPhoneUtils.Log("Unhandled error recovering contacts:" + ex.Message);
                    }
                }
                WindowsPhoneUtils.InvokeCallback(ListContactsCallback, WindowsPhoneUtils.CALLBACKID, JsonConvert.SerializeObject(returnUsers));
            });
        }