private void GenerateWords(IDictionaryRequest request, PartitionInfo partitionInfo) { var partitionedWords = new ConcurrentQueue <IDictionary <int, IGeneratedWord> >(); var useNoise = dictionaryConfiguration.UseNoise; var startTime = DateTime.Now; maxValue = dictionary.WordList.Count; FireGenerateChanged(new ProgressChangedEventArgs(10, String.Empty)); // Multi-Threaded block var tasks = new Task[partitionInfo.NumberOfPartitions]; var index = 0; for (; index < partitionInfo.NumberOfPartitions; index++) { tasks[index] = Task.Run(() => { var iterations = index == partitionInfo.NumberOfPartitions - 1 ? partitionInfo.LastPartitionSize : partitionInfo.FullPartitionSize; var wordsPartition = useNoise ? GeneratePartitionedWordsWithNoise(iterations) : GeneratePartitionedWords(iterations); partitionedWords.Enqueue(wordsPartition); }); } try { Task.WaitAll(tasks); var words = new WordContainer(dictionaryConfiguration.UseNoise) { PartitionedWords = partitionedWords }; var endTime = DateTime.Now; var duration = endTime - startTime; var result = new DictionaryResult(request, duration, words) { Words = words }; FireGenerateChanged(new ProgressChangedEventArgs(100, null)); FireGenerateCompleted(new DictionaryEventArgs(null, false, null, result)); } catch (AggregateException ae) { FireGenerateCompleted(new DictionaryEventArgs(ae.Flatten(), false, ae, null)); } }
private async Task GenerateAsyncInternal(IDictionaryRequest request, CancellationToken cancellationToken) { progressPercentage = 0; try { await LazyInitialization(); var partitionInfo = PartitionInfo.Get(request.Iterations, threadService.GetThreadCountByIterations(request.Iterations)); FireGenerateChanged(new ProgressChangedEventArgs(progressPercentage += 5, $"Created {partitionInfo.NumberOfPartitions} partitions.")); GenerateWords(request, partitionInfo); } catch (Exception e) { FireGenerateCompleted(new DictionaryEventArgs(e, false, this, null)); } }
/// <summary> /// Runs the <see cref="GenerateAsync(BigInteger)"/> routine. /// <para>Subscribe to <see cref="GenerateChanged"/> and /// <see cref="GenerateCompleted"/> events.</para> /// </summary> /// <param name="request">The request used to generate.</param> /// <returns>Returns an empty task.</returns> public async Task GenerateAsync(IDictionaryRequest request) { await GenerateAsyncInternal(request); }
public Task GenerateWordsAsync(IDictionaryRequest dictionaryRequest, CancellationToken cancellationToken) { await GenerateAsyncInternal(request, cancellationToken); }
public void GenerateWords(IDictionaryRequest dictionaryRequest, CancellationToken cancellationToken) { throw new NotImplementedException(); }