Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static class Program
- {
- static async Task<string> GetTaskAsync(int timeout)
- {
- Console.WriteLine("Task Thread: " + Thread.CurrentThread.ManagedThreadId);
- await Task.Delay(timeout);
- return timeout.ToString();
- }
- static async Task Main()
- {
- Console.WriteLine("Main Thread: " + Thread.CurrentThread.ManagedThreadId);
- Console.WriteLine("Should be greater than 5000");
- await Watch(NotParallel);
- Console.WriteLine("Should be less than 5000");
- await Watch(Parallel);
- }
- public static async Task Parallel()
- {
- var res1 = GetTaskAsync(2000);
- var res2 = GetTaskAsync(3000);
- Console.WriteLine("result: " + await res1 + await res2);
- }
- public static async Task NotParallel()
- {
- var res1 = await GetTaskAsync(2000);
- var res2 = await GetTaskAsync(3000);
- Console.WriteLine("result: " + res1 + res2);
- }
- private static async Task Watch(Func<Task> func) {
- var sw = new Stopwatch();
- sw.Start();
- await func?.Invoke();
- sw.Stop();
- Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds);
- Console.WriteLine("---------------");
- }
- }
Add Comment
Please, Sign In to add comment