private void BtnStartSorting_Click(object sender, EventArgs e) { if (sorting) { return; } // SORT NAME sortName = cb_Sortings.SelectedItem as string; if (sortName == null) { ShowMessageBox("Select sorting name!"); return; } // NUMBERS COUNT int numbersCount = ReadNumbersCount(); if (numbersCount == -1) { return; } // MAX RANDOM int maxRandom = ReadMaxRandom(); if (maxRandom == -1) { return; } Sorter.SetMaxRandom(maxRandom); // SORT TYPE sortType = ReadSortType(); // SORTING sorting = true; cts = new CancellationTokenSource(); Task.Run(() => RunSort(numbersCount, cts.Token)); }
void RunSort(int numbersCount, CancellationToken token) { Sorter sorter = null; Stopwatch watch1 = null; List <int> numbers = null; try { Invoke(updater, new object[] { "time", 0L } as object); Invoke(updater, new object[] { "sort_name", $"Name : {sortName}" } as object); Invoke(updater, new object[] { "process", "Preparing" } as object); Invoke(updater, new object[] { "numbers_count", $"Numbers count : {numbersCount}" } as object); Invoke(updater, new object[] { "array_size", $"Array size : {Sorter.FileSize(numbersCount * sizeof(int))}" } as object); Invoke(updater, new object[] { "steps_passed", "" } as object); Invoke(updater, new object[] { "need_steps", "" } as object); sorter = new Sorter(this); numbers = sorter.GenerateList(numbersCount, sortType); if (token.IsCancellationRequested) { return; } timer.Start(); watch1 = new Stopwatch(); watch1.Start(); Invoke(updater, new object[] { "process", "Sorting" } as object); switch (sortName) { case "Bubble sort": sorter.BubbleSort(numbers, token); break; case "Odd-Even sort": sorter.OddEvenSort(numbers, token); break; case "Enumeration sort": numbers = sorter.EnumerationSort(numbers, token); break; case "Parallel Odd-Even sort": sorter.ParallelOddEvenSort(numbers, token); break; case "Parallel Enumeration sort": numbers = sorter.ParallelEnumerationSort(numbers, token); break; case "Bucket sort": numbers = sorter.BucketSort(numbers, token); break; case "Parallel Bucket sort": numbers = sorter.ParallelBucketSort(numbers, token); break; case "Parallel Shell sort": sorter.ParallelShellSort(numbers, token); break; case "Merge sort": cannotCancel = true; numbers = sorter.MergeSort(numbers, token); break; case "Quick sort": cannotCancel = true; numbers = sorter.QuickSort(numbers, token); break; case "Parallel Quick sort": cannotCancel = true; numbers = sorter.ParallelQuickSort(numbers, token); break; case "Bitonic sort": sorter.BitonicSort(numbers, token); break; case "Parallel Bitonic sort": sorter.ParallelBitonicSort(numbers, token); break; default: break; } } catch (BitonicSortException e) { ShowMessageBox($"Error:\n{e.Message}"); failed = true; } catch (Exception e) { ShowMessageBox($"Error:\n{e.Message}"); failed = true; } finally { cannotCancel = false; if (watch1 != null) { watch1.Stop(); Invoke(updater, new object[] { "time", watch1.ElapsedMilliseconds } as object); } timer.Stop(); timeWasTicked = 0; //sorter.DisposeList(ref numbers); if (!token.IsCancellationRequested) { if (!failed) { Invoke(updater, new object[] { "process", "Finished" } as object); } else { Invoke(updater, new object[] { "process", "Failed" } as object); failed = false; } } else { Invoke(updater, new object[] { "process", "Cancelled" } as object); Invoke(updater, new object[] { "btn_stop_enable" } as object); } sorting = false; } }