void bw_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker bw = (BackgroundWorker)sender; BWEventArgs args = (BWEventArgs)e.Argument; PrintStepDelegate printDelegate = new PrintStepDelegate(PrintStep); int stepIteration = (args.endLength - args.startLength) / args.stepsCount; BeginInvoke(printDelegate, "Пакетное тестирование запущено."); for (int i = args.startLength, step = 1; step <= args.stepsCount; step++, i += stepIteration) { BeginInvoke(printDelegate, "=================================================="); BeginInvoke(printDelegate, string.Format("Начинается тестирование. Шаг: {0}/{1}", step, args.stepsCount)); BeginInvoke(printDelegate, string.Format(" Размерность массива: {0}", i)); BeginInvoke(printDelegate, "=================================================="); startTesting(i, printDelegate); BeginInvoke(printDelegate, "Тестирование шага завершено."); bw.ReportProgress(step * 100 / args.stepsCount); if (bw.CancellationPending) { BeginInvoke(printDelegate, "=================================================="); BeginInvoke(printDelegate, "Тестирование отменено!"); BeginInvoke(printDelegate, "=================================================="); break; } } }
void startTesting(int length, PrintStepDelegate printFunc) { Random rnd = new Random(); int[] numbers = new int[length]; for (int i = 0; i < length; ++i) { numbers[i] = rnd.Next(); } SortReport quickReport = startSortingProc("Quick-sort.exe", numbers); BeginInvoke(printFunc, string.Format("Quick-sort is finished at {0} ms.", quickReport.millisecs)); SortReport heapReport = startSortingProc("Heap-sort.exe", numbers); BeginInvoke(printFunc, string.Format("Heap-sort is finished at {0} ms.", heapReport.millisecs)); }