NancyFx框架檢測(cè)任務(wù)管理器詳解
本文為大家分享了NancyFx框架檢測(cè)任務(wù)管理器的具體方法,供大家參考,具體內(nèi)容如下
先建一個(gè)空的項(xiàng)目和之前的NancyFx系列一樣的步驟


然后建三個(gè)文件夾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頁(yè)面在根目錄下

<!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>
繼續(xù)往根目錄里面添加Startup類
[assembly:OwinStartup(typeof( NancyFxTaskManager.Startup))]
namespace NancyFxTaskManager
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.MapSignalR().UseNancy();
}
}
}

好了我們準(zhǔn)備就緒,看看運(yùn)行效果

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用C#實(shí)現(xiàn)網(wǎng)頁(yè)內(nèi)容保存為圖片并生成壓縮包
這篇文章主要為大家詳細(xì)介紹了如何使用C#實(shí)現(xiàn)網(wǎng)頁(yè)內(nèi)容保存為圖片并生成壓縮包,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-02-02
C#中判斷、驗(yàn)證字符串是否為日期格式的實(shí)現(xiàn)代碼
這篇文章主要介紹了C#中判斷、驗(yàn)證字符串是否為日期格式的實(shí)現(xiàn)代碼,使用DateTime類中自帶的兩個(gè)方法實(shí)現(xiàn),需要的朋友可以參考下2014-08-08
C#面向?qū)ο筇卣鞯木唧w實(shí)現(xiàn)及作用詳解
所有的面相對(duì)象思想,歸根結(jié)底是為了簡(jiǎn)化代碼,減少代碼量,構(gòu)建更符合現(xiàn)實(shí)生活邏輯的程序代碼,從而減輕程序員的負(fù)擔(dān)。不能一味地或者說(shuō)刻意地去使用面相對(duì)象的思想而忽略了程序所實(shí)現(xiàn)的功能或者框架,要根據(jù)實(shí)際情況2013-10-10
C# 使用AspriseOCR.dll實(shí)現(xiàn)驗(yàn)證碼識(shí)別
這篇文章主要介紹了C# 使用AspriseOCR.dll實(shí)現(xiàn)驗(yàn)證碼識(shí)別,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
C# 全角和半角轉(zhuǎn)換以及判斷的簡(jiǎn)單代碼
這篇文章介紹了在C#中判斷和轉(zhuǎn)換全角半角的方法,有需要的朋友可以參考一下2013-07-07

