每次调用Signalr Hub类时继续运行一项任务 [英] Keep running one task every time Signalr Hub class is called

查看:71
本文介绍了每次调用Signalr Hub类时继续运行一项任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要监视ASP.NET MVC5应用程序上的CPU性能,并且用户可以选择要监视的PC.我使用SignalR监视实时CPU性能.它适用于第一个CPU,问题是一旦用户选择要监视的另一台PC,根据解决方案

您需要使用网上论坛和服务器广播.

客户端:更改PC后,请保留现有的PC组"pcName1",并加入所选的PC组"pcName2".

服务器:首先,尝试服务器广播通过以下方式从服务器广播到每个组:

  var客户端= GlobalHost.ConnectionManager.GetHubContext< MonitorHub>().Clients;client.Group("pcName1").updateValues(values) 

稍后编辑

任务使用线程池中的后台线程.不建议使用Abort方法取消线程.

为了使用SignalR每隔几秒钟更新一次图表,您有两个选择:

  1. javascript 中使用 SetInterval()调用集线器并每隔几秒钟获取一次值.不要启动任何任务,只需返回值即可.这样,当客户端断开连接或更换PC时,您将不必中止任何正在运行的任务.

  2. 从中心中未创建 的作业广播服务器

    • 跟踪与已连接客户端的组(使用 JoinGroup() LeaveGroup())
    • 在后台作业中,对于每个组(PC),获取值并将其广播给连接的客户端

有用的链接:

  • FluentScheduler
  • 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);
    
        }
    

    解决方案

    You need to work with Groups and Server Broadcast.

    Client: when the the PC is changed, leave the existing PC group "pcName1" and join the selected PC group "pcName2".

    Server: First, try Working with Groups in SignalR to implement the groups. After that, use the Server Broadcast to broadcast to each group from the server with:

    var clients = GlobalHost.ConnectionManager.GetHubContext<MonitorHub>().Clients;
    clients.Group("pcName1").updateValues(values)
    

    Later Edit

    Tasks use background threads from the thread pool. Canceling threads using the Abort method is not recommended.

    In order to update your chart every few seconds using SignalR, you have two options:

    1. Use SetInterval() in your javascript to call your hub and get the values every few seconds. Don't start any tasks, just return the values. This way, when the client disconnects or changes the PC, you won't have to abort any running tasks.

    2. Server broadcast from a job that is not created in the hub

      • keep track of the groups with connected clients (using JoinGroup() and LeaveGroup())
      • in the background job, for each group(PC), get the values and broadcast them to connected clients

    Useful links:

    这篇关于每次调用Signalr Hub类时继续运行一项任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆