Advertisement
Guest User

Untitled

a guest
Dec 18th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. using System;
  2. using System.Threading.Tasks;
  3. using Microsoft.AspNetCore.Mvc;
  4. using System.Diagnostics;
  5. using Microsoft.AspNetCore.SignalR;
  6. using SignalRChat.Hubs;
  7.  
  8. namespace array_fill.Controllers
  9. {
  10. public class HomeController : Controller
  11. {
  12. static readonly int max_value = 1000;
  13. public int[] empty_array = new int[max_value];
  14. public string[] global = new string[11];
  15. public volatile int _lock = 0;
  16.  
  17. public IActionResult Index()
  18. {
  19. Stopwatch stopwatch = new Stopwatch();
  20. stopwatch.Start();
  21. for (int i = 0; i < max_value; i++)
  22. {
  23. empty_array[i] = i;
  24. }
  25. _lock = 0;
  26. stopwatch.Stop();
  27. Dothething();
  28.  
  29. ViewBag.help = stopwatch.ElapsedMilliseconds;
  30.  
  31. return View();
  32. }
  33.  
  34.  
  35. public int globalpause = 500;
  36. public async Task Dothething()
  37. {
  38. Parallel.Invoke(() => Getvalues(globalpause, 1), () => Getvalues(globalpause, 2), () => Getvalues(globalpause, 3)
  39. , () => Getvalues(globalpause, 4), () => Getvalues(globalpause, 5), () => Getvalues(globalpause, 6)
  40. , () => Getvalues(globalpause, 7), () => Getvalues(globalpause, 8), () => Getvalues(globalpause, 9)
  41. , () => Getvalues(globalpause, 10));
  42.  
  43. }
  44.  
  45.  
  46.  
  47. public Object locker = new Object();
  48.  
  49. public async Task Getvalues(int pause = 0, int num = 1)
  50. {
  51. var counter = 0;
  52. string log = $"Thread { num}\n";
  53. while (_lock <= empty_array.Length - 1)
  54. {
  55.  
  56. var message = $"{ empty_array[_lock] }\n";
  57. log += message;
  58. await _hubContext.Clients.All.SendAsync("ReceiveMessage", num, message);
  59. await Task.Delay(pause);
  60. Incrementlock();
  61. counter++;
  62.  
  63. }
  64. log += $"Total {counter} numbers \n";
  65. await _hubContext.Clients.All.SendAsync("ReceiveMessage", num, $"\n Total {counter} numbers");
  66. global[num] = log;
  67. System.IO.File.WriteAllLines("SavedLists.txt", global);
  68. }
  69.  
  70. private readonly IHubContext<ChatHub> _hubContext;
  71.  
  72. public HomeController(IHubContext<ChatHub> hubContext)
  73. {
  74. _hubContext = hubContext;
  75. }
  76.  
  77. public int Incrementlock()
  78. {
  79. lock (locker)
  80. {
  81. _lock++;
  82. return _lock;
  83. }
  84. }
  85. }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement