private void ExecuteProcessNumberSieveCommand() { // reset display LoadNumbers(); // disable process button DispatchService.Invoke(() => IsNotBusy = false); // perform sieve in background thread DispatchService.Background(() => { const int interval = 100; var max = _numbers.Count + 1; for (var i = 2; i <= max; i++) { // delay System.Threading.Thread.Sleep(interval); // adjust for collection starting at 2 var index = i - 2; var number = i; // See if i is prime. if (!PrimeNumberService.IsPrimeNumber(number)) { continue; } // mark as prime _numbers[index].IsPrime = true; _numbers[index].DisplayColor = ColorService.PrimeColor; // Knock out multiples of number var backgroundColor = ColorService.GetNextColor(); for (var j = number * number; j <= max; j += number) { _numbers[j - 2].IsPrime = false; _numbers[j - 2].Model.DisplayColor = backgroundColor; // delay System.Threading.Thread.Sleep(interval); } } // re-enable button DispatchService.Invoke(() => IsNotBusy = true); }); }