欢迎来到代码驿站!

.NET代码

当前位置:首页 > 软件编程 > .NET代码

NancyFx框架检测任务管理器详解

时间:2021-01-24 11:06:35|栏目:.NET代码|点击:

本文为大家分享了NancyFx框架检测任务管理器的具体方法,供大家参考,具体内容如下

先建一个空的项目和之前的NancyFx系列一样的步骤

然后建三个文件夹Models,Module,Views

然后分别安装一下组件

jQuery

Microsoft.AspNet.SignalR

Microsoft.Owin

Nancy

Nancy.Owin

然后往Model类里面添加CPUHub类,Broadcaster类

CPUHub类

public class CPUHub:Hub
  {
    private readonly Broadcaster broadcaster;
    public CPUHub():this(Broadcaster.broadcaster)
    {

    }
    public CPUHub(Broadcaster broadcaster)
    {
      this.broadcaster = broadcaster;
    }
  }

Broadcaster类

public class Broadcaster
  {
    private readonly static Lazy<Broadcaster> lazy = new Lazy<Broadcaster>(()=>new Broadcaster(GlobalHost.ConnectionManager.GetHubContext<CPUHub>().Clients));

    private readonly TimeSpan timeSpan = TimeSpan.FromMilliseconds(1000);
    private readonly Timer timer;
    public static Broadcaster broadcaster
    {
      get { return lazy.Value; }
    }
    private IHubConnectionContext hubConnectionContext
    {
      get;
      set;
    }
    private Broadcaster(IHubConnectionContext hubConnectionContexts)
    {
      hubConnectionContext = hubConnectionContexts;
      timer = new Timer(BroadcastCpuUsage,null,timeSpan,timeSpan);
    }
    private void BroadcastCpuUsage(object o)
    {
      string cpu = GetCurrentCpu();

    }
    private string GetCurrentCpu()
    {
      string currentCpu = "";
      HttpClient httpClient = new HttpClient();
      httpClient.BaseAddress = new Uri("http://localhost:3039");
      var response = httpClient.GetAsync("api/cpu").Result;
      if (response.IsSuccessStatusCode)
      {
        currentCpu = response.Content.ReadAsStringAsync().Result;
      }
      return currentCpu;
    }
  }

然后在往Module里面添加CPUModule类

public class CPUModule:NancyModule
  {
    PerformanceCounter performanceCounter;
    public CPUModule():base("api/cpu")
    {
      InitializePerformanceCounter();
      Get("/",Lexan=> 
      {
        int cpu = (int)Math.Ceiling(performanceCounter.NextValue());
        return Response.AsText(cpu.ToString());
      });
    }
    private void InitializePerformanceCounter()
    {
      performanceCounter = new PerformanceCounter();
      performanceCounter.CategoryName = "";
      performanceCounter.CounterName = "";
      performanceCounter.InstanceName = "";
      performanceCounter.NextValue();
      Thread.Sleep(1000);
    }
  }

然后添加index.html页面在根目录下

<!DOCTYPE html>
<html>
<head>
  <title>NancyTaskManager</title>
</head>
<body>
  <label id="lblVal"></label>
  <br />
  <canvas id="cvPercentage"></canvas>
  <br />
  <br />
  <canvas id="cvGraph" height="450" width="600"></canvas>
  <script src="Scripts/jquery-2.1.0.js"></script>
  <script src="Scripts/jquery.signalR-2.0.2.js"></script>
  <script src="Scripts/Chart.js"></script>
  <script src="/signalr/hubs"></script>
  <script src="Scripts/taskManager.js"></script>
</body>
</html>

继续往根目录里面添加Startup类

[assembly:OwinStartup(typeof( NancyFxTaskManager.Startup))]
namespace NancyFxTaskManager
{
  public class Startup
  {
    public void Configuration(IAppBuilder app)
    {
      app.MapSignalR().UseNancy();
      
    }
  }
}

好了我们准备就绪,看看运行效果

上一篇:C#生成饼形图及添加文字说明实例代码

栏    目:.NET代码

下一篇:Unity切割图集转换为多张图片

本文标题:NancyFx框架检测任务管理器详解

本文地址:http://www.codeinn.net/misctech/50133.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有