GG.
GG.

Reputation: 21884

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?

Upvotes: 0

Views: 322

Answers (1)

Grant Thomas
Grant Thomas

Reputation: 45068

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.

Upvotes: 1

Related Questions