Friday, December 1, 2017

Convert C# foreach loop to process in parallel

Note: this is a C# 4.0 feature

The first loop run in a serial matter, and the second one parallel. First one is guaranteed output ABCD, but the second write these characters in a random fashion

List<string> strCollection = new List<string>() { "A", "B", "C", "D" };
// Process one at a time
foreach (string str in strCollection)
{
    Console.WriteLine(str);
}
// Process in parallel
System.Threading.Tasks.Parallel.ForEach(strCollection, str =>
{
    Console.WriteLine(str);
});


No comments:

Turn on Windows 11 Fast Boot

If windows starting is slow, to enable windows 11 fast startup/boot,  Press Windows + R, type powercfg.cpl, and hit Enter.  This will direct...