Files
gpd-924-en/Multithreading/Multithreading/Tasks.cs
T
david 44ffc8bd43 add async programming examples
add race condition examples
2025-07-01 18:10:55 +02:00

135 lines
5.2 KiB
C#

// using System;
// using System.Threading;
// using System.Threading.Tasks;
//
// namespace Multithreading
// {
// internal class Program
// {
// public static void Main(string[] args)
// {
// Console.WriteLine("Program started");
//
// // Task task = new Task(() =>
// // {
// // for (int i = 0; i < 10; i++)
// // {
// // Console.WriteLine($"Task ({Thread.CurrentThread.ManagedThreadId}): {i}");
// // Thread.Sleep(500);
// // }
// // });
// // task.Start();
// //
// // Task task2 = Task.Run(() =>
// // {
// // for (int i = 0; i < 10; i++)
// // {
// // Console.WriteLine($"Task ({Thread.CurrentThread.ManagedThreadId}): {i}");
// // Thread.Sleep(500);
// // }
// // });
// //
// // task.Wait();
// // task2.Wait();
// //
// // Task task3 = new Task(() =>
// // {
// // for (int i = 0; i < 10; i++)
// // {
// // Console.WriteLine($"Task ({Thread.CurrentThread.ManagedThreadId}): {i}");
// // Thread.Sleep(500);
// // }
// // });
// // task3.RunSynchronously();
//
// // CancellationTokenSource cts = new CancellationTokenSource();
// // CancellationToken token = cts.Token;
// //
// // Task task4 = Task.Run(() =>
// // {
// // for (int i = 0; i < 10; i++)
// // {
// // Console.WriteLine($"Task ({Thread.CurrentThread.ManagedThreadId}): {i}");
// // Thread.Sleep(500);
// //
// // // token.ThrowIfCancellationRequested();
// // if (token.IsCancellationRequested)
// // {
// // // throw new TaskCanceledException();
// // break;
// // }
// // }
// // }, token);
// //
// // ConsoleKeyInfo keyInfo = Console.ReadKey();
// // if (keyInfo.Key == ConsoleKey.A) cts.Cancel();
// //
// // try
// // {
// // task4.Wait(token);
// // }
// // catch (OperationCanceledException e)
// // {
// // Console.WriteLine(e);
// // }
// //
// // cts.Dispose();
//
// // CancellationTokenSource cts2 = new CancellationTokenSource();
// // Random random = new Random();
// // Task[] tasks = new Task[10];
// // for (int i = 0; i < tasks.Length; i++)
// // {
// // tasks[i] = Task.Run(() =>
// // {
// // Console.WriteLine($"Task ({Thread.CurrentThread.ManagedThreadId}) started");
// // for (int j = 0; j < 10; j++)
// // {
// // Console.WriteLine($"Task ({Thread.CurrentThread.ManagedThreadId}): {j}");
// // Thread.Sleep(random.Next(100, 1000));
// //
// // if (random.NextSingle() > 0.9f)
// // {
// // Console.WriteLine($"Task ({Thread.CurrentThread.ManagedThreadId}) aborted");
// // cts2.Cancel();
// // // throw new OperationCanceledException();
// // }
// //
// // cts2.Token.ThrowIfCancellationRequested();
// // }
// //
// // Console.WriteLine($"Task ({Thread.CurrentThread.ManagedThreadId}) ended");
// // }, cts2.Token);
// // Console.WriteLine($"Task with id {tasks[i].Id} created");
// // Console.WriteLine($"Task ({tasks[i].Id}): {tasks[i].Status}");
// // }
// //
// // try
// // {
// // Task.WaitAll(tasks, cts2.Token);
// // // Task.WaitAny(tasks);
// // }
// // catch (OperationCanceledException)
// // {
// // Console.WriteLine("Operation was canceled");
// // }
// // catch (AggregateException)
// // {
// // Console.WriteLine("One or more tasks canceled");
// // }
// //
// // for (int i = 0; i < tasks.Length; i++)
// // {
// // Console.WriteLine($"Task ({tasks[i].Id}): {tasks[i].Status}");
// // }
//
// Parallel.For(0, 10000, i =>
// {
// // doing some heavy calculation
// Console.WriteLine($"Thread ({Thread.CurrentThread.ManagedThreadId}): {i}");
// });
//
// Console.WriteLine("Program ended");
// }
// }
// }