using System.Threading.Tasks;
public static void Main()
AsyncContext.Run(async () =>
ParallelOptions options = new()
MaxDegreeOfParallelism = 2,
TaskScheduler = TaskScheduler.FromCurrentSynchronizationContext()
textBox1.AppendText(@$"MaxDegreeOfParallelism: {options
.MaxDegreeOfParallelism}, MaximumConcurrencyLevel: {options
.TaskScheduler.MaximumConcurrencyLevel}" + "\r\n\r\n");
await Parallel.ForEachAsync(Enumerable.Range(1, 5), options, async (x, _) =>
textBox1.AppendText($"Processing #{x}\r\n");
textBox1.AppendText($"Completed #{x}\r\n");
private static class textBox1
public static void AppendText(string text) => Console.Write(text);