public void ActionsCanBeExecutedNotAtTheSameThread() { List <int> threadIds = new List <int>(); var executer = new ActionExecutor(); executer.AddActionToQueue(() => { threadIds.Add(Thread.CurrentThread.ManagedThreadId); }); executer.AddActionToQueue(() => { threadIds.Add(Thread.CurrentThread.ManagedThreadId); }); executer.AddActionToQueue(() => { threadIds.Add(Thread.CurrentThread.ManagedThreadId); }); executer.AddActionToQueue(() => { threadIds.Add(Thread.CurrentThread.ManagedThreadId); }); var task = executer.Execute(); task.GetAwaiter().GetResult(); var uniqueItems = threadIds.Distinct().Count(); Assert.True(uniqueItems >= 1); }
public void ActionsMustBeExecutedSequentially() { StringBuilder stringBuilder = new StringBuilder(); var executer = new ActionExecutor(); executer.AddActionToQueue(() => { stringBuilder.Append("My "); }); executer.AddActionToQueue(() => { stringBuilder.Append("name "); }); executer.AddActionToQueue(() => { stringBuilder.Append("is "); }); executer.AddActionToQueue(() => { stringBuilder.Append("Dmitriy"); }); var task = executer.Execute(); task.GetAwaiter().GetResult(); Assert.Equal("My name is Dmitriy", stringBuilder.ToString()); }
public void ActionsCanBeAddedNotAtTheSameTime() { int sum = 0; var executer = new ActionExecutor(); sum += 5; executer.AddActionToQueue(() => { sum += 10; }); sum += 5; executer.AddActionToQueue(() => { sum += 10; }); var task = executer.Execute(); task.GetAwaiter().GetResult(); Assert.Equal(30, sum); }
private void SaveButton_Click(object sender, RoutedEventArgs e) { var actionExecutor = new ActionExecutor(); //Initialization of ActionExecutor class //Select each created content fileContents.Select((content, index) => { //Add action with writing content to file to list of actions actionExecutor.AddActionToQueue(() => { Thread.Sleep(2000); //Just to make action longer and be sure that UI is not blocked //Standart .net procudere of writing file to path using (StreamWriter outputFile = new StreamWriter(Path.Combine(documentsPath, string.Format(fileNameTemplate, ++index)))) { outputFile.Write(content); } }); return(content); }).ToList(); //Because .Select() returns IEnumerable and it's causes LazyLoading actionExecutor.Execute(); //Execute actions in background sequentially this.ContentTextBox.Clear(); //Refresh TextBox this.CountOfContentsLabel.Content = "0"; //Default value of label }
public void ActionsMustBeExecutedInBackground() { var executer = new ActionExecutor(); executer.AddActionToQueue(() => { Thread.Sleep(Timeout.Infinite); }); executer.Execute(); Assert.True(true); }