I want to monitor CPU performance on my ASP.NET MVC5 application and users are able to select the PC they want to be monitored. I use SignalR for monitoring real time CPU performance. It works well for first CPU, the problem is once user select another PC to be monitored, another new Task according to this link will be created so that both of the previous and new Task will sent their data to the client chart.
What is the solution for handling my problem so that once user change the PC just corresponding PC's data will send to the client side chat or maybe I need to change my code so that just the first current Task can keep sending data.
I really appreciate any help.
Here is my code which is called every time user select any PC :
public class MonitorHub : Hub
{
CancellationTokenSource wtoken;
public MonitorHub()
{
wtoken = new CancellationTokenSource();
}
public void StartCounterCollection(string pcName)
{
var unitOfWork = new CMS.Data.UnitOfWork();
var task = Task.Factory.StartNew(async () =>
{
var perfService = new PerfCounterService();
// await FooAsync(perfService, ref pcName);
while (true)
{
var result = perfService.GetResult(pcName);
Clients.All.newCounter(result);
await Task.Delay(2000, wtoken.Token);
}
}
, wtoken.Token);
}