NancyFx框架检测任务管理器详解
程序员文章站
2023-12-14 14:17:10
本文为大家分享了nancyfx框架检测任务管理器的具体方法,供大家参考,具体内容如下
先建一个空的项目和之前的nancyfx系列一样的步骤
然后建三个文件夹mo...
本文为大家分享了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(); } } }
好了我们准备就绪,看看运行效果
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读
-
NancyFx框架检测任务管理器详解
-
windows下C#定时管理器框架Task.MainForm详解
-
详解Python的Twisted框架中reactor事件管理器的用法
-
windows下C#定时管理器框架Task.MainForm详解
-
详解Python的Twisted框架中reactor事件管理器的用法
-
Spring集成Quartz定时任务框架介绍和Cron表达式详解
-
Spring集成Quartz定时任务框架介绍和Cron表达式详解
-
RDIFramework.NET框架基于Quartz.Net实现任务调度详解及效果展示
-
详解Python的Twisted框架中reactor事件管理器的用法
-
yii框架通过控制台命令创建定时任务步骤详解