private void BackgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { CurrentAlgortihm currentAlgortihm = (CurrentAlgortihm)e.Argument; try { Thread.Sleep(300); } catch (ThreadInterruptedException exception) { Console.WriteLine("Thread Interrupted" + exception); } foreach (Algorithm algorithm in Algorithms.ToArray()) { currentAlgortihm(algorithm); Window.ShuffleWhenStarted(); int[] unsortedArray = new int[Window.ArrayLength]; Array.Copy(Window.Array, unsortedArray, Window.ArrayLength); algorithm.Start(); algorithm.Sort(); algorithm.Stop(); Sleep(100); Window.RunWhenFinallySorted(); Sleep(100); Window.ResetColor(); Sleep(100); if (ShouldSave) { SortSummary summary = new SortSummary { Iterations = algorithm.Swaps, Name = algorithm.Name, SortedArray = Window.Array, UnsortedArray = unsortedArray }; SortSummaries.Add(summary); } if (Algorithms.Any()) { Algorithms.Dequeue(); if (Algorithms.Any()) { Window.ShuffleAfterSorted(); Window.Invalidate(); } } } }