public void TestFileMonitor() { var stack = new ConcurrentStack <FileMonitorObject>(); var monitor = new FileSystemMonitor(new MonitorCommandOptions() { MonitoredDirectories = Path.GetTempPath() }, x => stack.Push(x)); monitor.StartRun(); var created = Path.GetTempFileName(); // Create a file var renamed = $"{created}-renamed"; File.WriteAllText(created, "Test"); // Change the size File.Move(created, renamed); // Rename it File.Delete(renamed); //Delete it Thread.Sleep(100); monitor.StopRun(); Assert.IsTrue(stack.Any(x => x.NotifyFilters == NotifyFilters.FileName && x.Path == created)); Assert.IsTrue(stack.Any(x => x.NotifyFilters == NotifyFilters.Size && x.Path == created)); Assert.IsTrue(stack.Any(x => x.ChangeType == CHANGE_TYPE.RENAMED && x.NotifyFilters == NotifyFilters.FileName && x.Path == renamed)); Assert.IsTrue(stack.Any(x => x.ChangeType == CHANGE_TYPE.DELETED && x.Path == renamed)); }
private static void RunCommandLine(CommandLineArguments arguments) { List <Task> tasks = new List <Task>(); // Build each configuration specified foreach (string buildConfig in arguments.GetParameterValue("buildConfig").Split('|')) { foreach (string installType in arguments.GetParameterValue("installerTypes").Split('|')) { tasks.Add(Task.Factory.StartNew(() => BuildInstaller(arguments.GetParameterValue("workingPath"), installType, buildConfig))); } } if (tasks.Any()) { // Wait until we have forms and none of them are processing while (_processes.Any() && _processes.Any(x => x.Status != InstallerStatus.Completed)) { System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1)); } } _processes.Clear(); tasks.Clear(); }
public void Push(IRevertable cellChangeState) { if (_historyForwardStack.Any()) { _historyForwardStack.Clear(); } _historyBackStack.Push(cellChangeState); }
public static void DoTasksParallel(IEnumerable <Task> tasks, int countOfParallesTreads) { if (!tasks.Any()) { return; } var workers = new List <Task>(); var tasksStack = new ConcurrentStack <Task>(); tasksStack.PushRange(tasks.Reverse().ToArray()); for (var i = 0; i < countOfParallesTreads; i++) { var worker = new Task(() => { Task result; while (tasksStack.Any() && tasksStack.TryPop(out result)) { result.RunSynchronously(); } }); workers.Add(worker); worker.Start(); } Task.WaitAll(workers.ToArray()); }
public void RollBack() { if (_historyBackStack.Any()) { _historyBackStack.TryPop(out IRevertable revert); var undoRevert = revert.Revert(); _historyForwardStack.Push(undoRevert); } }
/// <summary> /// Combines accumulated failures and throws an assertion, if there are any. /// Clears accumulated failures. /// </summary> public static void AssertFailures() { if (Failures.Any()) { Assert.Fail(Failures.Reverse().Aggregate((x, y) => string.Format("{0}\n{1}", x, y))); } Failures.Clear(); }
private static Person ValidateCSVInput(string[] data, string[] headers, IList <Person> exportList, ConcurrentStack <Person> csvFileList) { if (data.Length != headers.Length) { Console.WriteLine("The input data format were unexpected, please follow the input rule"); return(null); } // check id int id = -1; if (!int.TryParse(data[0], out id)) { Console.WriteLine("Invaild [ID] input, please try again"); return(null); } if (exportList.Any(p => p.ID == id) || csvFileList.Any(p => p.ID == id)) { Console.WriteLine("Duplicated [ID] input, please try again"); return(null); } // check name var name = data[1]; if (string.IsNullOrWhiteSpace(name)) { Console.WriteLine("Invaild [Name] input, please try again"); return(null); } // check sex Sex sex; if (!Enum.TryParse(data[2], out sex) || (sex != Sex.Female && sex != Sex.Male)) { Console.WriteLine("Invaild [Sex] input, please try again"); return(null); } // check birthday if (!DateTime.TryParse(data[3], out DateTime birthday)) { Console.WriteLine("Invaild [Birthday] input, please try again"); return(null); } return(new Person() { ID = id, Name = name, Sex = sex, Birthday = birthday }); }
public void Dequeue() { while (CollectionUpdateTasks.Any()) { if (CollectionUpdateTasks.TryPop(out var action) && action != null) { action.RunSynchronously(); } } }
public static bool EnsureStateStackEmpty() { if (stateStack.Count <= 0) { return(false); } Log.Warning("Random state stack is not empty. There were more calls to PushState than PopState. Fixing.", false); while (stateStack.Any()) { PopState(); } return(false); }
public void Dispose() { Array.Clear(_buffer, 0, _buffer.Length); GC.SuppressFinalize(_buffer); while (_pool.Any()) { if (_pool.TryPop(out var p)) { p.Dispose(); } } try { _acquisitionGate.Release(); } catch { } }
private void PostProcessHandler() { // Wait until we have forms and none of them are processing while (_processForms.Any() && _processForms.Any(x => x.Status == InstallerStatus.Processing) == false) { Thread.Sleep(TimeSpan.FromSeconds(1)); } if (_processForms.All(x => x.Status == InstallerStatus.Completed)) { // close the open windows if all successful foreach (InstallerForm form in _processForms) { if (form != null && form.InvokeRequired) { form.Invoke(new MethodInvoker(() => form.Close())); } } } else { UpdateStatus("Error Processing"); } }
public static void Main(string[] args) { var orderedDictionary = new Dictionary <string, long>(); var threadsNum = 8; var folderPath = Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory())); var fullPath = folderPath + @"\10k-livros"; var numFilesInPath = Directory.GetFiles(fullPath).Length; var files = Directory.EnumerateFiles(fullPath, "*.txt").ToList(); var stack = new ConcurrentStack <Thread>(); //100 //var totalFiles = files.GetRange(1, 40); //numFilesInPath = totalFiles.Count(); List <int> intervalos = new List <int>(); for (int i = 0; i < numFilesInPath; i += (numFilesInPath / threadsNum)) { intervalos.Add(i); } for (var i = 0; i < threadsNum - 1; i++) { var partialFilesList = files.GetRange(intervalos[i], (numFilesInPath / threadsNum) - 1); stack.Push(new Thread(() => ReadAllFiles(partialFilesList))); } foreach (var thread in stack) { thread.Start(); thread.Join(); } while (stack.Any(t => !t.IsAlive)) { orderedDictionary = wordDictionary.OrderByDescending(pair => pair.Value).Take(20) .ToDictionary(pair => pair.Key, pair => pair.Value); var filePath = folderPath + @"\word-analysis.csv"; var fileSummaryPath = folderPath + @"\word-summary-analysis.txt"; File.WriteAllText(fileSummaryPath, "Total files: " + numFilesInPath + "\n" + "Total words: " + wordDictionary.Count()); File.WriteAllLines(filePath, orderedDictionary.Select(x => x.Value + ", " + x.Key)); break; } }
public async Task LoopWaiting() { while (runLoop) { if (!tasks.Any()) { await Task.Delay(5000); } else { Task task; tasks.TryPop(out task); await task; } } }
internal void Cleanup(object state) { foreach (var kvp in cache) { ConcurrentStack <Tuple <long, HttpClient> > stack = kvp.Value; if (stack.Count == 0) { continue; } if (stack.Any(item => IsClientTooOld(item.Item1)) == false) { continue; } var newStack = new ConcurrentStack <Tuple <long, HttpClient> >(); if (cache.TryUpdate(kvp.Key, newStack, stack) == false) { continue; } var tmpStack = new Stack <Tuple <long, HttpClient> >(); Tuple <long, HttpClient> client; while (stack.TryPop(out client)) { if (IsClientTooOld(client.Item1)) { try { client.Item2.Dispose(); } catch (Exception) { } } else { tmpStack.Push(client); } } while (tmpStack.Count > 0) { newStack.Push(tmpStack.Pop()); } } }
/// <summary> /// Gets the next available <see cref="Dialog"/> from the internal collection. /// </summary> /// <returns> The next available <see cref="Dialog"/>. </returns> private Dialog GetNextDialog() { Dialog dialog; do { _dialogs.TryPeek(out dialog); if (dialog.ExternalCancellationToken.IsCancellationRequested) { Trace.WriteLine($"{this.GetType().Name.ToUpper()}: The next dialog has an external {nameof(CancellationToken)} that is already canceled. Therefore the dialog won't be shown."); this.RemoveTopMostView(DialogResult.Killed); dialog = null; } } while (dialog is null && _dialogs.Any()); return(dialog); }
private async Task PerformRandomRequest() { int availableRequestsCount = _allRequests.Count; while (true) { RequestType requestType = (RequestType)_random.Next(availableRequestsCount); if (!_tasksIds.Any() && (requestType == RequestType.DeleteTask || requestType == RequestType.GetTask || requestType == RequestType.PutTask)) { continue; } switch (requestType) { case RequestType.DeleteTask: if (_tasksIds.TryPop(out string deleteTaskId)) { await _allRequests[requestType](deleteTaskId); } break; case RequestType.GetTask: case RequestType.PutTask: if (_tasksIds.TryPop(out string getTaskId)) { await _allRequests[requestType](getTaskId); //items stays in db _tasksIds.Push(getTaskId); } break; case RequestType.PostTask: case RequestType.GetTasks: await _allRequests[requestType](string.Empty); break; } break; } }
public async void ProcessQueue() { while (_stack.Any()) { _stack.TryPop(out MessageContainer container); switch (container.MessageType) { case MessageType.GAMES_GET_LIST: break; case MessageType.SQLITE_INITIALIZE: InitializeSQLiteDB(); break; } await Task.Delay(200); } }
public async Task KeepConnected() { while (true) { try { if (Connected) { Console.WriteLine("Audit Server Connection Healthy"); if (logs.Any()) { var records = new object[logs.Count]; logs.TryPopRange(records, 0, records.Length); _ = WriteToServer(records); } await Task.Delay(30000); continue; } Console.WriteLine("Connecting to Audit Server"); client = new TcpClient(AddressFamily.InterNetwork); #if DEBUG await client.ConnectAsync(IPAddress.Loopback, Server.AUDIT_SERVER.Port).ConfigureAwait(false); #else await client.ConnectAsync(Server.AUDIT_SERVER.ServiceName, Server.AUDIT_SERVER.Port).ConfigureAwait(false); #endif if (!Connected) { await Task.Delay(5000); } } catch (Exception) { Console.WriteLine("Connection Failed"); await Task.Delay(5000); } } }
public bool RunValidation <T>(ConcurrentStack <string> logs, Type source, EntityBase <T> entity, CrudOperation?crudOperation = null, string baseMessage = null) where T : IValidatableObject, new() { if (crudOperation.HasValue) { switch (crudOperation.Value) { case CrudOperation.Create: entity.InitializeValidationContextValues(BaseValidator.CreationValidationRules); break; case CrudOperation.Read: entity.InitializeValidationContextValues(BaseValidator.ReadValidationRules); break; case CrudOperation.Update: entity.InitializeValidationContextValues(BaseValidator.UpdateValidationRules); break; case CrudOperation.Delete: entity.InitializeValidationContextValues(BaseValidator.DeleteValidationRules); break; } } if (!entity.Validate()) { logs.PushRange(entity.ValidationResults.Select(e => $"{baseMessage ?? String.Empty} {e.ErrorMessage}").ToArray()); } return(!logs.Any()); }
public string GetProxy() { var name = string.Empty; FillProxy(); if (_proxyList.Any()) { if (_proxyList.TryPop(out string ipProxy)) { name = ipProxy; } else { SendWarning($"ProxyContainerQueue.Method:GetProxy.Не удалось взять прокси из списка"); } } else { SendWarning($"ProxyContainerQueue.Method:GetProxy.Список прокси пуст!"); } return(name); }
public bool StackAny() { return _stack.Any(); }
private async Task ProcessTargetStack(ITaskBuilder taskBuilder) { // Keep building while we have targets to build and haven't been canceled. bool stopProcessingStack = false; while ( !_cancellationToken.IsCancellationRequested && !stopProcessingStack && _targetsToBuild.Any() ) { TargetEntry currentTargetEntry = _targetsToBuild.Peek(); switch (currentTargetEntry.State) { case TargetEntryState.Dependencies: // Ensure we are dealing with a target which actually exists. ProjectErrorUtilities.VerifyThrowInvalidProject ( _requestEntry.RequestConfiguration.Project.Targets.ContainsKey(currentTargetEntry.Name), currentTargetEntry.ReferenceLocation, "TargetDoesNotExist", currentTargetEntry.Name ); // If we already have results for this target which were not skipped, we can ignore it. In // addition, we can also ignore its before and after targets -- if this target has already run, // then so have they. if (!CheckSkipTarget(ref stopProcessingStack, currentTargetEntry)) { // Temporarily remove this entry so we can push our after targets _targetsToBuild.Pop(); // Push our after targets, if any. Our parent is the parent of the target after which we are running. IList <TargetSpecification> afterTargets = _requestEntry.RequestConfiguration.Project.GetTargetsWhichRunAfter(currentTargetEntry.Name); bool didPushTargets = await PushTargets(afterTargets, currentTargetEntry.ParentEntry, currentTargetEntry.Lookup, currentTargetEntry.ErrorTarget, currentTargetEntry.StopProcessingOnCompletion, TargetBuiltReason.AfterTargets); // If we have after targets, the last one to run will inherit the stopProcessing flag and we will reset ours. If we didn't push any targets, then we shouldn't clear the // flag because it means we are still on the bottom of this CallTarget stack. if ((afterTargets.Count != 0) && didPushTargets) { currentTargetEntry.StopProcessingOnCompletion = false; } // Put us back on the stack _targetsToBuild.Push(currentTargetEntry); // Determine which targets are dependencies. This will also test to see if the target should be skipped due to condition. // If it is determined the target should skip, the dependency list returned will be empty. IList <TargetSpecification> dependencies = currentTargetEntry.GetDependencies(_projectLoggingContext); // Push our before targets now, unconditionally. If we have marked that we should stop processing the stack here, which can only // happen if our current target was supposed to stop processing AND we had no after targets, then our last before target should // inherit the stop processing flag and we will reset it. // Our parent is the target before which we run, just like a depends-on target. IList <TargetSpecification> beforeTargets = _requestEntry.RequestConfiguration.Project.GetTargetsWhichRunBefore(currentTargetEntry.Name); bool pushedTargets = await PushTargets(beforeTargets, currentTargetEntry, currentTargetEntry.Lookup, currentTargetEntry.ErrorTarget, stopProcessingStack, TargetBuiltReason.BeforeTargets); if (beforeTargets.Count != 0 && pushedTargets) { stopProcessingStack = false; } // And if we have dependencies to run, push them now. if (dependencies != null) { await PushTargets(dependencies, currentTargetEntry, currentTargetEntry.Lookup, false, false, TargetBuiltReason.DependsOn); } } break; case TargetEntryState.Execution: // It's possible that our target got pushed onto the stack for one build and had dependencies process, then a re-entrant build started actively building // the target, encountered a legacy CallTarget, pushed new work onto the stack, and yielded back to here. Instead of starting the already-partially- // built target, wait for the other one to complete. Then CheckSkipTarget will skip it here. bool wasActivelyBuilding = await CompleteOutstandingActiveRequests(currentTargetEntry.Name); // It's possible that our target got pushed onto the stack for one build and had its dependencies process, then a re-entrant build came in and // actually built this target while we were waiting, so that by the time we get here, it's already been finished. In this case, just blow it away. if (!CheckSkipTarget(ref stopProcessingStack, currentTargetEntry)) { ErrorUtilities.VerifyThrow(!wasActivelyBuilding, "Target {0} was actively building and waited on but we are attempting to build it again.", currentTargetEntry.Name); // This target is now actively building. _requestEntry.RequestConfiguration.ActivelyBuildingTargets[currentTargetEntry.Name] = _requestEntry.Request.GlobalRequestId; // Execute all of the tasks on this target. MSBuildEventSource.Log.TargetStart(currentTargetEntry.Name); await currentTargetEntry.ExecuteTarget(taskBuilder, _requestEntry, _projectLoggingContext, _cancellationToken); MSBuildEventSource.Log.TargetStop(currentTargetEntry.Name); } break; case TargetEntryState.ErrorExecution: if (!CheckSkipTarget(ref stopProcessingStack, currentTargetEntry)) { // Push the error targets onto the stack. This target will now be marked as completed. // When that state is processed, it will mark its parent for error execution var errorTargets = currentTargetEntry.GetErrorTargets(_projectLoggingContext); try { await PushTargets(errorTargets, currentTargetEntry, currentTargetEntry.Lookup, true, false, TargetBuiltReason.None); } catch { _requestEntry.RequestConfiguration.ActivelyBuildingTargets.Remove(currentTargetEntry.Name); throw; } } break; case TargetEntryState.Completed: // The target is complete, we can gather up the results and remove this target // from the stack. TargetResult targetResult = currentTargetEntry.GatherResults(); // If this result failed but we are under the influence of the legacy ContinueOnError behavior for a // CallTarget, make sure we don't contribute this failure to the overall success of the build. targetResult.TargetFailureDoesntCauseBuildFailure = _legacyCallTargetContinueOnError; // This target is no longer actively building. _requestEntry.RequestConfiguration.ActivelyBuildingTargets.Remove(currentTargetEntry.Name); _buildResult.AddResultsForTarget(currentTargetEntry.Name, targetResult); TargetEntry topEntry = _targetsToBuild.Pop(); if (topEntry.StopProcessingOnCompletion) { stopProcessingStack = true; } PopDependencyTargetsOnTargetFailure(topEntry, targetResult, ref stopProcessingStack); break; default: ErrorUtilities.ThrowInternalError("Unexpected target state {0}", currentTargetEntry.State); break; } } }
public static void PopStack() { CreateStackIfNeeded(); Result += _stack.Any() && _stack.TryPop(out var num) ? num : 0; }
private void GenerateNames(int targetCount, ConcurrentStack <string> names) { int count = 0; while (count < targetCount) { int tries = 0; string name = "TSTests"; int unit = 'b' - 'a'; Random random = new Random(); while (_db.Texts.Any(x => x.Name == name) || _db.Groupings.Any(x => x.Name == name) || names.Any(x => x == name)) { if (tries > 15) { throw new Exception("What are the odds?"); } double nextRandom = random.NextDouble(); int increase = ((int)(nextRandom * 26)) * unit; name += ('a' + increase); ++tries; } names.Push(name); ++count; } }