Essential C# Threading  Suggest an edit

Parallel.ForEach Loop

If you have a foreach loop that you want to speed up and you don’t mind what order the output is in, you can convert it to a parallel foreach loop by doing the following:

using System;
using System.Threading;
using System.Threading.Tasks;

public class MainClass {

    public static void Main() {
        int[] Numbers = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        // Single-threaded
        Console.WriteLine("Normal foreach loop: ");
        foreach (var number in Numbers) {
            Console.WriteLine(longCalculation(number));
        }
        // This is the Parallel (Multi-threaded solution)
        Console.WriteLine("Parallel foreach loop: ");
        Parallel.ForEach(Numbers, number => {
            Console.WriteLine(longCalculation(number));
        });
    }

    private static int longCalculation(int number) {
        Thread.Sleep(1000); // Sleep to simulate a long calculation
        return number * number;
    }
}

Table Of Contents
22 Enum
25 GUID
107 Stream
108 Timers
110 Threading
  ↑ ↓ to navigate     ↵ to select     Esc to close