简体   繁体   中英

ASP.NET : PerformanceCounter displays nothing

I am a complete beginner in asp.net, C#, etc...

I saw this and this

Then I try this:

using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;
using System.Text;
using System.Net;
using System.Collections.Generic;
using System.Xml;

using System.Collections;
using System.Collections.Specialized;
using System.Diagnostics;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        Response.Write("Hello from code behind");

        PerformanceCounter cpuload = new PerformanceCounter();
        cpuload.CategoryName = "Processor";
        cpuload.CounterName = "% Processor Time";
        cpuload.InstanceName = "_Total";

        //or PerformanceCounter cpuload = new PerformanceCounter("Processor", "% Processor Time", "_Total");

        Console.WriteLine(cpuload.NextValue() + "%");

    }
}

But that only displays Hello from code behind . Why please?

You're writing to the console, which won't work, write to the response instead (just as your string does):

Response.Write(cpuload.NextValue().ToString() + "%");

While applications types that aren't strictly console applications can have an associated console, they're not visible unless explicitly made so, and couldn't be in a web application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM