fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApp22
  8. {
  9. class Program
  10. {
  11. internal class WorkTasksState
  12. {
  13. private int _inProgress = 0;
  14. public void AddTask() => Interlocked.Increment(ref _inProgress);
  15. public void SignalComplete() => Interlocked.Decrement(ref _inProgress);
  16. public bool AllDone => _inProgress == 0;
  17. public async Task WaitComplete()
  18. {
  19. while (!AllDone)
  20. {
  21. await Task.Delay(1);
  22. }
  23. }
  24. }
  25. static async Task Main()
  26. {
  27. while (true)
  28. {
  29. Console.WriteLine("Loop");
  30. WorkTasksState state = new();
  31. for(int i = 0; i < 2048; i++)
  32. {
  33. state.AddTask();
  34. ThreadPool.QueueUserWorkItem(Foo, state);
  35. }
  36. await state.WaitComplete();
  37. }
  38. }
  39. private static async void Foo(object? state)
  40. {
  41. var s = state as WorkTasksState;
  42. Console.WriteLine($"Start Foo in {Thread.CurrentThread.ManagedThreadId}");
  43. Random rnd = new();
  44. List<byte[]> temp = new();
  45. for(int i = 0; i < 1024; i++)
  46. {
  47. await Task.Yield();
  48. byte[] buffer = new byte[1024];
  49. temp.Add(buffer);
  50. }
  51. List<decimal> sums = new();
  52. foreach(var item in temp)
  53. {
  54. await Task.Yield();
  55. rnd.NextBytes(item);
  56. var sum = item.Sum(x=>x);
  57. sums.Add(sum);
  58. }
  59. //Раскоментируй и сравни
  60. //GC.Collect();
  61. Console.WriteLine($"Complete in {Thread.CurrentThread.ManagedThreadId}");
  62. s.SignalComplete();
  63. }
  64. }
  65. }
  66.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cs(30,43): error CS1525: Unexpected symbol `)', expecting `(' or `type'
prog.cs(43,29): error CS1525: Unexpected symbol `)', expecting `(' or `type'
prog.cs(44,36): error CS1525: Unexpected symbol `)', expecting `(' or `type'
prog.cs(51,37): error CS1525: Unexpected symbol `)', expecting `(' or `type'
Compilation failed: 4 error(s), 0 warnings
stdout
Standard output is empty