Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Threading.Tasks;
- using Microsoft.AspNetCore.Mvc;
- using System.Diagnostics;
- using Microsoft.AspNetCore.SignalR;
- using SignalRChat.Hubs;
- namespace array_fill.Controllers
- {
- public class HomeController : Controller
- {
- static readonly int max_value = 1000;
- public int[] empty_array = new int[max_value];
- public string[] global = new string[11];
- public volatile int _lock = 0;
- public IActionResult Index()
- {
- Stopwatch stopwatch = new Stopwatch();
- stopwatch.Start();
- for (int i = 0; i < max_value; i++)
- {
- empty_array[i] = i;
- }
- _lock = 0;
- stopwatch.Stop();
- Dothething();
- ViewBag.help = stopwatch.ElapsedMilliseconds;
- return View();
- }
- public int globalpause = 500;
- public async Task Dothething()
- {
- Parallel.Invoke(() => Getvalues(globalpause, 1), () => Getvalues(globalpause, 2), () => Getvalues(globalpause, 3)
- , () => Getvalues(globalpause, 4), () => Getvalues(globalpause, 5), () => Getvalues(globalpause, 6)
- , () => Getvalues(globalpause, 7), () => Getvalues(globalpause, 8), () => Getvalues(globalpause, 9)
- , () => Getvalues(globalpause, 10));
- }
- public Object locker = new Object();
- public async Task Getvalues(int pause = 0, int num = 1)
- {
- var counter = 0;
- string log = $"Thread { num}\n";
- while (_lock <= empty_array.Length - 1)
- {
- var message = $"{ empty_array[_lock] }\n";
- log += message;
- await _hubContext.Clients.All.SendAsync("ReceiveMessage", num, message);
- await Task.Delay(pause);
- Incrementlock();
- counter++;
- }
- log += $"Total {counter} numbers \n";
- await _hubContext.Clients.All.SendAsync("ReceiveMessage", num, $"\n Total {counter} numbers");
- global[num] = log;
- System.IO.File.WriteAllLines("SavedLists.txt", global);
- }
- private readonly IHubContext<ChatHub> _hubContext;
- public HomeController(IHubContext<ChatHub> hubContext)
- {
- _hubContext = hubContext;
- }
- public int Incrementlock()
- {
- lock (locker)
- {
- _lock++;
- return _lock;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement